AWS CloudFormation & CDK IaC
Define infrastructure as code — CloudFormation YAML/JSON or CDK TypeScript, deploy reproducibly.
Why
IaC makes infrastructure version-controlled, reviewable, and reproducible. CDK lets you write TS/Python that compiles to CloudFormation — 10x less code than YAML.
Drift detection catches manual changes.
When
Use IaC for all production infrastructure — no console clicks. CDK for complex stacks, CloudFormation for simple/sam.
Avoid for one-off resources (CLI is fine).
How
Example
Common Problems
- ⚠Destructive changes not caught — cdk deploy shows REPLACE/DELETE, read carefully
- ⚠Stack stuck in UPDATE_ROLLBACK — manual fix or continue-rollback, understand cause first
- ⚠CDK v1 vs v2 — v2 is one package (aws-cdk-lib), v1 is deprecated, migrate
Best Practices
- ✓Use cdk diff in CI — block PRs with unexpected destructive changes
- ✓Separate stacks by lifecycle — stateful (DB) vs stateless (Lambda) for safe redeploy
- ✓Use cdk-nag for security — auto-checks against AWS best practices, fails CI on violations
FAQ
What is AWS CloudFormation & CDK IaC and what problem does it solve?
Define infrastructure as code — CloudFormation YAML/JSON or CDK TypeScript, deploy reproducibly. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.
How do I get started with AWS CloudFormation & CDK IaC in 5 minutes?
Two steps to run it: (1) CDK init — npx cdk init app --language typescript; (2) Define stack — construct library (aws-s3, aws-lambda, etc.). Validate on the free tier first, then wire into production.
What is the #1 pitfall with AWS CloudFormation & CDK IaC?
The most frequent issue is: Destructive changes not caught — cdk deploy shows REPLACE/DELETE, read carefully. Run a pre-launch checklist item by item, and add policy/config scanning in CI.
What are the best practices for AWS CloudFormation & CDK IaC?
Best practices for AWS CloudFormation & CDK IaC include: Use cdk diff in CI — block PRs with unexpected destructive changes; Separate stacks by lifecycle — stateful (DB) vs stateless (Lambda) for safe redeploy; Use cdk-nag for security — auto-checks against AWS best practices, fails CI on violations. The first one — "Use cdk diff in CI — block PRs with unexpected destructive changes" — should be a team default baked into your IaC templates.
How do I control cost and avoid bill surprises with AWS CloudFormation & CDK IaC?
Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most AWS CloudFormation & CDK IaC options offer a free tier — validate within free quota before scaling.