Infrastructure as code
Infrastructure as code (IaC) means defining servers, networks, databases, permissions and platform configuration in files that live in version control, and letting tools create and change the real infrastructure to match. Changes go through the same workflow as application code: a pull request, a review, automated checks and a pipeline that applies them.
It’s the foundation for almost everything else in modern operations: landing zones, platform engineering, ephemeral test environments and disaster recovery all depend on being able to rebuild infrastructure from code.
Diagram
Section titled “Diagram”- An engineer changes the infrastructure code and opens a pull request.
- The pipeline shows exactly what will change (the plan), runs tests and checks policy before anyone approves.
- After approval, the pipeline, not a person, applies the change.
- Regular drift detection finds manual changes that bypassed the code, so they can be reverted or codified.
The main approaches
Section titled “The main approaches”| Approach | How it works | Examples |
|---|---|---|
| Declarative provisioning | Describe the desired end state; the tool works out the changes | Terraform, OpenTofu, AWS CloudFormation, Azure Bicep, Pulumi |
| Configuration management | Configure software on servers to a known state | Ansible, Chef, Puppet |
| Immutable images | Build machine or container images once; replace instead of patching | Packer, container images |
| GitOps | A controller continuously makes a cluster match what’s in Git | Argo CD, Flux |
| Policy as code | Rules checked automatically against every change | Open Policy Agent, cloud-native policy services |
How it works
Section titled “How it works”- Version control is the source of truth. If it isn’t in the repository, it shouldn’t exist.
- Plan before apply. Reviewers see the exact diff to real infrastructure, not just the code diff.
- Pipelines apply, people approve. Humans rarely need write access to production infrastructure; break-glass access is logged and reviewed.
- Modules as building blocks. A reviewed, versioned module for “a database with backups, encryption and monitoring” lets teams self-serve safely.
- Separate state per environment and component, stored remotely with locking and encryption, so one change can’t damage everything.
- Policy as code blocks dangerous changes (public storage, unencrypted databases, missing tags) before they happen.
- Treat secrets separately. Reference them from a secrets manager; never commit them.
- Rebuild regularly. Recreating an environment from scratch proves your code is complete, and is the best disaster recovery rehearsal.
- Repeatable environments: test, staging and production built the same way.
- Auditability: every change has an author, a review and a history.
- Recovery: infrastructure can be rebuilt from code after a disaster.
- Speed and safety together: self-service through reviewed modules and automated checks.
- A learning curve for teams used to consoles and tickets.
- State management needs care; a corrupted or shared state file can cause serious damage.
- Slow feedback for very large stacks, unless they’re split sensibly.
When to use it
Section titled “When to use it”For all production infrastructure, and for any environment that needs to be recreated, audited or kept consistent with others.
When to avoid it
Section titled “When to avoid it”Throwaway experiments in a sandbox account can be built by hand. Anything that will last or hold real data should move into code before it does.
Common pitfalls
Section titled “Common pitfalls”- Click-ops fixes during incidents that never make it back into code, so the next apply undoes them.
- One huge stack for everything, where every change risks the whole estate.
- Secrets in the repository.
- Copy-pasted configuration instead of shared modules, so fixes must be made in fifty places.
Related pages
Section titled “Related pages”- SLOs and error budgets
- Incident management
- Landing zones
- Platform engineering
- Zero-downtime schema changes, the database side of the same idea
Further reading
Section titled “Further reading”- Kief Morris, Infrastructure as Code (O’Reilly)
- HashiCorp, Terraform documentation (opens in a new tab)
- OpenGitOps principles (opens in a new tab)
- Open Policy Agent (opens in a new tab)
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.