📖 Scenario: You are setting up an AWS Lambda function that automatically runs when a new file is uploaded to an S3 bucket. This helps process files without manual steps.
🎯 Goal: Build an AWS Lambda function triggered by S3 upload events using Infrastructure as Code. You will create the S3 bucket, configure the Lambda function, and connect the trigger.
📋 What You'll Learn
Create an S3 bucket named exactly my-upload-bucket
Create a Lambda function named exactly ProcessUploadFunction
Configure the Lambda function with runtime python3.12
Set the Lambda function handler to lambda_function.lambda_handler
Add an S3 event trigger on my-upload-bucket for ObjectCreated events
Use valid AWS CloudFormation YAML syntax
💡 Why This Matters
🌍 Real World
Automating file processing workflows by triggering Lambda functions on file uploads to S3 is common in data pipelines, image processing, and serverless applications.
💼 Career
Understanding how to connect AWS Lambda with S3 event triggers is essential for cloud engineers and developers working on serverless architectures and event-driven systems.
Progress0 / 4 steps
1
Create the S3 bucket resource
Create an AWS CloudFormation resource for an S3 bucket named my-upload-bucket. Use the logical ID UploadBucket.
AWS
Hint
Use AWS::S3::Bucket as the resource type and set BucketName property.
2
Add the Lambda function resource
Add a Lambda function resource with logical ID ProcessUploadFunction. Set Runtime to python3.12, Handler to lambda_function.lambda_handler, and use a placeholder Role ARN arn:aws:iam::123456789012:role/lambda-exec-role.
AWS
Hint
Use AWS::Lambda::Function as the resource type and set the required properties.
3
Add S3 event trigger to Lambda
Add an Event source to the Lambda function to trigger on ObjectCreated events from the my-upload-bucket S3 bucket. Use the Events property with logical ID S3UploadEvent inside the Lambda resource.
AWS
Hint
Use the Events property inside the Lambda resource to add the S3 trigger.
4
Add permission for S3 to invoke Lambda
Add an AWS::Lambda::Permission resource with logical ID AllowS3Invoke to allow the S3 bucket my-upload-bucket to invoke the Lambda function ProcessUploadFunction. Set Action to lambda:InvokeFunction, Principal to s3.amazonaws.com, and SourceArn to the ARN of my-upload-bucket.
AWS
Hint
Use AWS::Lambda::Permission to allow S3 to invoke the Lambda function.
Practice
(1/5)
1. What happens when you configure an AWS Lambda function with an S3 event trigger for object creation?
easy
A. The Lambda function runs manually and is not triggered by S3 events.
B. The Lambda function runs only when the S3 bucket is deleted.
C. The Lambda function runs automatically whenever a new file is uploaded to the S3 bucket.
D. The Lambda function runs only when an object is deleted from the S3 bucket.
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 C