Components of CloudFormation Template File
Explore the anatomy of the AWS CloudFormation template.
We'll cover the following
A CloudFormation template is a script in JSON or YAML format that describes the stack to be built for our application deployment. Let’s look at the structure and sections of the AWS CloudFormation template.
Template sections
CloudFormation offers ten different sections in a template. Out of all the sections, only one is required, and the rest are optional. AWS recommends following this logical order while building the template:
-
AWSTemplateFormatVersion
(optional): This is the AWS CloudFormation template version. The only valid value is2010-09-09
, and this version must be declared as a literal string. -
Description
(optional): This section is typically devoted to writing comments or a brief explanation of the purpose of this template. The value must be a literal string and cannot exceed1024
bytes. -
Metadata
(optional): Metadata is an object that provides additional details about specific services used in the template. Remember, this is different from the description section, which is a literal string. Since this is an object, we can use key-value pairs to describe the implementation details of a specific resource. -
Parameters
(optional): This section passes values to theResources
section and customizes our template based on environments. Each parameter must have a unique logical name, a type, and a value. -
Rules
(optional): This section validates the previously writtenParameters
section and ensures that the stack creation happens successfully. Each rule must contain aRuleCondition
that determines when it will take effect, andAssertions
describes the list of allowed values for that particular parameter. -
Mappings
(optional): This section is used to feed the values to theParameters
section. We can have many key-value pairs that can be used, like a lookup table. AWS CloudFormation provides intrinsic functions to fetch values from this section. -
Conditions
(optional): Imagine a scenario where we want certain services to be created only in specific environments. For example, we must not install testing packages in the production environment. For cases like these, we can use this section to specify the criteria under which a particular service must be provisioned. -
Transform
(optional): This section identifies one or more macros that CloudFormation uses to process the template. We need to pay attention to the order of the listed macros as they are executed in the listed order within the template. -
Resources
(required): This section specifies the services and their properties that need to be provisioned. It contains the following fields:-
Logical ID
: This is an alphanumeric field used to identify a resource within a template uniquely. This ID references this resource in other parts of the template. -
Type
: This field identifies the type of resource that needs to be provisioned. For example, to provision an S3 bucket, the resource type will beAWS::S3::Bucket
. -
Properties
: This field identifies other options we can specify for our service. For instance, we can useBucketName
,AccessControl
,DeletionPolicy
, and many more as properties while defining an S3 bucket.
-
Get hands-on with 1400+ tech skills courses.