Deploying CloudFormation Safely at Scale
This lesson explains the CloudFormation features that protect production resources and deploy infrastructure consistently across many accounts and Regions.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how change sets, stack policies and deletion policies make changes safe.
- Describe how StackSets deploy to many accounts and Regions.
- Explain how Guard, Hooks and drift detection enforce standards.
Making changes safe
Several CloudFormation features reduce the risk of changing production stacks. A change set previews exactly what an update will do. Most importantly, it shows replacements, where changing a property, such as a database identifier, deletes and recreates the resource. A stack policy denies updates to, or replacement of, critical resources unless the operator explicitly overrides it. Rollback triggers watch CloudWatch alarms during an update and for a period afterwards, and roll the stack back if an alarm fires.
The DeletionPolicy and UpdateReplacePolicy attributes retain a resource, or take a snapshot of it, when it is deleted or replaced, so data survives a mistake. Termination protection stops a whole stack from being deleted, and drift detection finds resources that were changed outside CloudFormation. The following table summarises the features.
| Feature | What it does |
|---|---|
| Change sets | Preview an update, including replacements such as a changed database identifier |
| Stack policies | Deny updates to or replacement of critical resources unless overridden |
| Rollback triggers | Watch CloudWatch alarms during and after an update and roll back if they fire |
| DeletionPolicy / UpdateReplacePolicy | Retain or snapshot a resource on deletion or replacement |
| Termination protection | Stops a stack being deleted |
| Drift detection | Finds resources changed outside CloudFormation |
Deploying at scale
StackSets deploy one template to many accounts and Regions. With service-managed permissions, they use AWS Organizations to target OUs, and automatic deployment adds stack instances to accounts that join a target OU, so baselines reach new accounts without anyone acting. Concurrency and failure tolerance settings control how fast a change rolls out and how many failures stop it, which limits the damage a faulty template can do.
Within a stack, nested stacks and registry modules package reusable components, such as a standard VPC or database, and cross-stack references or Parameter Store share values between independently managed stacks. The AWS CDK defines infrastructure in general-purpose programming languages, synthesises CloudFormation templates, and generates least-privilege IAM policies through its grant methods, which suits teams that build and share a lot of infrastructure.
Governance
Three tools enforce standards on infrastructure defined in CloudFormation, at different points. CloudFormation Guard checks templates against policy-as-code rules in the pipeline, before deployment. CloudFormation Hooks run inside CloudFormation and check each resource before it is provisioned, such as blocking an unencrypted bucket. Because they run inside the service, they apply even when someone deploys outside the pipeline. Service Catalog takes a different approach: it offers governed, versioned products that users can launch without having broad permissions to create the underlying resources. The following table summarises the tools.
| Tool | Checks |
|---|---|
| CloudFormation Hooks | Resources before provisioning, e.g. block unencrypted buckets |
| CloudFormation Guard | Templates against policy-as-code rules in pipelines |
| Service Catalog | Offers governed, versioned products without broad permissions |
Protecting a production database during stack updates
A stack contains an Aurora cluster that holds years of customer data. A developer once changed a property that forced replacement, and the update deleted the old cluster. The architect adds three layers of protection. A stack policy refuses any update that would replace or delete the cluster:
{
"Statement": [
{ "Effect": "Allow", "Principal": "*", "Action": "Update:*", "Resource": "*" },
{ "Effect": "Deny", "Principal": "*", "Action": ["Update:Replace", "Update:Delete"],
"Resource": "LogicalResourceId/OrdersCluster" }
]
}The template sets DeletionPolicy: Snapshot and UpdateReplacePolicy: Snapshot on the cluster, so even an approved replacement keeps a final snapshot. Finally, updates are applied through change sets with a rollback trigger that watches the application's error alarm for fifteen minutes:
aws cloudformation create-change-set --stack-name orders --change-set-name rel-42 \
--template-body file://orders.yaml \
--rollback-configuration 'RollbackTriggers=[{Arn=arn:aws:cloudwatch:eu-west-1:111111111111:alarm:orders-5xx,Type=AWS::CloudWatch::Alarm}],MonitoringTimeInMinutes=15'Each control covers a different failure: the stack policy stops the dangerous change, the retention policies preserve data if it happens anyway, and the rollback trigger reverses an update that deploys cleanly but breaks the application.
How does a service-managed StackSet reach accounts that join an OU later?
Show answer
With automatic deployment enabled, it creates stack instances in accounts when they join the target OU, using roles that Organizations manages.
Exam Tips
Remember for the exam
- To deploy the same baseline to every account and Region, use StackSets.
- To prevent accidental replacement of a production database, combine a stack policy with DeletionPolicy and UpdateReplacePolicy.
- To validate resources against rules before they are created, use CloudFormation Hooks.
- To roll back an update automatically if alarms fire, use rollback triggers.
- To find manual changes to stack resources, use drift detection.