Lambda function concept in AWS - Time & Space Complexity
We want to understand how the time to run a Lambda function changes as we increase the number of events it handles.
How does the number of events affect the total work Lambda does?
Analyze the time complexity of invoking a Lambda function multiple times.
// Pseudocode for invoking Lambda multiple times
for (int i = 0; i < n; i++) {
aws lambda invoke --function-name MyFunction --payload '{"key": "value"}' response.json
}
This sequence calls the same Lambda function n times, each with a small input.
Look at what repeats as we increase n.
- Primary operation: Lambda function invocation API call
- How many times: Exactly n times, once per loop iteration
Each new event causes one Lambda call, so the total calls grow directly with n.
| 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 in a straight line as n increases.
Time Complexity: O(n)
This means if you double the number of events, the total Lambda calls double too.
[X] Wrong: "Calling Lambda multiple times is just one operation regardless of n."
[OK] Correct: Each Lambda call is a separate operation that takes time, so more calls mean more total work.
Understanding how repeated Lambda calls add up helps you design systems that scale well and explain your reasoning clearly.
"What if we changed the Lambda function to process multiple events in one call? How would the time complexity change?"
Practice
Solution
Step 1: Understand Lambda's role
AWS Lambda runs your code without needing you to manage servers.Step 2: Compare options
Options A, C, and D describe other AWS services, not Lambda.Final Answer:
To run code automatically without managing servers -> Option BQuick Check:
Lambda = serverless code execution [OK]
- Confusing Lambda with storage services
- Thinking Lambda creates virtual machines
- Assuming Lambda manages user permissions
Solution
Step 1: Identify correct Python Lambda handler syntax
Lambda handlers in Python must accept two parameters: event and context.Step 2: Check each option
def handler(event, context): return 'Hello World' correctly defines a Python function with event and context parameters and returns a string. function handler(event) { return 'Hello World'; } is JavaScript syntax. lambda handler(event): print('Hello World') misuses lambda keyword. def handler(): print('Hello World') lacks required parameters.Final Answer:
def handler(event, context): return 'Hello World' -> Option CQuick Check:
Python Lambda handler needs event and context [OK]
- Using JavaScript syntax in Python Lambda
- Omitting event or context parameters
- Misusing lambda keyword for handler
def handler(event, context):
name = event.get('name', 'Guest')
return f"Hello, {name}!"What will be the output if the event is
{}?Solution
Step 1: Analyze event input and code behavior
The event is an empty dictionary, so event.get('name', 'Guest') returns the default 'Guest'.Step 2: Determine return value
The function returns the string "Hello, Guest!" because 'name' is not in event.Final Answer:
"Hello, Guest!" -> Option AQuick Check:
Missing key uses default value 'Guest' [OK]
- Assuming missing key causes error
- Thinking None is returned instead of default
- Ignoring default value in get()
def handler(event, context):
return event['message']When triggered with event
{}, it throws a KeyError. How can you fix it?Solution
Step 1: Identify cause of KeyError
Accessing event['message'] when 'message' key is missing causes KeyError.Step 2: Use safe access method
Replacing event['message'] with event.get('message', 'No message') avoids error by providing default.Final Answer:
Use event.get('message', 'No message') instead of event['message'] -> Option AQuick Check:
Use get() to avoid KeyError on missing keys [OK]
- Ignoring missing key causes error
- Thinking print fixes KeyError
- Assuming function name affects key access
Solution
Step 1: Understand event-driven processing
S3 can trigger Lambda automatically when new objects are created, ideal for immediate processing.Step 2: Evaluate options for image resizing
Use S3 event trigger on object creation to invoke Lambda, which resizes images and saves back to S3 uses S3 event triggers and Lambda to resize and save images back to S3, which is efficient and serverless. Run Lambda on a schedule to scan S3 and resize images manually delays processing and requires scanning. Use Lambda to create EC2 instances that resize images misuses Lambda to create servers. Upload images directly to Lambda storage for resizing is invalid as Lambda has no persistent storage for uploads.Final Answer:
Use S3 event trigger on object creation to invoke Lambda, which resizes images and saves back to S3 -> Option DQuick Check:
S3 event triggers + Lambda = best for image processing [OK]
- Thinking Lambda can store files persistently
- Using scheduled scans instead of event triggers
- Misusing Lambda to create servers
Ready to prove it?
Pass the test and this pattern moves unknown
