...

/

Working with CloudFormation Templates

Working with CloudFormation Templates

Get a working knowledge of AWS CloudFormation templates.

Before provisioning and configuring any AWS cloud resources, we must first understand and learn to work with CloudFormation templates. A CloudFomration template contains all the key instructions to deploy an AWS infrastructure’s resources using CloudFormation.

In this lesson, we’ll explore the basic aspects of a CloudFormation template and how to create a new one or reuse and adapt an existing one according to our needs.

Anatomy of a CloudFormation template

CloudFormation uses templates to define the AWS resources that we want to create and configure. Templates are written in a declarative language, with supported JSON and YAML. All resources that we want to provision and the order in which we want to provision them are essentially declared in a CloudFormation template.

Templates can be version-controlled, just like any other code, making it easier to roll back any changes in case of deployment failure without compromising the entire infrastructure.

Press + to interact

The CloudFormation template has several aspects, and each supported AWS resource has its own syntax and parameters that we need to follow. To keep things simple, we’ll only explore the basic anatomy of a CloudFormation template, which is enough to start working with templates for the commonly used general CloudFormation stacks. The following illustration provides a breakdown of the different sections in a CloudFormation template:

Press + to interact
Breakdown of the CloudFormation template
Breakdown of the CloudFormation template

Here’s the basic skeleton code format for a CloudFormation template in JSON and YAML that covers the first basic layer of the template:

AWSTemplateFormatVersion: "2010-09-09"
# Adding a Comment in a YAML template
Description: >
This is a sample description
in a YAML template that has multiline value
support.
Metadata:
<Template Metadata Formatted as List of YAML Objects>
Parameters:
<Set of Parameters Formatted as List of YAML Objects>
Rules:
<Set of Rules Formatted as List of YAML Objects>
Mappings:
<Set of Mappings Formatted as List of YAML Objects>
Conditions:
<Set of Conditions Formatted as List of YAML Objects>
Transform:
<Set of Transform Formatted as List of YAML Objects>
Resources:
<Set of Resources Formatted as List of YAML Objects>
Outputs:
<Set of Outputs Formatted as List of YAML Objects>
CloudFormation template skeleton code

Note: You can choose either JSON or YAML formats when working with CloudFormation templates. However, due to different syntaxes, the YAML format offers a few more features than the JSON format. For example, in the ...