0
0
AWScloud~5 mins

Lambda pricing model in AWS - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Lambda pricing model
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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
How Execution Grows With Input

Each additional function call adds one more API request and one more execution.

Input Size (n)Approx. API Calls/Operations
1010 Lambda invocations
100100 Lambda invocations
10001000 Lambda invocations

Pattern observation: The number of operations grows directly with the number of function calls.

Final Time Complexity

Time Complexity: O(n)

This means the total cost grows in a straight line as you run more Lambda functions.

Common Mistake

[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.

Interview Connect

Understanding how costs grow with usage helps you design efficient cloud solutions and explain pricing clearly.

Self-Check

"What if we changed the function to run longer each time? How would the time complexity of cost change?"