Infrastructure as code
Infrastructure as code (IaC) means defining servers, networks, databases, permissions and cloud services in files that live in version control, and creating or changing them only by applying those files with a tool. The code is the source of truth: reviewed like application code, applied by a pipeline, and repeatable in every environment.
It replaces clicking in consoles and running one-off scripts, which leave environments that nobody can reproduce, audit or safely change.
Diagram
Section titled “Diagram”- A change is made in code and proposed as a pull request.
- Automated policy checks run, and the plan of exactly what will change is shown to reviewers.
- After approval, the pipeline applies the change; people don't apply it from their laptops.
- Drift detection compares the code with what actually exists and flags manual changes.
Approaches and tools
Section titled “Approaches and tools”- Declarative tools describe the desired end state, and the tool works out the changes: Terraform (opens in a new tab) and OpenTofu (opens in a new tab), AWS CloudFormation, Azure Bicep, and Kubernetes manifests.
- General-purpose languages such as Pulumi (opens in a new tab) or the AWS CDK generate the declarative definitions from TypeScript, Python or other languages, with loops, functions and tests.
- Configuration management (Ansible, Chef, Puppet) configures software on servers; less needed where images are built immutably.
- GitOps keeps the desired state in Git and has an agent in the platform (for example Argo CD (opens in a new tab) or Flux (opens in a new tab)) continuously reconcile reality to it.
Practices that matter
Section titled “Practices that matter”- Everything through the pipeline. Console changes in production are emergencies only, followed by updating the code. Restrict write permissions so this is enforced, not hoped for.
- Review the plan, not just the code. The plan shows what will actually be created, changed or destroyed. Block plans that delete stateful resources unless explicitly approved.
- Protect state. Tools such as Terraform keep a state file mapping code to real resources. Store it remotely, encrypted, locked during changes and backed up; it can contain secrets.
- Modules for reuse. Platform teams publish tested, versioned modules (a standard database, a standard service account) that embed security and tagging standards. Product teams compose them. See platform engineering.
- Small blast radius. Split infrastructure into separate stacks or workspaces by environment and component, so one change can’t touch everything.
- Policy as code. Check plans automatically against rules such as “no public storage buckets” or “every resource has an owner tag”, with tools like Open Policy Agent or the cloud’s own policy services.
- Test. Static checks and linting on every change, plans in CI, and integration tests that create and destroy real resources for important modules.
- No secrets in code. Reference a secrets manager.
- Environments are reproducible: test looks like production because both come from the same code.
- Every change is reviewed, auditable and reversible through version control.
- Disaster recovery includes rebuilding infrastructure from code.
- Learning curve and up-front effort, especially for importing existing infrastructure.
- State management and drift need ongoing care.
- A bad change applied automatically can do damage at scale; reviews and safeguards matter.
Common pitfalls
Section titled “Common pitfalls”- Manual changes “just this once” that the next apply silently reverts, or that cause drift nobody notices.
- One giant stack for an entire organization, which nobody dares apply.
- Copy-pasted code across teams instead of shared, versioned modules.
- Apply from laptops with personal admin credentials.
Related patterns
Section titled “Related patterns”- Landing zones, usually defined in IaC
- CI/CD architecture
- Kubernetes
- Production readiness checklist
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.