Amazon Web Services (AWS) CloudFormation is Amazon’s primary infrastructure-as-code (IaC) service. Nearly any resource that can be created through the AWS console can (and should) be created using CloudFormation templates. IaC is a cornerstone of the modern software development lifecycle. With IaC, solutions can be deployed, downtown, and redeployed as often as needed.
Quoting the noted software developer Spider Man, “with great power comes great responsibility.” CloudFormation tools need to be used with care and with future maintainability in mind. The objective is always to be able to deploy an entire solution from scratch and with the least amount of code in your templates.
Pat Ryan, cloud practice lead for Evans & Chambers, has been working with AWS for 4 years, and he presented the following lessons learned during the Tech Talk.
- Deploy early and often.
- Always be able to deploy your entire solution from scratch.
- YAML is preferred over JSON, as YAML supports comments and is more compact.
- Make use of the built-in constants that are available when a stack deploys—for example, AWS::StackId
- Make resource identifiers unique to the stack.
- Nest, but not too deep.
- IAM policies can be complicated to get right in CloudFormation. Separate them into their own template.
- Give your template to a colleague and ask her or him to deploy it. If the deployment fails, you will find out what resources you forgot to put in the template or what dependencies you did not properly factor out.
- Deploy from the command line, as it is faster and more easily repeatable than deploying from the console.
- When creating the template definition for an AWS service that you have never used before, use the documentation and keep the definition as minimal as possible.
- CloudFormation has its limits. Consider other solutions such as AWS Cloud Development Kit (CDK), Ansible, and Terraform.
About the EC Tech Talk Series
The Tech Talk Series is an employee-led platform dedicated to EC’s core value of continual learning. These talks aim to cover a broad range of technology-based topics to promote the sharing of best practices and ideas across EC’s project teams.