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?"