How to Trigger AWS Lambda from S3 Events
You can trigger a
AWS Lambda function from S3 by configuring an event notification on the S3 bucket. This notification sends events like object creation to Lambda, which then runs your code automatically.Syntax
To trigger Lambda from S3, you configure an S3 event notification that points to your Lambda function. The key parts are:
Bucket: The S3 bucket where events happen.Event: The type of event to listen for, likes3:ObjectCreated:*.LambdaFunctionArn: The Amazon Resource Name of the Lambda function to invoke.
terraform
resource "aws_lambda_function" "example" { function_name = "example_lambda" runtime = "python3.9" handler = "lambda_function.lambda_handler" role = aws_iam_role.lambda_exec.arn filename = "lambda_function_payload.zip" } resource "aws_s3_bucket" "example_bucket" { bucket = "example-trigger-bucket" } resource "aws_lambda_permission" "allow_s3" { statement_id = "AllowS3Invoke" action = "lambda:InvokeFunction" function_name = aws_lambda_function.example.function_name principal = "s3.amazonaws.com" source_arn = aws_s3_bucket.example_bucket.arn } resource "aws_s3_bucket_notification" "bucket_notification" { bucket = aws_s3_bucket.example_bucket.id lambda_function { lambda_function_arn = aws_lambda_function.example.arn events = ["s3:ObjectCreated:*"] } depends_on = [aws_lambda_permission.allow_s3] }
Example
This example shows how to set up an S3 bucket that triggers a Lambda function whenever a new object is created. The Lambda function simply logs the event details.
python
import json def lambda_handler(event, context): print("Received event:", json.dumps(event)) return { 'statusCode': 200, 'body': json.dumps('S3 event processed successfully') }
Output
Received event: { ... S3 object created event details ... }
Common Pitfalls
- Missing Lambda permission: You must grant S3 permission to invoke your Lambda using
lambda:InvokeFunction. Without this, the trigger won't work. - Incorrect event type: Use the correct event like
s3:ObjectCreated:*to catch uploads. - Bucket notification not updated: Changes to bucket notifications require redeployment or update to take effect.
terraform
/* Wrong: Missing permission */ resource "aws_s3_bucket_notification" "wrong" { bucket = aws_s3_bucket.example_bucket.id lambda_function { lambda_function_arn = aws_lambda_function.example.arn events = ["s3:ObjectCreated:*"] } } /* Right: Add permission resource */ resource "aws_lambda_permission" "allow_s3" { statement_id = "AllowS3Invoke" action = "lambda:InvokeFunction" function_name = aws_lambda_function.example.function_name principal = "s3.amazonaws.com" source_arn = aws_s3_bucket.example_bucket.arn }
Quick Reference
Remember these key points when triggering Lambda from S3:
- Configure S3 event notifications for the bucket.
- Grant Lambda permission to be invoked by S3.
- Use event types like
s3:ObjectCreated:*for uploads. - Deploy changes carefully to update triggers.
Key Takeaways
Configure S3 event notifications to trigger Lambda on object events.
Grant Lambda permission for S3 to invoke it using lambda:InvokeFunction.
Use correct event types like s3:ObjectCreated:* to catch uploads.
Update and redeploy bucket notifications after changes.
Test your Lambda with sample S3 events to verify triggers.