Components of CloudFormation Template File

Explore the anatomy of the AWS CloudFormation template.

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:

  1. AWSTemplateFormatVersion (optional): This is the AWS CloudFormation template version. The only valid value is 2010-09-09, and this version must be declared as a literal string.

  2. 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 exceed 1024 bytes.

  3. 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.

  4. Parameters (optional): This section passes values to the Resources section and customizes our template based on environments. Each parameter must have a unique logical name, a type, and a value.

  5. Rules (optional): This section validates the previously written Parameters section and ensures that the stack creation happens successfully. Each rule must contain a RuleCondition that determines when it will take effect, and Assertions describes the list of allowed values for that particular parameter.

  6. Mappings (optional): This section is used to feed the values to the Parameters 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.

  7. 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.

  8. 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.

  9. 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 be AWS::S3::Bucket.

    • Properties: This field identifies other options we can specify for our service. For instance, we can use BucketName, AccessControl, DeletionPolicy, and many more as properties while defining an S3 bucket.

Get hands-on with 1200+ tech skills courses.