Lambda with S3 event triggers in AWS - Time & Space Complexity
We want to understand how the time to process files grows when using Lambda triggered by S3 events.
Specifically, how does the number of files affect the number of Lambda executions?
Analyze the time complexity of the following operation sequence.
// S3 bucket configured to trigger Lambda on object creation
// Lambda function processes each new object
exports.handler = async (event) => {
for (const record of event.Records) {
const bucket = record.s3.bucket.name;
const key = record.s3.object.key;
// Process the object identified by bucket and key
}
};
This sequence shows Lambda triggered by S3 when new files are added, processing each file individually.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Lambda function invocation triggered by each S3 object creation event.
- How many times: Once per new object uploaded to the S3 bucket.
Each new file uploaded causes one Lambda invocation, so the total executions grow directly with the number of files.
| 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 more files are added.
Time Complexity: O(n)
This means the total Lambda executions increase directly in proportion to the number of files uploaded.
[X] Wrong: "One Lambda invocation can process all files at once, so time stays the same regardless of file count."
[OK] Correct: Each file upload triggers a separate event and Lambda invocation; they do not batch automatically.
Understanding how event-driven functions scale with input size helps you design systems that handle growth smoothly and predict costs.
"What if the Lambda function was triggered by a batch of S3 events instead of one per file? How would the time complexity change?"
Practice
Solution
Step 1: Understand S3 event triggers
S3 event triggers can invoke Lambda functions automatically on specific events like object creation.Step 2: Identify the event type for object creation
When a new file is uploaded, the event type is 'ObjectCreated', which triggers the Lambda function.Final Answer:
The Lambda function runs automatically whenever a new file is uploaded to the S3 bucket. -> Option CQuick Check:
S3 ObjectCreated event triggers Lambda automatically [OK]
- Confusing object deletion with creation events
- Thinking Lambda runs manually without triggers
- Assuming triggers only work on bucket deletion
Solution
Step 1: Identify correct event type for object creation
The event type for object creation is "s3:ObjectCreated:*" which covers all create events.Step 2: Check Lambda function ARN placement
The LambdaFunctionArn must point to the Lambda function's ARN, not the bucket ARN.Final Answer:
{ "Events": ["s3:ObjectCreated:*"], "LambdaFunctionArn": "arn:aws:lambda:region:account-id:function:myFunction" } -> Option AQuick Check:
Use "s3:ObjectCreated:*" for creation triggers [OK]
- Using ObjectRemoved event instead of ObjectCreated
- Putting bucket ARN instead of Lambda ARN
- Using invalid event names like ObjectCreated:Delete
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
return f"File {key} uploaded to {bucket}"What will be the output if a file named 'photo.jpg' is uploaded to bucket 'my-images'?
Solution
Step 1: Extract bucket and key from event
The code accesses event['Records'][0]['s3']['bucket']['name'] as bucket name and event['Records'][0]['s3']['object']['key'] as file key.Step 2: Substitute values from the upload event
For file 'photo.jpg' uploaded to 'my-images', bucket='my-images' and key='photo.jpg'.Final Answer:
"File photo.jpg uploaded to my-images" -> Option BQuick Check:
Event keys map file and bucket correctly [OK]
- Mixing bucket name and object key positions
- Assuming event keys are missing causing errors
- Confusing string formatting output
Solution
Step 1: Check permissions for S3 to invoke Lambda
S3 must have permission to invoke the Lambda function via a resource-based policy on Lambda.Step 2: Understand common misconfigurations
Missing this permission causes Lambda not to trigger even if bucket notifications are set.Final Answer:
The S3 bucket notification configuration is missing permission for S3 to invoke Lambda. -> Option AQuick Check:
S3 needs invoke permission on Lambda [OK]
- Confusing Lambda execution role with S3 invoke permission
- Assuming code errors prevent invocation
- Thinking region mismatch blocks invocation
Solution
Step 1: Use event type for object creation
Use 's3:ObjectCreated:*' to capture all create events including uploads.Step 2: Apply suffix filter for '.png'
Configure the bucket notification with a filter to trigger Lambda only for files ending with '.png'.Final Answer:
Set the event type to 's3:ObjectCreated:*' and add a filter with suffix '.png' in the bucket notification configuration. -> Option DQuick Check:
Use event filters to limit Lambda triggers by file type [OK]
- Using ObjectRemoved event instead of ObjectCreated
- Relying only on Lambda code filtering without event filters
- Using prefix filter with file extension instead of suffix
Ready to prove it?
Pass the test and this pattern moves unknown
