CloudFormation is a pretty capable tool which provides templating functionality for most of the Amazon web services. But still, keeping up with the release cadence of all the AWS services isn’t that easy. So there always is a little gap of what features the console offers and what CloudFormation offers.
So for this use case (and some others like initial data load), AWS introduced custom resources. This Resource basically represents an AWS lambda invocation which is called whenever your template gets instantiated, removed or update.
The states that AWS supports are:
Since some of the AWS API calls take a rather long time, the invocation model of this lambda is asynchronous. I linked the AWS docs for a more detailed explanation of the input structure on the various operations.
What is important here is, that all input structures carry a callback URL which has to be called, so CloudFormation knows it can continue creating/deleting the stack.
If you forget to call the callback or your code doesn’t exit correctly (e.g. uncaught exceptions), then CloudFormation wait a very long time (about 1 hour) until it actually times out.
To make things easier, AWS provides a node module, where all the callback stuff is handled correctly. And even better, it is already pre-installed if the Lambda function is inlined in the CloudFormation template, so no extra bundling is necessary here.
So first I have to create a Lambda function inside my template (it can also be an externally defined lambda, but this makes it easier to demonstrate).
This is the most basic function, which only logs the event and context and then confirms its execution back to CloudFormation.
Now I enhanced my very basic version to at least differentiate between those CloudFormation operations.
Now I have to make this Lambda accessible as CustomResource.
CustomResources look like this.
To bring all this together, this would be the concluding CloudFormation template with all resources.
- Implementing DynamoDB triggers (streams) using CloudFormation
- Using parameters through multiple nested CloudFormation stacks
- Integrate API Gateway with SNS using CloudFormation
- Integrate API Gateway with Kinesis Firehose using CloudFormation
- visualizing a cloudformation template
- hosting a Cloudfront site with S3 and API Gateway
- a serverless cron in AWS CloudFormation
- Publish an S3 Event to Lambda through SNS