Infrastructure as Code
Infrastructure as Code (IaC) is the practice of defining infrastructure (networks, servers, databases, permissions and services) in machine-readable files that are version-controlled, reviewed and applied automatically, rather than configured by hand.
Because every cloud resource can be created through an API, the whole environment can be described as code. That changes infrastructure from something that is built once and carefully maintained into something that can be created, tested, destroyed and recreated at will.
Why it matters
- Repeatability: every environment (development, test, production, a spike) is built from the same definition, so they don’t drift apart.
- Reviewability: infrastructure changes go through the same pull request, review and CI checks as application code. The history of why something changed lives in version control.
- Disposability: environments can be created for an experiment and torn down afterwards, which is what makes cheap architectural spiking possible.
- Recovery: if an environment is lost or compromised, it can be rebuilt from code.
- Security and compliance: policies can be checked automatically before anything is deployed, and the code itself is an accurate record of what should exist.
Approaches and tools
IaC tools fall broadly into two styles:
- Declarative templates describe the desired end state, and the tool works out how to get there. Examples include AWS CloudFormation, Azure Bicep, Terraform and OpenTofu (HCL).
- General-purpose languages let you define infrastructure in TypeScript, Python, C#, Go and so on, with loops, functions and reusable components, and generate the deployment underneath. Examples include AWS CDK and Pulumi.
Provider-native tools integrate most tightly with their cloud. Cross-cloud tools such as Terraform and OpenTofu offer one language across many providers and SaaS services. Pick one per estate and be consistent; the practices matter more than the tool.
Good practice
- Everything in code: if it was created by hand in the console, it doesn’t really exist. Treat console changes as experiments only.
- Deploy through pipelines: changes are applied by CI/CD with a preview (“plan” or “diff”) that is reviewed, not from someone’s laptop.
- Small, composable modules: build reusable modules for common patterns (a secure bucket, a standard network) rather than one giant template.
- Separate state and environments: keep environments isolated so a mistake in development can’t affect production.
- Detect drift: regularly compare what’s deployed with what’s in code, and fix the code rather than the environment.
- Policy as code: check templates automatically for security and cost rules (for example cfn-guard, Checkov or Open Policy Agent) before deployment.
- No secrets in code: reference secrets managers and parameter stores instead.
IaC is a foundation for the principles of immutable infrastructure and “don’t SSH in, build new”.