Lesson 1 of 4 Infrastructure Automation

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.

True or false

Templates and stacks

Exam skills 2.2.1

Check how CloudFormation templates and stacks work.

Log in to try this activity Interactive activities are free with an account.

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.

FeaturePurpose
Change setsPreview updates
Drift detectionFind manual changes
Stack policies, DeletionPolicy (Retain, Snapshot)Protect critical resources
Rollback triggersRoll back an update when CloudWatch alarms fire
StackSetsDeploy across accounts and Regions
Nested stacks, exportsReuse 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.

Safe and repeatable CloudFormation changes. A change set previews which resources an update will add, modify or replace before it is executed, catching an update that would replace a database. StackSets deploy one template as stack instances in many accounts and Regions, such as a security baseline.
The accounts and Regions are illustrative. Reviewing the change set is what reveals a replacement before it happens.
Match the pairs

CloudFormation safety

Exam skills 2.2.1

Match each requirement with the CloudFormation feature that meets it.

Log in to try this activity Interactive activities are free with an account.

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.

Worked example

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.

NeedCloudFormation feature
Define the stack onceA template with parameters (such as the environment name)
Deploy to several Regions and accountsStackSets
Preview changes before applying themChange sets
Protect the production databaseDeletionPolicy: Retain or Snapshot
Detect manual changesDrift 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.

Check yourself

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.