Infrastructure as code
Describe your servers, networks and databases in files you can review, version and replay.
Infrastructure as code (IaC) manages servers, networks and cloud resources through version-controlled configuration files instead of manual clicks. Declarative tools like Terraform let you write the desired end state, preview the changes with plan, and apply them idempotently. A state file records what actually exists, so the tool can compute diffs and detect drift when reality wanders from the code.
Declarative code in Git
You describe the infrastructure you want — three servers, a load balancer, a database — in configuration files kept under version control. Declarative style states the desired end result and lets the tool figure out the steps, whereas imperative scripting spells out each command. Because the code is the source of truth, anyone can review a change in a pull request and rebuild the whole environment from scratch.
The tools
Terraform and Pulumi are cloud-agnostic provisioners (Pulumi lets you write in real languages like TypeScript or Python), CloudFormation is AWS's native service, and Ansible specializes in configuring and provisioning machines. Provisioning tools create and destroy cloud resources; configuration tools install packages and set files on existing hosts. Most teams combine them, using one to build the servers and another to configure what runs on them.
Idempotency
A well-behaved IaC run is idempotent: applying the same configuration once or ten times converges to the same end state without creating duplicates. The tool checks what already exists and only acts on the gap, so re-running is always safe. This is what lets you treat the config as a fixed target you can re-apply confidently rather than a script that does damage if run twice.
plan then apply
Before changing anything, plan performs a dry run that compares your code to the current state and prints exactly what will be added, changed or destroyed. You read that diff, confirm it matches intent, and only then run apply to execute it. This preview step is the safety rail that keeps a one-line edit from silently deleting a production database.
The state file
To compute those diffs the tool keeps a state file — a record of every resource it manages and its real-world identifiers. On each run it reconciles desired state (your code) against recorded state to produce the minimal change. Because state is sensitive and shared, teams store it remotely with locking so two engineers can't apply at once and corrupt it.
Detecting drift
Drift happens when someone changes a resource outside the code — a hotfix clicked into the console at 3am — so reality no longer matches the state file. The next plan surfaces that gap, and apply either reverts the manual change or you fold it back into code. Continuously detecting and closing drift is what keeps the code an honest, reproducible description of production.
The problem: infrastructure built by hand doesn't scale
For years, standing up infrastructure meant a human logging into a console or SSHing into a box and running commands: create a server, attach a disk, open a firewall port, install a runtime. This works for one machine and falls apart for a hundred. Manual setup is slow, impossible to review, and — most damagingly — irreproducible. Two engineers following the "same" wiki page produce subtly different servers, and six months later nobody can say exactly how production was configured or why.
This is the phenomenon of snowflake servers: unique, hand-tuned machines that no one dares touch because rebuilding them is a mystery. When a snowflake dies, so does the knowledge of how it was made.
Infrastructure as code solves this by treating infrastructure the same way we treat application code. You describe your servers, networks, load balancers and databases in text files, commit those files to Git, review changes in pull requests, and let a tool build the real resources to match. The infrastructure becomes reproducible (rebuild it identically on demand), versioned (every change has an author, a diff and a history), and auditable (the files are the single source of truth). A whole environment can be created, cloned for staging, or torn down with one command.
Declarative vs imperative
There are two fundamentally different ways to express infrastructure, and the distinction shapes everything.
Imperative style spells out the steps: "create a server, then attach this disk, then open port 443." It's a script — a sequence of commands. The catch is that you must handle every case yourself: what if the server already exists? What if it exists but the port is closed? Re-running an imperative script blindly can fail or duplicate resources.
Declarative style describes the desired *end state*: "there should be one server with this disk and port 443 open." You don't say how to get there — the tool inspects what currently exists and computes the necessary actions to reach your description. If everything already matches, it does nothing. This is the model of Terraform, CloudFormation and Pulumi, and it's why declarative IaC dominates: you maintain a picture of the world you want, not a fragile recipe of steps.
The declarative approach depends on idempotency — the property that applying the same configuration any number of times converges to the same result. Because the tool acts only on the gap between desired and actual state, re-running is inherently safe. That safety is what lets you re-apply confidently after a failure, on a schedule, or in a pipeline.
The tool landscape
The ecosystem splits roughly into two jobs.
- Provisioning — creating and destroying cloud resources (servers, networks, databases, DNS).
Terraformis the cloud-agnostic standard, using its own HCL language and a huge provider ecosystem.Pulumidoes the same job but lets you write infrastructure in real programming languages like TypeScript, Python or Go.CloudFormation(and CDK) is AWS's native provisioning service, deeply integrated but AWS-only. - Configuration management — installing packages, editing config files and managing services on machines that already exist.
Ansibleis the popular choice here, agentless and driven by YAML playbooks; Chef and Puppet occupy the same space.
The line blurs — Ansible can provision, and Terraform can run setup scripts — but the mental model holds: provisioners build the boxes, configuration tools decide what runs inside them. Real teams commonly pair them, for example Terraform to create a fleet and Ansible to configure it, or increasingly bake configuration into container images so the provisioner just runs them.
State, plan and apply
The engine that makes declarative IaC work is the state file. To compute the difference between what you want and what exists, the tool needs a record of the resources it already manages and their real-world IDs. That record is the state. On every run it reconciles three things: your code (desired state), the state file (last known state), and optionally the live cloud (actual state).
From that reconciliation comes the two-phase workflow that defines the discipline:
- plan is a dry run. It compares desired to current and prints exactly what will be created, modified or destroyed — a diff you read before touching anything. Seeing "1 to add, 0 to change, 1 to destroy" is the moment you catch a mistake before it happens.
- apply executes that plan, making the real changes and updating the state file to match.
Because state is both sensitive (it can contain secrets) and shared, teams store it in a remote backend with locking, so two engineers can't apply simultaneously and corrupt it. Treating state carelessly — losing it, editing it by hand, committing it to Git — is one of the most common ways IaC setups go wrong.
Drift, and using IaC in practice
The promise of IaC is that the code is an honest description of production. The threat to that promise is drift: the gap that opens when someone changes a resource outside the code — a manual hotfix in the console, an auto-scaling event, a colleague's quick fix at 3am. Now reality no longer matches the state file, and the next plan will reveal the discrepancy. You then decide: revert the change to match code, or import the change back into code. Continuously detecting and closing drift — ideally by forbidding manual changes entirely — is what keeps the whole system trustworthy.
In practice, mature IaC looks like this: infrastructure code lives in Git next to (or beside) the application; every change goes through a pull request where a teammate reads the plan output; a CI/CD pipeline runs plan on the PR and apply on merge; state is stored remotely with locking; secrets are injected from a vault rather than hard-coded; and modules are reused so a "standard service" is defined once and stamped out many times.
The payoff is enormous: environments become disposable and identical, disaster recovery becomes "run apply again," and the terrifying question "how is production actually configured?" has a boring answer — read the repository. The cost is discipline. IaC only delivers if the team commits to making *every* change through the code, because the moment people start clicking in the console again, drift returns and the snowflakes grow back.
Declarative IaC describes the desired end state; imperative describes the step-by-step commands to get there.
Terraform, Pulumi and CloudFormation provision cloud resources; Ansible focuses on configuring machines.
Idempotency means applying the same config repeatedly converges to the same result, never duplicating resources.
A state file records what the tool believes exists, so it can diff desired vs actual and plan a minimal change.
plan previews the changes before apply executes them — a dry run that prevents surprises.
Config lives in Git, making infrastructure reproducible, reviewable and auditable; drift is when reality diverges from the code.
Quick quiz
What is the difference between declarative and imperative infrastructure code?
Why does a Terraform-style tool keep a state file?
CI/CD pipeline explained
Turn every commit into a tested, shippable, deployable artifact — automatically.
How Kubernetes works
You declare the desired state; the cluster relentlessly makes it real.
Git branching workflows
The commit-to-merge loop every team runs — and the branching models that shape it.
Docker best practices
The handful of habits that turn a bloated, insecure image into a small, fast, safe one.