Infrastructure as Code

In this lesson, you will learn about the Cloudformation template and the CloudFormation stack. Additionally, you’ll get a detailed look at the ​`template.yaml` of the project you initialized.

The CloudFormation template #

For deploying applications, SAM uses CloudFormation, an AWS service for managing infrastructure as code. This means that CloudFormation converts a source file describing an application infrastructure (called template) into a set of running, configured cloud resources (called stack).

Instead of individually configuring different resources such as file storage, databases, and queues with CloudFormation, you just need to declare the required resources in a textual file. The template.yaml file in your project directory is a CloudFormation template.

The CloudFormation stack #

You can use CloudFormation to create a whole stack of resources from the template in a single command. It is also smart enough to detect the differences between a template and a deployed stack, making it easy to update infrastructure resources in the future.

Benefits of CloudFormation #

  • You can modify the template file and CloudFormation will reconfigure or delete only the resources that actually need to change.

  • If a resource update fails for whatever reason, CloudFormation will reset all the other resources to the previous configuration, managing a whole set of infrastructural components as a single unit. This makes it easy and safe for a whole team of developers to add, remove, or reconfigure the infrastructural services supporting an application.

  • It also becomes trivially simple to know which version of infrastructure is compatible with which version of code. This supports infrastructure traceability and reproducible installations. (It’s not a coincidence that the template file is in the same directory as the function source; they should be committed to a version control system together).

  • Another benefit of CloudFormation is that you can share templates with other teams, or even publish them online so that others can set up your application with a single click.

CloudFormation Template Formats #

CloudFormation supports JSON and YAML template formats. In this course, you’ll use YAML because it is easier to read in print. One downside of YAML is that whitespace is important and getting the right indentation might be a bit fiddly. If you want more control over the structure of your templates, feel free to use JSON instead. YAML is actually a superset of JSON, so you can also embed JSON into YAML for sections where you want to make structure clear and avoid problems with whitespace.

YAML CloudFormation #

The following file is the template.yaml from the project you created in the previous lesson. Let’s discuss it in detail.

Get hands-on with 1000+ tech skills courses.