IaC Best Practices
Habits that keep infrastructure code safe, readable, and drift-free.
1 min read
- Everything in code. If it was clicked into existence, it can't be reliably reproduced. Use the console to explore, then codify.
- No manual changes to managed resources. They cause drift. Use drift detection to find resources that no longer match their template.
- Review changes with change sets or
cdk diffin pull requests before deploying. - Separate stacks by lifecycle: e.g. a network stack, a data stack, and an app stack. Data stacks change rarely and deserve extra protection (termination protection and Retain policies).
- Parameterize environments. The same template or CDK app for dev and prod, with different parameters.
- Deploy from a pipeline (CodePipeline, GitHub Actions) rather than from laptops.
- Lint and scan templates with cfn-lint or cdk-nag to catch mistakes and insecure settings early.
Note
Already have resources created by hand? CloudFormation can import existing resources into a stack, and the IaC generator can create a template from resources in your account.
Key takeaways
- Everything in code, no manual changes, and drift detection to catch strays.
- Review diffs in PRs and deploy from pipelines.
- Split stacks by lifecycle and protect stateful ones.
Check your understanding
Pick an answer to see the explanation.
1. Someone changed a security group in the console that CloudFormation manages. What is this called?