Validating Templates

Get ready to learn how to validate the template files which configure your application!

YAML makes it easy to read CloudFormation template files but is very fiddly and error-prone. Small spacing issues can cause problems and that might not be immediately obvious when you read a template. CloudFormation will try deploying resources even when the template is not fully valid and then fail when it starts processing an invalid resource, so feedback on errors might take a long time with complex templates. One of the projects I work on includes custom DNS setup for a content delivery distribution which takes about 35-40 minutes to fully set up, then messing around with a template that you have to wait where you can only find out if it’s correct or not after 40 minutes to check is a great way to lose a whole day just fixing one tiny issue.

CFN Lint

Instead of waiting for CloudFormation to explode on invalid templates, use CFN Lint to perform basic validation locally. CFN Lint understands CloudFormation resources, so it is smart enough to catch when you mistype a property name. Install CFN Lint locally using the following command:

pip install cfn-lint

You can then check the basic syntax of your templates from the command line easily:

$ cfn-lint template.yaml 
E0001 Error transforming template: Resource with id [HelloWorldFunction] is invalid. Missing required property 'Handler'.

Get hands-on with 1000+ tech skills courses.