Template Structure
A CloudFormation template is composed of several major sections. While only Resources
is required, a typical template is organized as follows:
-
AWSTemplateFormatVersion
(Optional)- Specifies the template format version. The only valid value is
2010-09-09
.
- Specifies the template format version. The only valid value is
-
Description
(Optional)- A text string that describes the template.
-
Metadata
(Optional)- JSON or YAML objects that provide additional information about the template.
-
Parameters
(Optional)- Allows you to input custom values to your template each time you create or update a stack. This makes templates reusable.
-
Mappings
(Optional)- A mapping of keys and associated values that you can use to specify conditional parameter values, similar to a lookup table (e.g., mapping an AWS Region to a specific AMI ID).
-
Conditions
(Optional)- Define conditions that control whether certain resources are created or whether certain resource properties are assigned a value during stack creation or update.
-
Transform
(Optional)- Specifies one or more macros that AWS CloudFormation uses to process your template. The most common is
AWS::Serverless
for SAM templates.
- Specifies one or more macros that AWS CloudFormation uses to process your template. The most common is
-
Resources
(Required)- The core of the template. This section specifies the stack resources and their properties, such as an EC2 instance or an S3 bucket.
-
Outputs
(Optional)- Describes the output values that you can import into other stacks, view on the AWS CloudFormation console, or see in response to
describe-stacks
API calls.
- Describes the output values that you can import into other stacks, view on the AWS CloudFormation console, or see in response to
Important Features
Drift Detection
-
Drift detection enables you to detect whether a stack's actual configuration has "drifted" from its expected configuration as defined in the template.
-
A stack is considered to have drifted if one or more of its resources have been changed manually outside of CloudFormation.
Cross-Stack References
-
You can export output values from one stack to be used in another stack.
-
The other stacks can then import these values using the
Fn::ImportValue
intrinsic function. This is useful for creating loosely coupled, modular architectures (e.g., a networking stack that exports a VPC ID for application stacks to use).
Resource Import
- Allows you to bring existing, manually-created AWS resources under CloudFormation management without having to delete and recreate them.
Deletion Policies
-
You can add a
DeletionPolicy
attribute to any resource.-
Delete
(Default): CloudFormation deletes the resource when the stack is deleted. -
Retain
: CloudFormation leaves the resource intact when the stack is deleted. -
Snapshot
: For resources that support it (like EBS Volumes and RDS Databases), CloudFormation creates a snapshot before deleting the resource.
-
Custom Resources
- Enable you to write custom provisioning logic in your template. CloudFormation invokes a Lambda function or SNS topic to handle the creation, update, or deletion of these resources.
CloudFormation Designer
- A graphic tool for creating, viewing, and modifying CloudFormation templates with a drag-and-drop interface.