🧪 Experiment: Create an AWS Lambda Function
That Logs a Message When an Image is Added to an
S3 Bucket
✅ Objective:
To automatically trigger an AWS Lambda function when a new image is uploaded to
an S3 bucket.
The Lambda function should log: "An image has been added"
🧰 Tools Required:
Tool Purpose
AWS Account To access Lambda and S3 services
S3 Bucket To store uploaded images
Lambda To write serverless logging function
IAM Roles To allow Lambda access to S3 & logs
📚 Theory:
🔷 AWS Lambda + S3 Integration:
AWS Lambda can be triggered by events in S3, such as PUT
(upload) events.
Common use cases:
o Image processing
o Logging file uploads
o Automating file conversions
🔶 Workflow:
1. Create an S3 bucket.
2. Create a Lambda function.
3. Add an S3 trigger to invoke the Lambda on object creation.
4. The Lambda logs: "An image has been added".
🔽 Diagram Placeholder: S3 Upload → S3 Event Trigger → Lambda → Logs
🔧 Implementation Steps
▶️Step 1: Create an S3 Bucket
1. Go to AWS Console → S3
2. Click Create Bucket
o Bucket name: my-image-uploads
o Region: Same as Lambda
3. Enable Block Public Access (default)
4. Click Create Bucket
▶️Step 2: Create a Lambda Function
1. Go to AWS Console → Lambda
2. Click Create Function
o Function name: logImageUpload
o Runtime: Python 3.9
o Permissions: Create a new role with basic Lambda permissions
▶️Step 3: Write the Lambda Code
Paste the following into the inline code editor:
import json
def lambda_handler(event, context):
print("An image has been added")
print("Event details:")
print(json.dumps(event)) # Optional: Log the S3 event
return {
'statusCode': 200,
'body': json.dumps('Image upload logged successfully')
}
Click Deploy.
▶️Step 4: Add S3 Trigger to Lambda
1. Go to Configuration → Triggers → Add trigger
2. Choose S3
o Bucket: my-image-uploads
o Event type: PUT
o Prefix: (optional) images/
o Suffix: .jpg or .png to limit to image uploads
3. Click Add
▶️Step 5: Upload an Image to S3
1. Go to your S3 bucket
2. Click Upload
o Select a .jpg or .png image
3. Click Upload
▶️Step 6: View Lambda Logs
1. Go to AWS Lambda → Monitor → View Logs in CloudWatch
2. Open the latest log stream
3. You should see:
An image has been added
✅ Output:
Whenever an image is uploaded to the specified S3 bucket, the Lambda function is
triggered.
The log message "An image has been added" is displayed in CloudWatch Logs.
🔽 Screenshot Placeholder: CloudWatch log with the message "An image has been added"
📌 Conclusion:
This experiment demonstrates how AWS Lambda can be used for event-driven
automation.
By integrating Lambda with S3, we can log or process files immediately upon upload.
This is useful for image processing, watermarking, alerting, or storing metadata.