PlatformEngr® Terraform course

Terraform for platform teams: printable course notes

Run Terraform as a platform team: the plan and apply loop, inputs and validation, remote state with a lock and secrets kept out of it, a module for every team, dev and prod in separate directories, tests, checks, policy and CI, import, drift and provider upgrades, five debugging labs, and a capstone.

These pages are the same content as the interactive course, on one page per module, for reading and printing.

Four pieces

PieceWhat it is
See the end resultNot the first step: a 30-minute preview with few explanations. A cluster, a locked remote state, one module for two teams, and a plan that would destroy, stopped. Useful to know where you are heading.
Full course10 modules from an empty laptop to five teams on one module: state and locking, modules, dev and prod, tests and CI, import, drift, upgrades, five debugging labs and a capstone.
Reference repoThe finished implementation: the module, both environments, the state store, the policy, the workflows and the docs. You copy from it.
Your repotf-platform: the module, envs/dev and envs/prod. You create it in module 00.

Your setup

Commands in these notes use these values (shared with the interactive course, stored only in this browser).

Outline

ModulePartTimeYou areYou will
00 Orientation: what a platform team keeps in TerraformOrientation20 minPlatform and productCreate the repo and the cluster, and initialise an empty Terraform configuration in envs/dev.
01 Write, plan, apply: a namespace from codeThe loop35 minPlatform teamCreate team-checkout's namespace, quota and default limits from envs/dev/main.tf, with the provider pinned and locked.
02 Inputs, locals, outputs and validationThe loop35 minPlatform teamReplace every hard-coded value in envs/dev with a typed, validated variable, a local or an output.
03 State: a shared backend, a lock, and the secrets insideState, modules and environments45 minPlatform teamMove dev's state into a locked remote backend and keep the one secret you create out of it.
04 One module, two teamsState, modules and environments45 minPlatform teamPackage the tenant kit as a module with typed inputs, move checkout into it without destroying anything, and use it for a second team.
05 Dev and prod without copy and pasteState, modules and environments35 minPlatform teamRun the same module for dev and prod from two directories with separate, locked state, and say who reviews each.
06 Tests, checks and a pipeline on every pull requestSafe change45 minPlatform and productMake every pull request prove that the code is formatted, valid, tested and within policy, with no access to the cluster or the state.
07 Import, drift and provider upgradesSafe change45 minPlatform teamBring a hand-made namespace under Terraform, detect and undo a hand change, and upgrade a provider with an empty plan as the proof.
08 Debugging labs: five plans that go wrongSafe change45 minPlatform teamDiagnose five common Terraform failures from their messages and fix each without destroying anything.
09 Capstone: onboard team-payments on your ownSafe change35 minPlatform and productOnboard a fifth team end to end, extending the module without replacing anything that exists.

The change loop

  1. Write: Describe what should exist, in `.tf` files in Git.
  2. Plan: Terraform compares code, state and the real objects and proposes the steps. Nothing changes.
  3. Review: A pull request with checks, and a person who reads the plan's last line.
  4. Apply: The steps run, and the result is written to state.
  5. State: The record of what Terraform manages. Locked while someone writes it.

Terraform or OpenTofu

Every module works with both. These notes show terraform; on OpenTofu type tofu instead. Only module 06 (the workflow file) and module 07 (Dependabot's ecosystem name) differ.

Between you and production