Provisioning Repeatable Infrastructure with AWS CloudFormation
This lesson explains how CloudFormation provisions AWS resources from templates, making infrastructure repeatable and recoverable.
Learning outcomes
After completing this lesson, you will be able to:
- Describe templates, stacks and their sections.
- Explain how change sets and drift detection make changes safer.
- Describe how StackSets deploy across Regions and accounts.
Templates and stacks
AWS CloudFormation provisions infrastructure from templates written in YAML or JSON. A template declares Resources, the only required section, plus optional Parameters for inputs, Mappings for lookup tables, Conditions for optional resources and Outputs for values to export.
CloudFormation creates the resources as a stack, in the order their dependencies require. If any resource fails, it rolls the whole stack back, and deleting the stack deletes everything in it. StackSets deploy the same template as stacks in many accounts and Regions from one place.
Templates and stacks
Safety and reuse
CloudFormation provides features that make changes safer. A change set previews exactly which resources an update will add, modify or replace before anything happens, which catches an update that would replace a database. Drift detection finds resources changed manually outside CloudFormation. Stack policies deny updates to critical resources, and DeletionPolicy set to Retain or Snapshot keeps data when a resource is removed. Rollback triggers watch CloudWatch alarms during an update and roll it back if one fires.
Other features support reuse. StackSets deploy one template to many accounts and Regions, such as a baseline of security resources. Nested stacks reuse common components, such as a standard network, inside other templates, and exports share output values between stacks. The following table summarises the features.
| Feature | Purpose |
|---|---|
| Change sets | Preview updates |
| Drift detection | Find manual changes |
| Stack policies, DeletionPolicy (Retain, Snapshot) | Protect critical resources |
| Rollback triggers | Roll back an update when CloudWatch alarms fire |
| StackSets | Deploy across accounts and Regions |
| Nested stacks, exports | Reuse components |
On failure, stack events show the failing resource and the reason.
The following diagram shows a change set before an update, and a StackSet deploying to several accounts and Regions.

CloudFormation safety
Uses
Infrastructure as code supports several common requirements. It creates identical development, test and production environments, so that what was tested is what runs in production. It rebuilds an environment in another Region for disaster recovery, and it supports immutable infrastructure, where changes are made by deploying new resources rather than modifying running ones.
AWS Service Catalog builds on CloudFormation by publishing approved templates as products that users can launch themselves, without needing permissions on the underlying services, which gives self-service with governance.
The same environment in three Regions
A company must deploy an identical application stack in three Regions for latency and DR, and keep them identical over time.
| Need | CloudFormation feature |
|---|---|
| Define the stack once | A template with parameters (such as the environment name) |
| Deploy to several Regions and accounts | StackSets |
| Preview changes before applying them | Change sets |
| Protect the production database | DeletionPolicy: Retain or Snapshot |
| Detect manual changes | Drift detection |
If the DR Region were ever needed, the same template could recreate the whole environment in minutes, which is what makes backup-and-restore and pilot-light DR strategies practical.
How can a stack keep its database when the stack is deleted?
Show answer
Set DeletionPolicy to Retain or Snapshot on the database resource.
Exam Tips
Remember for the exam
- To recreate infrastructure quickly in another Region, use CloudFormation templates.
- To deploy the same baseline to many accounts and Regions, use StackSets.
- To keep a database snapshot when the stack is deleted, use a DeletionPolicy of Snapshot.
- To let users launch approved stacks without broad permissions, use Service Catalog.
- To roll back an update automatically if errors rise, use CloudFormation rollback triggers.