Why infrastructure as code, and how Terraform works
🎯You'll be able to
Explain what infrastructure as code solves, and run Terraform's basic cycle: init, plan, apply and destroy.
🏠The everyday picture
An architect's drawing describes the finished building. Builders compare the drawing with the site and do whatever is missing. If a wall is built wrongly, they check against the drawing, not against someone's memory. And the same drawing can build an identical second house.
📘What it really is
- Infrastructure as code (IaC) means describing servers, networks, databases and permissions in text files kept in Git. Benefits:
- repeatable: the same files build dev, staging and production the same way;
- reviewable: a change is a diff in a pull request that someone can check;
- recoverable: if an environment is lost, apply the files again;
-
documented: the files say what exists and how it's set up.
-
Terraform (by HashiCorp) and OpenTofu (an open-source fork, CD4) are the most widely used IaC tools. Others include Pulumi (in general-purpose languages), AWS CloudFormation and Azure Bicep (each for one cloud).
-
Terraform is declarative: you describe what should exist, and Terraform works out the steps. You don't write "create a container"; you write "there is a container like this", and Terraform creates, changes or leaves it.
-
Providers are plugins that talk to a platform's API: AWS, Google Cloud, Azure, Cloudflare, GitHub, Kubernetes and hundreds more. RouteRani uses the
kreuzwerker/dockerprovider, which talks to Docker on your computer, so you practise the same workflow at no cost. -
The cycle:
terraform initdownloads providers and prepares the folder;terraform plancompares the files with what exists and shows what it would change;terraform applyshows the plan again and, after you typeyes, makes the changes;terraform destroyremoves everything this configuration created.
📝A worked example
Input: in the starter kit, cd infra, terraform init, then terraform plan.
Expected output: init downloads the Docker provider; plan lists 7 resources to create (the network, three images, the database, the
API and the load balancer) and ends with Plan: 7 to add, 0 to change, 0 to destroy. After Project 1 the plan has 11 (adding a volume, a
generated password, a random suffix and a second API copy), and 15 with -var monitoring=true. Counted on 1 November 2026 with Terraform
1.17.1.
🌍Where it's used
- New environments: a bank's team creates a full test copy of its payment platform for each big release, then destroys it.
- Disaster recovery: a region outage can be answered by applying the same files in another region (CD6).
- Audits: the Git history shows who changed a firewall rule, when, and who approved it.
📝Case scenario
A hospital chain had three "identical" environments built by hand over two years. A booking bug appeared only in production; after a week of searching, they found a database setting someone had changed in production during an incident and never copied elsewhere. After moving to Terraform, the three environments came from the same files with different sizes, and a difference like that showed up in a plan.
🖱️Try it
Run terraform plan in the starter's infra/ folder, then terraform apply, then terraform plan again. Success: the first plan shows
7 to add, the apply creates them, and the last plan says No changes. Your infrastructure matches the configuration.
⚠️Common mistakes
- Changing things by hand "just this once". Sign: the next plan wants to undo your fix, or everyone's afraid to apply. Fix: change the files and apply.
- Applying without reading the plan. Sign: surprise deletions. Fix: read every plan, especially any line with
destroyorreplace. - Treating Terraform like a script. Sign: files that list steps in order. Fix: describe the end state and let references set the order.
🛠️For your project
Project 1 is an environment built entirely from the files in infra/; tests 7, 8 and 10 check that plan, drift repair and rebuild work.
✅Quick check
What does "declarative" mean for Terraform?
Show the answer
You describe what should exist, and Terraform works out what to create, change or delete.
What does terraform plan do?
Show the answer
Compares the files with what exists and shows the changes it would make, without making them.
