Enable Lambda function to an S3 bucket using cloudformation
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To trigger a Lambda function from an S3 bucket in CloudFormation, you need more than just the bucket and the function. The working setup requires three pieces: the Lambda function, permission that allows S3 to invoke it, and the bucket notification configuration that points S3 events at the function.
The Required Resource Relationships
An S3 to Lambda integration has two directions of configuration:
- S3 must know which Lambda function to call.
- Lambda must allow the S3 bucket to invoke it.
If either side is missing, the stack may create successfully but uploads will not trigger the function.
The usual CloudFormation resources are:
- '
AWS::Lambda::Function' - '
AWS::IAM::Role' - '
AWS::Lambda::Permission' - '
AWS::S3::Bucket'
Minimal Working Template
The template below creates a bucket, a Python Lambda function, permission for the bucket to invoke it, and an object-created notification.
The DependsOn matters because S3 validates the invocation target while applying the notification configuration. If the permission is not in place yet, stack creation can fail.
Why AWS::Lambda::Permission Is Required
Many first attempts define the bucket notification and assume that is enough. It is not. S3 needs explicit permission to invoke the function.
This is the relevant part:
Without that permission, S3 events do not have the right to call the function even if the notification exists.
Filtering by Prefix or Suffix
You can restrict the notification to certain keys. For example, trigger only on images uploaded under an incoming/ prefix:
That reduces unnecessary invocations and keeps event routing predictable.
What the Lambda Receives
When S3 triggers the function, the event contains bucket and object metadata. A simple handler can extract the bucket name and key:
That is enough to begin validation, image processing, metadata extraction, or downstream orchestration.
Common Pitfalls
The biggest CloudFormation mistake is circular or invalid ordering. The bucket references the function, while the permission references the bucket. Using DependsOn on the bucket is the practical fix for that creation sequence.
Another common issue is giving the Lambda execution role permission to read from the bucket and assuming that also grants S3 permission to invoke the function. Those are different permission paths.
It is also easy to forget that some bucket updates replace the resource if the bucket name is fixed externally. Be careful when applying changes to production buckets that already contain data.
Summary
- A working S3 to Lambda setup needs the function, bucket notification, and invocation permission.
- '
AWS::Lambda::Permissionis required so S3 can call the function.' - Put the bucket notification in
NotificationConfiguration. - Use
DependsOnso permission exists before S3 validates the target. - Add prefix or suffix filters when only certain objects should trigger the function.
Related reading
- Enable logical replication on Google Cloud Postgres
- Enable S3 ACL access for CloudFront logs
- Enabling HSTS in AWS ELB application load balacer
- Enforce MFA for AWS console login but not for API calls
- endpoints “default-http-backend” not found in Ingress resource
- Enforce Unique consumer group id in Kafka
- Equivalent for Kafka / AWS Kinesis Stream on Google Cloud Platform
- Error AccessControlListNotSupported when trying to create a bucket ACL in AWS

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.