AWS Lambda to Amazon S3 via a custom resource

CloudFormation → AWS Lambda → Amazon S3

Creates a Lambda function that writes to an S3 bucket during deployment.

This pattern creates an Amazon S3 object by using an AWS CloudFormation custom resource and an AWS Lambda function. Custom resources enable you to write custom provisioning logic in templates that CloudFormation runs anytime you create, update (if you changed the custom resource), or delete stacks.
This pattern deploys a Lambda function that is responsible for creating (and deleting) S3 objects. The function is given CRUD permissions on objects in the S3 bucket. When the stack is first created, CloudFormation sends a create request to the function. When the stack is deleted, CloudFormation sends a delete request to the function. The function ARN is stored as a CloudFormation Export named "function-s3-create".

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Launch Stack

Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/cfn-custom-resource-s3-create

Deploy

sam deploy --guided


Testing

See the GitHub repo for detailed testing notes.

Cleanup

1. Delete the stack: sam delete --stack-name STACK_NAME.
2. Confirm the stack has been deleted: aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"

Created by:

Greg Davis

Greg Davis

I am a Principal Solutions Architect, Serverless Developer, Database Engineer, and Data Integration Architect. I specialize in architecting and developing Serverless, event-driven solutions.

Follow on LinkedIn