How to Set Up CloudFormation Custom Resources

Many examples on this site rely on CloudFormation custom resources. Custom resources allow you to manage resources within CloudFormation even when those type of resources are not supported natively by CloudFormation. I’ve created a number of custom resources that are available on GitHub, and in this brief tutorial, I will show you how to use them.

Note: This article is not an in-depth explanation of what custom resources are or how to write your own. There are many of those articles on the internet, and likly one day I’ll add another one. This article just helps you get set up with the custom resources I’ve written for you, which are often prerequisites for other examples on the site.

Demo Quickstart - Use My Pre-Built Serverless Service

If you’re just looking to run one of the examples on my site and want to deploy the custom resources without making your own service out of it, then you can use the [example service I already built for you][exsvc]. Here’s how to do that:

Here are those commands so you can run them yourself:
git clone https://github.com/jthomerson/serverless-training-examples.git
cd serverless-training-examples
npm i
cd services/custom-resources/
npm i
../../node_modules/.bin/sls deploy

Making Your Own Service With Silvermine’s CloudFormation Custom Resources

If, instead, you already have some familiarity with deploying serverless code and you want to make your own service / stack that provides custom resources, and you want to base that on our Silvermine open source custom resources, you can do that, too. If you’re using the Serverless framework, then we’ve made it easy for you by providing an example-serverless.yml file in the project. You can add an NPM dependency on @silvermine/cloudformation-custom-resources and copy that example serverless file into your own serverless.yml file and you have something that’s ready to deploy. Here’s how to do that:

Here are those commands so you can run them yourself:
mkdir cloudformation-custom-resources
cd cloudformation-custom-resources/
npm init -y
npm i --save-exact @silvermine/cloudformation-custom-resources
# Some custom resources require a newer version of the AWS SDK than what Lambda
# provides. If you're using one of those, then you also need to run this command:
npm i --save-exact aws-sdk
cp node_modules/\@silvermine/cloudformation-custom-resources/example-serverless.yml \
   ./serverless.yml
sls deploy

Please note: in my examples, I define a specific version of the Serverless framework that the example is compatible with. You’ll see that in the serverless.yml file that you just copied near the top with a frameworkVersion key. So, if you get an error about having an incompatible version of Serverless, check the version in serverless.yml and then do npm i -g serverless@{version} (replacing {version} with the one mentioned in the serverless.yml file). Alternatively, you can try removing or commenting out that line from the serverless.yml file, but do so at your own risk since I can’t guarantee that my examples will work with the version of Serverless you are running.

Stay Informed

Subscribe to the Serving of Serverless newsletter or follow me on Twitter. I’ll keep you current with the serverless world, new training resources, and I will never sell or share your contact information.