Creating a Lambda function in AWS - Performance & Efficiency
When creating a Lambda function, it's important to understand how the time to complete the setup grows as you add more resources or configurations.
We want to know how the number of steps or API calls changes as we create more Lambda functions.
Analyze the time complexity of the following operation sequence.
# Create a Lambda function
aws lambda create-function \
--function-name MyFunction \
--runtime python3.12 \
--role arn:aws:iam::123456789012:role/lambda-role \
--handler lambda_function.lambda_handler \
--zip-file fileb://function.zip
This sequence creates one Lambda function with specified runtime, role, handler, and code package.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: The
create-functionAPI call to provision a Lambda function. - How many times: Once per Lambda function created.
Each Lambda function requires one API call to create it, so the total calls grow directly with the number of functions.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of API calls increases one-to-one with the number of Lambda functions created.
Time Complexity: O(n)
This means the time to create Lambda functions grows linearly with how many you create.
[X] Wrong: "Creating multiple Lambda functions can be done with a single API call, so time stays the same."
[OK] Correct: Each Lambda function requires its own create call; there is no batch create API, so time grows with the number of functions.
Understanding how API calls scale when creating cloud resources shows you can plan for growth and manage deployment time effectively.
"What if we used a deployment tool that creates multiple Lambda functions in parallel? How would the time complexity change?"
Practice
Solution
Step 1: Understand Lambda's role
AWS Lambda lets you run code without needing to manage servers or infrastructure.Step 2: Compare options
The incorrect options describe other AWS services such as user permissions (IAM), data storage (S3), and virtual machines (EC2).Final Answer:
To run code without managing servers -> Option AQuick Check:
Lambda = serverless code execution [OK]
- Confusing Lambda with storage services
- Thinking Lambda manages servers
- Mixing Lambda with user management
Solution
Step 1: Understand handler format
The handler is specified asfilename.functionname. Commonly, the file isindexand function ishandler.Step 2: Match options
Onlyindex.handlermatches the correct format; others reverse or misuse parts.Final Answer:
index.handler -> Option BQuick Check:
Handler = filename.function [OK]
- Swapping filename and function name
- Using invalid handler names
- Confusing handler with runtime
def handler(event, context):
return event.get('key', 'default')What will be the output if the event is
{}?Solution
Step 1: Analyze the handler function
The function returns the value of 'key' from event, or 'default' if 'key' is missing.Step 2: Check the event input
The event is an empty dictionary, so 'key' is not present, so it returns 'default'.Final Answer:
"default" -> Option AQuick Check:
Missing key returns default [OK]
- Assuming missing key returns None
- Expecting an error on missing key
- Confusing key name with value
Solution
Step 1: Understand permissions in Lambda
Lambda functions need an IAM role with permissions to run and access resources.Step 2: Identify cause of permissions error
A permissions error usually means the execution role is missing required permissions.Final Answer:
The Lambda execution role lacks necessary permissions -> Option DQuick Check:
Permissions error = missing IAM role rights [OK]
- Blaming handler name for permissions error
- Ignoring IAM role setup
- Assuming runtime causes permissions error
Solution
Step 1: Configure S3 event trigger
To run Lambda automatically on uploads, configure S3 to trigger Lambda on 'ObjectCreated' events.Step 2: Assign correct IAM role permissions
Lambda needs an execution role with permissions to read from S3 to process images.Final Answer:
Set S3 event trigger on bucket for 'ObjectCreated' and assign Lambda execution role with S3 read permissions -> Option CQuick Check:
S3 trigger + IAM role = automatic Lambda run [OK]
- Thinking manual invocation is automatic
- Confusing runtime with event triggers
- Using EC2 unnecessarily for Lambda triggers
Ready to prove it?
Pass the test and this pattern moves unknown
