Your First S3 + Lambda Trigger: Build It, Test It, Clean It Up
A Lambda function that runs whenever a file is uploaded to a bucket and logs the file name.
What we're building: A Lambda function that runs whenever a file is uploaded to a bucket and logs the file name.
- Create the bucket with public access blocked.
- Create the function (Python) with this handler:
1
2
3
4
5
def lambda_handler(event, context):
for record in event["Records"]:
bucket = record["s3"]["bucket"]["name"]
key = record["s3"]["object"]["key"]
print(f"New object: s3://{bucket}/{key}")- Add the S3 trigger for "All object create events" on that bucket.
- Test by uploading a file, then open the function's CloudWatch logs to see the printed line.
- Common pitfall: if your function writes output back to the same bucket that triggers it, it can invoke itself repeatedly. Use a separate output bucket or a prefix filter.
- Clean up: delete the trigger, the function, its log group, and the bucket so nothing keeps costing you.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article