The idea
Infrastructure as code (IaC) means describing servers, networks and other cloud parts in text files. A tool reads the file and makes the real cloud match it. The file lives in version control, like any other code.
Without IaC, people build servers by clicking in the cloud's web console. Nothing records exactly what they did, so servers slowly drift apart. Hand-made servers that are each a little different are called snowflakes.
Plan
An earlier version of the file built web-1 (tiny) and old-1, so the tool already manages both. Now you edit the desired state: web-1 small, old-1 removed, web-2 added. This is declarative: you say what should exist, not how.
The tool checks the servers it manages, listed in its state, against the cloud provider: web-1 is tiny, old-1 is small, web-2 does not exist yet. This is the actual state. Servers made outside the tool stay invisible to it.
The tool compares wanted with actual and prints a plan: update web-1 (~), create web-2 (+), destroy old-1 (-). Nothing has changed yet. A person reads the plan before anything happens.
Apply
Now apply carries out the plan, one action at a time. First it resizes web-1 from tiny to small. The server keeps its name; only its size changes, so this is an update in place.
web-2 is in the file but not in the cloud, so apply creates it, small, as written. Nobody had to remember the size or click through a form: the file is the record.
old-1 is in the tool's state but no longer in the file, so apply destroys it. The cloud now matches the file exactly. Be careful: deleting a line from the file deletes the real server too.
Living with it
Run plan again and the tool finds no differences: "No changes. Your infrastructure matches the configuration." Running it twice gives the same result as running it once. That property is called idempotence.
Someone resizes web-1 to large by hand in the console. The cloud no longer matches the file: that is drift. The next plan shows ~ web-1 large → small, and apply puts it back.
Because the file holds the whole design, the same tool can build an identical copy from nothing: a test copy, or a fresh start after a disaster. Changes go through review in a pull request, like any code.
What happens
;; step 1/11 · iac
file (what you want) → tool → cloud (what exists)
kept in version control (Git)
reviewed, repeatable, rebuildable
tools: Terraform, OpenTofu, Pulumi,
CloudFormation, kubectl apply
The Python program is a toy that shows the idea; its have dict stands for the servers the tool manages, as they really are. Real tools (Terraform, OpenTofu, Pulumi, AWS CloudFormation) call the cloud provider's API to read and change real resources, handle dependencies between them, and save a state file. In Terraform the same wish is written in HCL, for example resource "aws_instance" "web_1" { instance_type = "t3.small" ... }. terraform plan prints the actions with + (create), ~ (update in place) and - (destroy), then a summary such as "Plan: 1 to add, 1 to change, 1 to destroy."