1The Infinite Loop Anti-Pattern
A common mistake when setting up S3 triggers is having the Lambda function save its processed output back into the EXACT SAME S3 bucket that triggered it. This causes the new file to trigger the Lambda again, creating an infinite loop that will cost thousands of dollars. Always write output to a different bucket or use a specific prefix/folder rule.
2Step-by-Step Breakdown
Event-Driven Architecture. Lambda acts as the glue between AWS services. Instead of constantly polling databases or queues for changes, Lambda reacts instantly to events.
S3 Event Notifications. You can configure an S3 bucket to trigger a Lambda function whenever an object is created, deleted, or modified.
The 'event' Object. When triggered by S3, Lambda receives a JSON payload (the 'event' object) containing the Bucket Name and the exact Object Key (filename) that triggered the execution.
S3 Trigger Pattern. A classic pattern: User uploads a large image to an S3 bucket. S3 triggers a Lambda. The Lambda resizes the image and saves the thumbnail to a different S3 bucket.
Knowledge Check. When Amazon S3 triggers a Lambda function, which argument inside your Lambda code contains the name of the uploaded file?
- →The 'context' object
- →The 'event' object
Amazon API Gateway. Lambda cannot be accessed directly via a public URL by default. API Gateway allows you to create RESTful and HTTP APIs to securely expose your Lambda functions to the internet.
Lambda Proxy Integration. The most common API Gateway setup. It passes the entire HTTP request (headers, query string parameters, body) directly into the Lambda 'event' object.
Returning HTTP Responses. When using API Gateway Proxy Integration, your Lambda function MUST return a specific JSON dictionary containing a 'statusCode' and a 'body' string.
Synchronous vs Asynchronous. API Gateway triggers Lambda *synchronously* (the API waits for Lambda to finish and return a response). S3 triggers Lambda *asynchronously* (S3 fires the event and immediately forgets about it).
Summary. Use S3 triggers for background processing, and API Gateway for user-facing REST APIs.
