Intermediate

Multi-Environment Workflows

Compare and implement two standard patterns for managing dev/staging/prod environments in Terraform — directory-per-environment and Terraform workspaces — and understand when each is the right fit.

~2.5h
0 / 7 steps
🚀
Intro

What you're building

There are two dominant, genuinely different patterns for running the same infrastructure across multiple environments, and picking the wrong one for your situation causes real pain later. Terraform workspaces (terraform workspace new prod) let one configuration maintain multiple, named, isolated state files without duplicating any .tf files — lightweight, but every environment is forced to share identical configuration structure, varying only through variables. Directory-per-environment duplicates a thin per-environment main.tf that calls shared modules with different inputs — more files, but environments can genuinely diverge in structure when they need to (prod needs a resource dev doesn't, for instance) without fighting the tool.

You'll build the same small setup both ways, feel the tradeoff directly, and end with a clear opinion on which pattern fits which kind of team and infrastructure.

🔨

Step 1 — Build the workspaces version

🔨

Step 2 — Build the directory-per-environment version

🔨

Step 3 — Compare the blast radius of a mistake in each pattern

🔨

Step 4 — Add environment-specific tfvars files as a lighter middle ground

🎯
Secret Mission

Secret Mission: add a CI guardrail against the wrong-workspace mistake

🧹
Wrap Up

Before You Go

Pro

Test what you just learned

Self-testing is one of the best ways to retain new skills. Unlock project quizzes to check your understanding.

Log in to unlock