Infrastructure as code

Play this lesson

Built by clicking

Months ago, Remy set up Shard Garden's servers by clicking through the cloud console. Nobody remembers every setting now, and staging differs from production in ways nobody can list.

Infrastructure as code

Infrastructure as code writes servers, databases and networks down in files. Terraform, a popular tool for it, lets a team "build, change, and version cloud and on-prem resources safely and efficiently."

Its files are declarative: they "describe the end state of your infrastructure", not the clicks to get there. They live in Git and are reviewed in pull requests, like code.

Quick check

What does a declarative Terraform file describe?

  1. What should exist when it is done
  2. Every click, in order
  3. Only what to delete
Show the answer

What should exist when it is done

Declarative means the end state. Terraform works out the steps to get there.

Write, plan, apply

Terraform works in three steps:

Read the plan

plan
# google_firestore_database#   .saves must be replaced-/+ resource ... "saves"Plan: 1 to add,  0 to change,  1 to destroy.

What will happen if Remy approves this plan?

  1. The saves database is destroyed and a new one is created
  2. One setting changes in place
  3. Nothing: adding one and destroying one cancel out
Show the answer

The saves database is destroyed and a new one is created

"-/+" and "must be replaced" mean destroy, then create a new one. A new database starts empty, so Remy stops and finds out why before approving.

State: what Terraform remembers

Terraform "keeps track of your real infrastructure in a state file, which acts as a source of truth for your environment."

When someone changes a setting by hand in the console, the real thing drifts away from the files. The next plan shows the difference.

Quick check

Priya changed a database setting by hand in the console. What will the next plan show?

  1. A change, to put the setting back to what the files say
  2. Nothing: the console change is now permanent
  3. An error that stops all plans
Show the answer

A change, to put the setting back to what the files say

The files are the source of truth. To keep Priya's change, it goes into the files and through a review.

Same files, three environments

Shard Garden's dev, staging and production are built from the same files, with one variable for the environment's name and size. Staging really matches production, because the same code made both.

Quick check

Why build staging from the same files as production?

  1. So staging really matches production
  2. Files make staging cheaper
  3. Terraform can only read one set of files
Show the answer

So staging really matches production

Staging is a rehearsal. If it is built differently, it can pass changes that production then rejects.

And the game server itself?

The servers are now code. The game server program still needs to run the same way on every one of them.

Bolt says: Next: containers.

Fact-checked 12 October 2026