Volume 9 · Chapter 8 — CI/CD, DevOps and Infrastructure as Code
CloudFormation in Depth
8.1 Introduction
The IaC chapter introduced CloudFormation and the plan/apply idea; the exam demands the template anatomy, intrinsic functions, change sets, nested stacks and StackSets, drift detection, and the policies that control stack behavior. This chapter provides that depth, because CloudFormation mechanics are frequently tested.
8.2 Template Anatomy and Intrinsic Functions
A CloudFormation template (JSON/YAML) has sections:Resources(the only required section - what to create),Parameters(inputs at deploy time, can reference SSM Parameter Store or Secrets Manager),Mappings(lookup tables, e.g. AMI per Region),Conditions(create resources conditionally, e.g. only in prod),Outputs(values to export/share), and Metadata.Intrinsic functionsadd dynamism:!Ref(reference a parameter/resource),!GetAtt(get a resource attribute),!Sub(string substitution),!Join,!FindInMap, and!ImportValue(import another stack’s export). Knowing the sections and these functions is core to reading and writing templates on the exam.
flowchart TB T[CloudFormation template] --> P[Parameters - inputs, SSM/Secrets refs] T --> M[Mappings - Region/AMI lookups] T --> C[Conditions - conditional resources] T --> R[Resources - required] T --> O[Outputs - export values] R -.!Ref / !GetAtt / !Sub / !ImportValue.-> R
8.3 Change Sets, Rollback, and Policies
CloudFormation’s safety mechanisms: achange setpreviews exactly what a stack update will create, modify, or deletebeforeexecuting (the CloudFormation equivalent of a plan) - review it to avoid surprises. On a failed create/update, CloudFormationrolls backautomatically to the last good state.DeletionPolicy(Retain/Snapshot/Delete) controls what happens to a resource when removed (e.g. Retain or Snapshot a database so data isn’t lost), andUpdateReplacePolicysimilar for replacement. Astack policyprotects critical resources from accidental updates.Termination protectionprevents deleting a stack. These make CloudFormation changes reviewable and safe - the exam tests change sets (preview), DeletionPolicy (protect data), and automatic rollback.
8.4 Nested Stacks and StackSets
For scale and reuse,nested stackslet a parent stack include child stacks (reusable components like a standard VPC or database module), keeping templates modular.StackSetsdeploy a stack acrossmultiple accounts and Regionsfrom a single operation - essential for rolling out baseline resources (guardrails, roles, config) organization-wide, integrated with AWS Organizations for automatic deployment to new accounts. The exam distinction: nested stacks for modular reuse within a deployment; StackSets for deploying the same stack across many accounts/Regions (multi-account governance).Custom resources(backed by Lambda) let CloudFormation manage things it doesn’t natively support.
8.5 Drift Detection and Helper Scripts
Drift detectionreports when a stack’s actual resources have been changed outside CloudFormation (manual changes), so you can detect and correct configuration drift - reinforcing that infrastructure changes should go through the template, not the console. For bootstrapping EC2 instances,cfn-initand helper scripts (with theAWS::CloudFormation::Initmetadata) configure instances declaratively, andWaitConditions/CreationPolicysignal when resources are truly ready. The exam associates drift detection with catching out-of-band changes and expects awareness that CloudFormation can bootstrap instances via cfn-init and signal readiness with CreationPolicy.
8.6 Banking Production Scenario
A bank provisions all infrastructure with CloudFormation. Templates use parameters (pulling values from SSM Parameter Store), mappings for per-Region AMIs, and conditions to vary prod vs non-prod. Reusable nested stacks encode standard VPC, database, and security modules. Every change is previewed with a change set in the pipeline before execution, databases carry a DeletionPolicy of Snapshot so data survives stack removal, and stack policies plus termination protection guard critical stacks. StackSets deploy baseline guardrails, roles, and config rules across every account and Region, auto-applying to new accounts via Organizations. Drift detection runs on a schedule to catch manual changes. This modular, reviewed, multi-account, drift-monitored IaC is exactly the professional-level CloudFormation design the certification targets.
8.7 Chapter Revision Notes
- Template sections: Resources (required), Parameters (inputs, SSM/Secrets), Mappings, Conditions, Outputs; intrinsic functions: !Ref, !GetAtt, !Sub, !FindInMap, !ImportValue.
- Change sets preview create/modify/delete before executing; automatic rollback on failure; DeletionPolicy (Retain/Snapshot) protects data; stack policies + termination protection guard resources.
- Nested stacks = modular reuse within a deployment; StackSets = deploy across many accounts/Regions (with Organizations) for governance; custom resources (Lambda) extend coverage.
- Drift detection catches out-of-band manual changes; cfn-init bootstraps instances; CreationPolicy/WaitCondition signal readiness.
- Use change sets and DeletionPolicy for safe changes; StackSets for org-wide baselines.