Sub topic 2
Lookup Tables, Conditions, Outputs, and Safe Stack Updates
Assume you know basic AWS services, YAML, and the AWS CLI. CloudFormation evaluates a template, creates a stack, and manages the resources declared in that stack.
Mappings and `FindInMap`
A Mappings section is a static lookup table. Use Fn::FindInMap when a known input selects a value, such as a Region-specific AMI ID or an environment-specific retention period.
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
Environment:
Type: String
Default: dev
AllowedValues: [dev, prod]
Mappings:
Storage:
dev:
RetentionDays: 7
prod:
RetentionDays: 90
Conditions:
IsProduction: !Equals [!Ref Environment, prod]
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
LifecycleConfiguration:
Rules:
- Id: ExpireObjects
Status: Enabled
ExpirationInDays: !FindInMap
- Storage
- !Ref Environment
- RetentionDays
VersioningConfiguration:
Status: !If [IsProduction, Enabled, Suspended]
Outputs:
BucketArn:
Description: ARN of the application bucket
Value: !GetAtt AppBucket.ArnThe three FindInMap arguments are the mapping name, top-level key, and second-level key. Mappings are best for stable, finite choices. Use parameters or another service when values change frequently.
Conditions and Outputs
A condition controls whether CloudFormation creates a resource or assigns a property value. !If selects between two values. You can also add Condition: IsProduction to a resource to create it only in production.
An output publishes a value after deployment. !GetAtt reads a resource attribute, while !Ref usually returns a resource's name or identifier. Outputs can also be exported for another stack to import.
Dependencies
CloudFormation detects implicit dependencies when one resource uses !Ref, !GetAtt, or a substitution containing another resource. Use DependsOn when an ordering requirement is not visible in a value reference.
Stack Lifecycle and Update Behavior
Use the AWS CLI to create or update a stack, inspect events, and delete it.
aws cloudformation deploy --template-file template.yaml --stack-name demo
aws cloudformation describe-stack-events --stack-name demo
aws cloudformation delete-stack --stack-name demoA property change can cause no interruption, temporary interruption, or resource replacement. Check the resource documentation before changing production resources. Replacement can delete the old resource unless a retention policy protects it.
| Change result | Meaning |
|---|---|
| No interruption | CloudFormation changes the resource in place |
| Replacement | CloudFormation creates a new resource and removes the old one |
Change Sets
Preview an update with a change set before executing it.
aws cloudformation create-change-set \
--stack-name demo \
--change-set-name preview \
--template-body file://template.yaml
aws cloudformation describe-change-set \
--stack-name demo \
--change-set-name preview
aws cloudformation execute-change-set \
--stack-name demo \
--change-set-name previewExercise
Why might you use DependsOn when a template already contains references between resources?
Show answer
Use it when CloudFormation must enforce an ordering relationship that is not expressed by a value reference.