Subscribe an AWS Lambda function to an S3 bucket event using CloudFormation and SAM.
This quick tutorial will show you how to create a CloudFormation template (leveraging SAM) to connect an AWS Lambda function to an S3 event. In this example we will simply log a message to a CloudWatch log when a file is added to the S3 bucket.
As always I like to provide you with the full CloudFormation template in case you just don’t want to read up on the full solution.
Full Source: https://github.com/dsandor/example-aws-s3-lambda
So if you take a look at the template.yaml
file there are 3 resources:
- An S3 Bucket that I named S3Bucket
.
- A Lambda Function that will be called when a file is added to the bucket. S3TriggeredLambdaHandler
- A Lambda Permission that allows the S3 service to call your Lambda. AllowS3ToCallLambdaPermisson