Lambda pricing model in AWS - Time & Space Complexity
We want to understand how the cost of running AWS Lambda changes as we run more functions.
How does the number of function executions affect the total price?
Analyze the time complexity of the following operation sequence.
// Invoke Lambda function multiple times
for (let i = 0; i < n; i++) {
lambda.invoke({
FunctionName: 'myFunction',
Payload: JSON.stringify({ key: i })
}, (err, data) => {
if (err) console.log(err);
});
}
This code calls the same Lambda function n times, each with a different input.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Lambda function invocation API call
- How many times: Exactly n times, once per loop iteration
Each additional function call adds one more API request and one more execution.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 Lambda invocations |
| 100 | 100 Lambda invocations |
| 1000 | 1000 Lambda invocations |
Pattern observation: The number of operations grows directly with the number of function calls.
Time Complexity: O(n)
This means the total cost grows in a straight line as you run more Lambda functions.
[X] Wrong: "Running many Lambda functions at once costs the same as running one."
[OK] Correct: Each function call is separate and adds to the total cost, so more calls mean more cost.
Understanding how costs grow with usage helps you design efficient cloud solutions and explain pricing clearly.
"What if we changed the function to run longer each time? How would the time complexity of cost change?"
Practice
Solution
Step 1: Understand AWS Lambda pricing basics
AWS Lambda charges based on how many times your function runs (requests) and how long it runs (compute time).Step 2: Identify the pricing factors
Memory size affects compute time cost but the main factors are requests and compute time.Final Answer:
Number of requests and compute time -> Option AQuick Check:
Requests + compute time = cost [OK]
- Confusing storage or bandwidth as pricing factors
- Thinking memory size alone determines cost
- Mixing API calls with requests
Solution
Step 1: Recall AWS Lambda pricing units
Lambda pricing is based on number of requests and compute time measured in GB-seconds (memory size x execution time).Step 2: Match correct units
Requests count how many times function runs; GB-seconds measure memory used over time.Final Answer:
Number of requests and GB-seconds -> Option CQuick Check:
Requests + GB-seconds = pricing units [OK]
- Using CPU cycles or network packets incorrectly
- Confusing storage with compute time
- Mixing units like MB with packets
Solution
Step 1: Understand cost components
Cost depends on requests and GB-seconds (memory size x execution time x requests).Step 2: Analyze impact of memory size
Doubling memory from 256 MB to 512 MB doubles GB-seconds, increasing cost significantly, but the number of requests (1,000,000) is the main driver of total cost.Final Answer:
Number of requests being 1,000,000 -> Option DQuick Check:
Requests x GB-seconds = total cost [OK]
- Ignoring number of requests effect
- Thinking network bandwidth affects Lambda cost
- Assuming execution time or memory alone drives cost
Solution
Step 1: Review memory and execution time relation
Cost depends on memory size multiplied by execution time. High memory with short time still costs more than lower memory.Step 2: Identify cost cause
Setting memory to 1024 MB increases GB-seconds cost even if function runs only 100 ms.Final Answer:
Memory size is too high for short execution time -> Option AQuick Check:
High memory + short time can increase cost [OK]
- Assuming short execution time always means low cost
- Ignoring memory size impact
- Thinking free tier always applies
Solution
Step 1: Understand cost relation to memory and time
Cost = requests x memory size x execution time. Reducing memory lowers GB-seconds cost if function still works.Step 2: Evaluate options
Reducing memory to 128 MB saves cost if performance is acceptable. Increasing memory or requests raises cost. Switching to EC2 is a different model and may not be cheaper.Final Answer:
Reduce memory size to 128 MB if function still runs correctly -> Option BQuick Check:
Lower memory reduces GB-seconds cost [OK]
- Increasing memory without checking cost impact
- Thinking more requests reduce cost
- Assuming EC2 always cheaper than Lambda
Ready to prove it?
Pass the test and this pattern moves unknown
