Lambda with API Gateway pattern in AWS - Time & Space Complexity
When using Lambda with API Gateway, we want to know how the number of requests affects the work done.
We ask: How does the system handle more users calling the API?
Analyze the time complexity of the following operation sequence.
// API Gateway receives HTTP request
// API Gateway triggers Lambda function
// Lambda processes request and returns response
// API Gateway sends response back to client
This sequence shows how each API call triggers a Lambda function to handle it.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Lambda function invocation triggered by API Gateway request
- How many times: Once per each API request received
Each new API request causes one Lambda invocation, so the work grows directly with the number of requests.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 Lambda invocations |
| 100 | 100 Lambda invocations |
| 1000 | 1000 Lambda invocations |
Pattern observation: The number of Lambda calls grows linearly as requests increase.
Time Complexity: O(n)
This means the total work grows in direct proportion to the number of API requests.
[X] Wrong: "Lambda runs once and handles all requests at the same time."
[OK] Correct: Each request triggers a separate Lambda run, so work grows with requests, not fixed.
Understanding this helps you explain how serverless scales and costs relate to usage, a key cloud skill.
"What if the Lambda function called another Lambda inside it for each request? How would the time complexity change?"
Practice
Solution
Step 1: Understand API Gateway's function
API Gateway acts as a front door to accept and process HTTP requests from clients.Step 2: Connect API Gateway to Lambda
API Gateway triggers Lambda functions to execute backend logic upon receiving requests.Final Answer:
To receive HTTP requests and trigger Lambda functions -> Option AQuick Check:
API Gateway triggers Lambda = A [OK]
- Confusing API Gateway with data storage
- Thinking Lambda manages servers
- Assuming API Gateway runs background tasks
Solution
Step 1: Identify correct integration type
API Gateway uses 'Lambda Function' integration to directly invoke Lambda functions.Step 2: Specify Lambda function ARN
You must provide the ARN (Amazon Resource Name) of the Lambda function to link it properly.Final Answer:
Set the integration type to Lambda Function and select the function ARN -> Option CQuick Check:
Integration type Lambda Function + ARN = D [OK]
- Using HTTP integration instead of Lambda Function
- Confusing MOCK integration with Lambda
- Incorrectly using VPC Link for Lambda
exports.handler = async (event) => {
return { statusCode: 200, body: JSON.stringify({ message: event.queryStringParameters.name }) };
};
What will be the response body if the API Gateway is called with URL ?name=Alice?Solution
Step 1: Understand event object from API Gateway
API Gateway passes query parameters inside event.queryStringParameters, so event.queryStringParameters.name equals 'Alice'.Step 2: Check Lambda return value
Lambda returns JSON string with message key set to 'Alice', so response body is '{"message":"Alice"}'.Final Answer:
{"message":"Alice"} -> Option AQuick Check:
Query param 'name=Alice' -> message = 'Alice' [OK]
- Assuming queryStringParameters is null
- Returning raw object instead of JSON string
- Confusing parameter name with literal string
Solution
Step 1: Understand 502 Bad Gateway meaning
502 error usually means API Gateway received an invalid response from Lambda.Step 2: Check Lambda response format
Lambda must return an object with statusCode and body as string; missing or malformed response causes 502.Final Answer:
Lambda function is not returning a properly formatted response -> Option BQuick Check:
502 error = bad Lambda response format [OK]
- Assuming wrong API Gateway URL causes 502
- Ignoring Lambda execution role issues (causes 403)
- Confusing MOCK integration with Lambda integration
Solution
Step 1: Understand Lambda scaling behavior
Lambda scales automatically with incoming requests without needing reserved concurrency unless limiting concurrency is required.Step 2: Evaluate cost and scalability
Relying on default Lambda scaling avoids paying for reserved concurrency and handles unpredictable spikes efficiently.Step 3: Compare other options
EC2 autoscaling adds server management and cost; MOCK integration doesn't run real code; reserved concurrency limits scaling.Final Answer:
Use API Gateway with Lambda proxy integration and rely on Lambda's default scaling without reserved concurrency -> Option DQuick Check:
Default Lambda scaling + proxy integration = C [OK]
- Setting reserved concurrency unnecessarily limits scaling
- Using EC2 adds complexity and cost
- Using MOCK integration doesn't run Lambda code
Ready to prove it?
Pass the test and this pattern moves unknown
