AWS core services · Lesson 5 of 8 · 3 min read
CloudFormation in depth
Template anatomy
AWSTemplateFormatVersion: "2010-09-09"
Description: Orders service infrastructure
Parameters:
Env: { Type: String, AllowedValues: [dev, staging, prod] }
VpcId: { Type: "AWS::EC2::VPC::Id" }
Mappings:
Sizing:
dev: { InstanceClass: db.t4g.medium }
prod: { InstanceClass: db.r6g.large }
Conditions:
IsProd: !Equals [!Ref Env, prod]
Resources:
DbSecret:
Type: AWS::SecretsManager::Secret
Properties:
GenerateSecretString:
SecretStringTemplate: '{"username":"orders"}'
GenerateStringKey: password
ExcludeCharacters: '"@/\'
Db:
Type: AWS::RDS::DBInstance
DeletionPolicy: Snapshot
UpdateReplacePolicy: Snapshot
Properties:
Engine: postgres
DBInstanceClass: !FindInMap [Sizing, !Ref Env, InstanceClass]
MultiAZ: !If [IsProd, true, false]
AllocatedStorage: "50"
StorageEncrypted: true
MasterUsername: !Sub "{{resolve:secretsmanager:${DbSecret}:SecretString:username}}"
MasterUserPassword: !Sub "{{resolve:secretsmanager:${DbSecret}:SecretString:password}}"
Outputs:
DbEndpoint:
Value: !GetAtt Db.Endpoint.Address
Export: { Name: !Sub "${Env}-orders-db-endpoint" }
- Intrinsic functions:
!Ref,!GetAtt,!Sub,!Join,!Select,!Split,!FindInMap,!If,!Equals,!ImportValue,!GetAZs,!Base64,!Cidr. - Pseudo parameters:
AWS::Region,AWS::AccountId,AWS::StackName,AWS::Partition,AWS::NoValue(remove a property conditionally). - Dynamic references:
{{resolve:ssm:/path}},{{resolve:secretsmanager:...}}— secrets never in templates or parameters.
Change management
- Change sets — preview what will be added, modified or replaced before executing. Always review "Replacement: True" (e.g. changing an RDS identifier or some subnet properties recreates the resource).
- DeletionPolicy (
Retain,Snapshot,Delete) and UpdateReplacePolicy protect data on delete/replacement. Stack termination protection and stack policies (deny updates to critical resources). - Rollback: failed creates/updates roll back automatically;
UPDATE_ROLLBACK_FAILEDneedscontinue-update-rollback(optionally skipping resources) after fixing the cause. Rollback triggers can watch CloudWatch alarms. - Drift detection — find manual changes; resolve by updating the template or reverting the resource.
- Resource import brings existing resources under a stack.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
Composition and scale
| Feature | Use |
|---|---|
| Nested stacks | Reuse templates as components (AWS::CloudFormation::Stack); deployed together |
| Exports/ImportValue | Share outputs between stacks — but an exported value can't change or be deleted while imported (tight coupling); SSM parameters are a looser alternative |
| StackSets | Deploy the same stack to many accounts/regions (with Organizations: auto-deploy to new accounts) — guardrails, baseline roles, Config rules |
| Modules / macros / transforms | AWS::Serverless (SAM), AWS::LanguageExtensions (Fn::ForEach), custom macros |
| Custom resources | Lambda-backed logic for things CloudFormation doesn't support |
| CDK | Write infrastructure in TypeScript/Python that synthesises CloudFormation; constructs L1/L2/L3 |
Quality in CI: cfn-lint, cfn-guard (policy-as-code), Checkov; deploy via change sets with manual approval for prod; aws cloudformation deploy --no-execute-changeset to create the change set for review.
CloudFormation vs Terraform in one breath: CloudFormation is AWS-native, state is managed by AWS, deep integration (StackSets, rollback, drift); Terraform is multi-cloud/multi-provider with a huge ecosystem, explicit state and plan, modules and a richer language. Many companies use Terraform for AWS too — the Terraform topic covers it in depth.