Build-and-Destroy-Stack-Per-Service.md 5.7 KB

Build and destroy a bunch: one stack per service

Side page to OpenTofu, stacks and modules (core step 15), and the fourth lesson of a four-lesson infrastructure-as-code track: OpenTofu from zero, then plans, state, drift and recovery, then Spacelift from zero, then this page. It teaches how to split an environment into one stack per service, how to wire the stacks together, and how to build and destroy the whole set without leaving a bill behind.

Next: Teardown checklist.

Unfamiliar word or acronym? The Glossary defines it.

Status: the free lab was run on Linux; the cloud lab was only validated, never applied; PowerShell was not run. On 2026-10-22 the author ran the whole free lab on Linux with OpenTofu 1.12.6 (the version CI pins) and the hashicorp/local provider 2.9.1: five full build-and-destroy cycles, each ending with a checker that found every state empty, plus the wrong-order and forgotten-resource exercises (Exercise 5 and Exercise 6); the output shown is from that run, trimmed. The cloud lab's five stacks were checked with tofu init and tofu validate against the digitalocean/digitalocean provider 2.106.0 (a schema check: no token, no API call, nothing created), so their argument names are right, but nothing in them was ever applied. Expect to fix something. The PowerShell scripts were written from Microsoft's and OpenTofu's documentation and were not run (there was no Windows machine): treat every PowerShell line as est. Vendor facts come from the pages listed in the claims register, read on 2026-10-22. Nothing here was run against this project's own stacks, which are locked. Prerequisites. The three earlier lessons of the track: OpenTofu from zero (init, plan, apply, destroy), Plans, state, drift and recovery and Spacelift from zero. If you want less first, What is infrastructure as code and OpenTofu, stacks and modules cover the minimum (provider, resource, plan, apply, state, stack). Also useful: The terminal and your first script and Teardown checklist. The free lab needs OpenTofu 1.8 or newer, a terminal and a few megabytes of disk; no account, no money. About 60 minutes of reading and 90 minutes for the free lab (est.); the optional cloud lab adds about an hour (est.) and uses your own account.

The parts

This page is split into four parts so each stays readable. Read them in order; the text is unchanged from the single page.

  1. Why and how to split, and the lab script: The concepts (why one stack per service, layout, passing values, build and destroy order, tags, costs, lessons), then the start of the free path: costs and the Bash setup script.
  2. The PowerShell lab and exercises 1 to 4: The PowerShell version of the lab, then Exercises 1 to 4: predicting, building, destroying and five full cycles.
  3. Exercises 5 to 8 and the cloud lab: Exercises 5 to 7 (wrong order, a forgotten resource, golden addresses), then the optional cloud lab with Exercise 8.
  4. Spacelift, break and defend, and the claims register: The Spacelift option, build, break, detect, evade, defend, common mistakes and the claims register.

Exercise index

Every exercise follows predict, do, check, undo. Exercises 1 to 7 are free and local; Exercise 8 is optional and uses your own cloud account.