You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Every CloudFormation template follows a well-defined structure. Understanding the anatomy of a template is essential for writing, reading, and debugging infrastructure definitions. In this lesson we will examine each section of a template, what it does, and when to use it.
A CloudFormation YAML template can contain the following top-level sections:
AWSTemplateFormatVersion: '2010-09-09'
Description: A description of what this template does
Metadata:
# Optional information about the template
Parameters:
# Input values supplied at stack creation time
Mappings:
# Static key-value lookup tables
Conditions:
# Logic to control whether resources are created
Resources:
# The AWS resources to create (REQUIRED)
Outputs:
# Values to return after the stack is created
Only the Resources section is required. All other sections are optional, but most real-world templates use several of them.
This declares the template format version. The only valid value is '2010-09-09', which has been the version since CloudFormation launched. You should always include it for clarity:
AWSTemplateFormatVersion: '2010-09-09'
A human-readable string that describes the purpose of the template. It is displayed in the CloudFormation console when you view a stack:
Description: >
This template provisions a three-tier web application
with an Application Load Balancer, EC2 instances,
and an RDS PostgreSQL database.
Descriptions have a maximum length of 1,024 bytes. Keep them concise but informative.
The Metadata section provides additional information about the template. It is most commonly used to:
cfn-init helperMetadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Network Configuration
Parameters:
- VpcCidr
- SubnetCidr
- Label:
default: Instance Configuration
Parameters:
- InstanceType
- KeyPairName
This tells the CloudFormation console to group the parameters under meaningful headings instead of listing them alphabetically.
Parameters let you pass values into a template at stack creation time. This makes your templates reusable — the same template can create different environments by supplying different parameter values.
Parameters:
EnvironmentType:
Type: String
Default: dev
AllowedValues:
- dev
- staging
- production
Description: The environment type for this stack
InstanceType:
Type: String
Default: t3.micro
AllowedValues:
- t3.micro
- t3.small
- t3.medium
Description: The EC2 instance type
VpcCidr:
Type: String
Default: '10.0.0.0/16'
AllowedPattern: '^(\d{1,3}\.){3}\d{1,3}/\d{1,2}$'
Description: The CIDR block for the VPC
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.