Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
Infrastructure as code explained: declarative definitions, plan and apply, state, drift and modules, with Terraform examples and common pitfalls.
Quick answer: Infrastructure as code (IaC) means defining your servers, networks, databases and other infrastructure in text files, and letting a tool create and update the real resources to match. Instead of clicking through a cloud console and hoping you remember every setting, you write down the desired result, keep it in version control, review changes like any other code, and apply them automatically. That makes infrastructure repeatable (the same every time), reviewable (changes are visible before they happen), recoverable (you can rebuild from the files) and documented (the files are the documentation).
Building infrastructure by hand in a web console, sometimes called "ClickOps", is fine for learning and for one-off experiments. It breaks down quickly beyond that.
Martin Fowler's note on Infrastructure as Code describes the shift: treat infrastructure with the same practices as software.
Most IaC tools are declarative. You state what should exist, not the steps to create it.
resource "aws_s3_bucket" "assets" {
bucket = "example-app-assets"
}
resource "aws_instance" "web" {
count = 3
ami = "ami-0abcdef1234567890"
instance_type = "t3.small"
tags = {
Name = "web-${count.index}"
Environment = "production"
}
}
This says: one storage bucket and three servers of this type should exist. The tool works out how to get there.
| Imperative | Declarative | |
|---|---|---|
| You write | The steps: "create a server, then another" | The result: "there are three servers" |
| Running it twice | May create duplicates | Changes nothing the second time |
| Handles existing resources | You must check yourself | The tool compares and adjusts |
That second property is idempotence: applying the same definition repeatedly gives the same result. It is what makes automation safe.
Using Terraform as the example; its introduction describes the same cycle of write, plan and apply.
Plan: 1 to add, 1 to change, 0 to destroy.
+ aws_instance.web[2]
~ aws_instance.web[0]
instance_type: "t3.micro" -> "t3.small"
The plan is the most valuable part. You see the consequences before anything happens, including the alarming ones, such as a line saying a database will be destroyed and recreated.
To know what it manages, the tool keeps a state file: a record mapping each resource in your code to the real resource in the cloud.
State needs care:
Drift is when reality no longer matches the code, usually because someone changed something manually. The next plan will show the difference, and applying will revert the manual change. The cure is cultural as much as technical: make all changes through code.
| Tool | Purpose | Style |
|---|---|---|
| Terraform, OpenTofu | Provision cloud resources on any provider | Declarative, its own language (HCL) |
| AWS CloudFormation, Azure Bicep | Provision resources on one cloud | Declarative, YAML/JSON or a dedicated language |
| Pulumi, AWS CDK | Provision resources | Declarative results written in general-purpose languages |
| Ansible, Chef, Puppet | Configure software inside servers | Mostly declarative or procedural |
| Kubernetes manifests, Helm | Define workloads in a cluster | Declarative; see what Kubernetes does |
| Packer, Dockerfiles | Build machine and container images | Scripts |
Two categories are worth separating:
Mutable: servers are long-lived and updated in place. Over time each accumulates its own history of patches and fixes.
Immutable: servers are never changed after creation. To update, you build a new image, start new servers from it, and delete the old ones.
Immutable infrastructure removes drift between servers, makes rollback simple (start the old image again), and fits naturally with containers and with blue-green and canary deployments. The common phrase is to treat servers as cattle, not pets.
GitOps takes this one step further: the Git repository is the single source of truth, and an automated agent continuously makes the running system match it. Nobody runs commands against production directly. A change is a merged pull request, and a rollback is a revert. Tools such as Argo CD and Flux do this for Kubernetes.
Describing your servers, networks and other infrastructure in text files, and using a tool to create and update the real thing to match those files.
Terraform is mainly for provisioning infrastructure resources. Ansible is mainly for configuring the software on servers. They are often used together.
The gap that opens up when the real infrastructure no longer matches its definition, usually because of manual changes.
A record the tool keeps that maps the resources in your code to the actual resources it created, so it knows what to change next time.
Clicking around a console creates infrastructure that exists only in the cloud and in someone's memory. Infrastructure as code puts it in files that can be versioned, reviewed, tested and reapplied. The habit to build is simple and strict: if it is not in code, it does not get changed.