Handbooks / AWS / Chapter 10
CloudFormation, CDK and Cost Basics
7 pages · ~5 min✓ Reviewed
Builds on Monitoring with CloudWatch and CloudTrail.
AWSPage 1CloudFormation: Templates and Stacks Mental Model
1. Template vs Stack
- Template (JSON/YAML) declares resources
- Stack = one deployed instance of a template
- Creates, updates, deletes resources as a unit
3. Declarative Ordering
- State the desired end state, not the steps
- Order comes from Ref/GetAtt dependencies
DependsOn only for hidden dependencies
yamlResources:
Bucket:
Type: AWS::S3::Bucket
Outputs:
BucketArn:
Value: !GetAtt Bucket.Arn minimal template: one bucket, one output
RememberResources is the only required section. Don't add DependsOn when a !Ref already links two resources.
AWSPage 2CloudFormation: Change Sets, Drift and Rollback
1. Change Sets
- Change set = preview of an update: what it will add, modify, remove
- Each change carries a Replacement flag
- Review first, then
execute-change-set
3. Drift and Rollback
- Drift detection compares live resources with the template: IN_SYNC or DRIFTED
- It only detects, it never fixes
- Failed create → ROLLBACK_COMPLETE: delete only, no update
- Failed update → rolls back to the last good state
4. Protect Data
AWSPage 3CDK: Constructs, Synth and Deploy
1. Constructs and Levels
- CDK defines infrastructure in code (TypeScript, Python and others)
- Tree: App contains Stacks, which contain Constructs
cdk synth turns the code into a CloudFormation template
- CodeApp, Stack
- synthtemplate in cdk.out
- deployCloudFormation stack
2. Commands and Bootstrap
AWSPage 4AWS Pricing Models and Savings Options
- Compute Savings Plans: apply across EC2, Fargate and Lambda
- EC2 Instance Savings Plans: fix family and region, deeper discount
- Reserved Instances: Savings Plans are the more flexible modern default
2. Spot
- Spare AWS capacity, up to 90% off
- AWS can reclaim it with a 2-minute warning
- Use only for stateless, fault-tolerant or batch work
3. Pick by Workload
AWSPage 5Cost Explorer, Budgets and Cost Traps
1. Seeing and Capping Spend
- Cost Explorer: past spend and forecast, by service, account, region or tag
- Cost allocation tags must be activated in Billing first
- Budget actions can apply IAM or SCP restrictions
2. NAT Gateway Trap
- Hourly charge plus per-GB processing on all traffic
- Fix: S3 and DynamoDB gateway endpoints (free)
- Other services: interface endpoints
- Keep traffic in the same AZ
- Private subnet
- Gateway endpointfree
- S3 / DynamoDB
AWSPage 6AWS Cost and IaC Cheat Sheet
1. CloudFormation Flow
- 1Write template
- 2create-change-setaws cloudformation
- 3Reviewcheck Replacement
- 4execute-change-set
- 5detect-stack-driftdetect only
2. CDK Flow
- 1cdk bootstraponce per account/region
- 2cdk synth
- 3cdk diff
- 4cdk deploy
- L1
Cfn*: raw resource - L2: curated defaults
- L3: patterns of many resources
The first create of a stack failed, leaving it in ROLLBACK_COMPLETE. You fix the template and run an update. What happens?
- The update is rejected: this state allows delete only.
- Delete the stack, then create it again.
You rename a Bucket construct ID in CDK and deploy. What happens to the bucket?
- The new ID gives a new logical ID, so CDK creates a new bucket.
- The old bucket is deleted only if its removal policy is DESTROY. The L2 default is RETAIN, so it stays behind as an orphan.
- Run
cdk diff first to see the change.
Private-subnet jobs read terabytes from S3 through a NAT Gateway, and the bill is high. What is the fix?
- NAT charges hourly plus per GB processed.
- Add a free S3 gateway endpoint so that traffic skips the NAT.