Template Metadata and Description
Every CloudFormation template begins with the template format version. In this example, we use “2010-09-09”, a long-established standard. The Description field provides a brief summary of the template’s purpose, while the Metadata section supports additional information used by processes and applications—such as organizing parameter groups and labels.The Metadata section is particularly useful for customizing the presentation of parameters when users launch the stack.
Parameters
The Parameters section defines the user inputs needed during stack creation. This section allows you to prompt users for essential information such as the environment type (e.g., “dev” or “prod”) or network identifiers such as VPC and Subnet IDs.Mappings and Conditions
Mappings allow you to create key-value associations, such as linking AWS regions to their respective AMI IDs. This is useful because AMIs are region-specific. In the template, theFn::FindInMap function dynamically retrieves the correct AMI based on the region where the stack is deployed.
EnvType parameter is set to “prod”.
Utilize Mappings and Conditions together to create flexible templates that adapt to different deployment environments.
Transform Section
The Transform section is optional and is primarily used with the AWS Serverless Application Model (SAM). By declaring a transform, you can simplify the definitions for serverless applications, such as Lambda functions. In this example, we include the SAM transform as shown below.Resources
The Resources section is a mandatory part of any CloudFormation template. It declares all AWS resources to be created by the template. In this example, we define both an EC2 instance and an S3 bucket. Note that the creation of the S3 bucket is conditional, based on whether the environment is set to production as defined by the Conditions section.Ensure that resources conditioned on specific environments are thoroughly tested to prevent unexpected deployment issues.
Outputs
The Outputs section is optional but highly valuable. It allows you to retrieve important information from the created resources—such as the EC2 instance ID or S3 bucket name—after the stack is deployed. Conditions can also be applied here to display outputs only when specific criteria are met.Recap
In this tutorial, we dissected the anatomy of a CloudFormation template and highlighted its key components:- Template Version and Description: Establish the foundational metadata.
- Metadata: Supports custom organization and labeling of parameters.
- Parameters: Prompts users for essential inputs during deployment.
- Mappings: Provides key-value lookups, particularly for region-specific configurations.
- Conditions: Controls resource creation based on deployment criteria.
- Transform: Supports serverless application definitions with AWS SAM integration.
- Resources: Declares all AWS components to be provisioned.
- Outputs: Facilitates retrieval of resource information post-deployment.