AWS Certified SysOps Administrator - Associate
Domain 3 Deployment Provisioning and Automation
Creating and Managing Services With CloudFormation Templates
In this lesson, we dive deep into creating and managing AWS services using CloudFormation templates—an essential part of the Infrastructure as Code (IaC) approach. CloudFormation templates can be written in YAML or JSON, and while tools like the AWS Cloud Development Kit (CDK) can generate these templates, our focus here is solely on CloudFormation and the AWS CDK.
Overview of CloudFormation Templates
CloudFormation templates, when executed, create stacks—a collection of AWS resources defined in the template. By uploading your template file to an S3 bucket or integrating it into your CI/CD pipeline, you can automate the deployment of various AWS services. Note that a standard template deploys a single stack to one region by default. To deploy stacks across multiple accounts or regions, you must use the StackSets feature.
Below is an illustrative diagram summarizing the components and structure of a CloudFormation template. It highlights key features such as the collection of resources, more than 500 resource types, configuration via properties, dependency management, and the ability to author templates in YAML or JSON.
Structure of a CloudFormation Template
A typical CloudFormation template includes several sections:
AWS Template Format Version & Resources (Required)
These are essential to define the template version and the AWS resources to be deployed.Description
Provides an overview of what the template accomplishes.Metadata
Stores supplementary information about your template.Parameters
Allows dynamic input at runtime (e.g., environment type like production or development, or instance size).Mappings
Facilitates key-value lookups, such as mapping region-specific AMI IDs.Conditions
Introduces logic to decide when certain resources should be created. For example, you might launch larger instances only in production.Outputs
Exposes key values such as DNS names or IP addresses for subsequent use or for other stacks.
Note
For example, a template might ask whether the environment is production or development and configure resources based on that input. Parameters and conditions play a vital role in making these decision-based adjustments.
Deployment Process
When a CloudFormation template is deployed, the service performs the following steps:
Defining Resources
Specify which AWS services (e.g., EC2, RDS, networking components) you wish to provision.Deploying the Template
CloudFormation processes the template to create a corresponding stack.Monitoring Stack Events and Outputs
Track the deployment progress, and review outputs such as resource IDs or URLs.
The diagram below visually describes these steps:
Updating and Deleting Stacks
After deployment, you can update or delete resources as needed:
Change Sets:
CloudFormation offers change sets to preview the impact of any modifications to your template. This ensures that unintended changes, such as accidental deletion of critical resources (e.g., databases), are avoided.The following diagram outlines the process of updating a stack using change sets:
Deleting a Stack:
Deleting a stack removes all associated resources. However, be cautious as certain configurations (like S3 bucket retention or delete protection) might block the deletion of some resources. It is advisable to review dependencies and protections before initiating a delete operation.
Warning
Before deleting a stack, ensure that you have accounted for any data retention requirements or dependencies that might prevent resource deletion.
CloudFormation Designer
In addition to using the AWS CLI or console for managing stacks, AWS CloudFormation Designer offers a graphical interface for visualizing your stack's architecture. Although it can sometimes be clunky, this integrated tool supports both JSON and YAML formats, aiding in the creation and modification of CloudFormation templates.
Conclusion
A CloudFormation template is composed of multiple sections—from required format version and resources to optional parameters, mappings, conditions, and outputs. By writing your template in YAML or JSON, CloudFormation orchestrates the creation, updating, and deletion of stacks to manage your AWS resources efficiently.
Stay tuned for the next article, where we'll explore more advanced CloudFormation concepts and best practices.
Happy Building!
Watch Video
Watch video content