🏗️ How Terraform Works

You describe the destination. Terraform figures out the route, checks the map against reality, and drives — one cloud API call at a time.

1. Infrastructure as Code — The Big Idea

Before Terraform, "infrastructure" meant clicking through a cloud console, or running one-off scripts that nobody remembered a year later. Infrastructure as Code (IaC) means you write the desired end-state in text files, commit them to Git like any other code, and a tool makes reality match the text.

📐 Analogy: A cloud console click-fest is like building a house with no blueprint — it works, but nobody can rebuild it identically, and nobody remembers why that one wall is there. Terraform is the blueprint. Anyone can read it, review it, version it, and hand it to a different builder (engineer) who gets the exact same house.
Key point: Terraform is declarative — you state WHAT you want ("3 web servers, 1 database"), not HOW to get there step by step. Terraform itself works out the order of operations and the exact API calls.

2. The Core Loop — Animated

Press ▶ Play and watch what happens when you run terraform apply for the first time:

📄 main.tf resource "aws_instance" "web" {   instance_type = "t3.micro" } terraform apply ⚙️ Terraform Core idle plugin 🔌 AWS Provider idle API call ☁️ AWS (the real world) no EC2 instance yet 🗄 terraform.tfstate empty
Ready — press Play to run "terraform apply".

3. What Happens in Each Step

#StepWhat actually happens
1Read configTerraform Core parses every .tf file in the folder and builds an internal graph of everything you want.
2Load provider pluginFor each provider block (AWS, Azure, GCP...), Terraform downloads and talks to a plugin that knows that cloud's API.
3Compare to stateTerraform checks terraform.tfstate — its record of what it built last time — to see what's new, changed, or gone.
4Call the real APIThe provider plugin translates your resource block into actual AWS/Azure/GCP API calls that create real infrastructure.
5Save the new stateOnce the resource exists, Terraform records its real ID and attributes in terraform.tfstate — this is how it will recognize it next time.
⚠️ Terraform never "remembers" your infrastructure by reading the cloud fresh every time. It trusts the state file. Lose the state file, and Terraform thinks nothing exists — even though your servers are still running and costing money. Chapter 4 is entirely about this.

4. The Four Commands You'll Type Constantly

# One-time setup per folder — downloads provider plugins
terraform init

# Preview — what WOULD change, without touching anything
terraform plan

# Actually make it happen
terraform apply

# Tear it all down when you're done (labs, temporary environments)
terraform destroy
🍕 Analogy: init = stock the kitchen with the right tools for this recipe. plan = read the recipe out loud before cooking anything ("this will add 2 eggs, remove the old flour"). apply = actually cook it. destroy = clear the plates — safely, on purpose, all at once.

5. Vocabulary You'll See Everywhere

TermMeaningAnalogy
ProviderPlugin that knows how to talk to a specific API (AWS, Azure, GCP, GitHub, Kubernetes...)A translator for one language
ResourceOne real-world object Terraform manages (a VM, a database, a DNS record)One line item in the blueprint
StateTerraform's record of what it has already builtThe builder's own notebook
PlanA preview of exactly what will change before it happensA dry run / read-aloud of the recipe
ApplyActually executing the plan against real infrastructureCooking the meal
IdempotentRunning apply again with no config changes does nothing — already matches"Already done? Skip it."
🔓 You're reading a free chapter of Terraform — the first two are open.
Unlock the rest of this course with a one-time payment.
Unlock this course →
🔓 See course prices