Terraform Environments Done Right: Workspaces Are a Trap
How to structure Terraform for multiple environments without copy-pasting modules or stepping on production state.
Terraform workspaces look like the easy answer for environments. Then one day someone runs terraform destroy on the workspace that controls the prod state, and you understand the real cost. Here is the structure that scales.
Never share state files between environments
Workspaces share a backend. Environments share risk. A mistake in environment code can reach production's state. The pattern that holds: one state file per environment, isolated backends.
infra/
├── backend/
│ ├── s3/ # sets up the S3 + DynamoDB lock
│ └── main.tf
├── modules/
│ ├── vpc/
│ ├── eks/
│ └── app/
├── environments/
│ ├── dev/
│ │ ├── backend.tf
│ │ ├── main.tf
│ │ └── terraform.tfvars
│ ├── staging/
│ │ ├── backend.tf
│ │ ├── main.tf
│ │ └── terraform.tfvars
│ └── prod/
│ ├── backend.tf
│ ├── main.tf
│ └── terraform.tfvars
Each environment points at its own S3 key. Production is detached from dev by construction, not by discipline.
The module contract
Modules are where you get value. Three rules:
- Small and opinionated. A module with 14 optional variables and 9
countflags is a config generator, not a module. - Versioned, not copied. Publish modules to a registry and pin
source = "git::...?ref=v1.2.0". Copy-pasting modules across environments recreates drift by hand. - Tested at plan time.
terraform validatein CI plusterraform planagainst each env's state catches 90% of accidents before they land.
The drift problem
Terraform is a source of truth only until someone clicks a console button. Catch drift with:
terraform plan -detailed-exitcode
# 0 → no changes, 1 → plan succeeded with changes, 2 → error
Run it nightly, alert on unexpected changes, and treat a modified resource outside your code as a bug to fix, not to ignore.
What workspaces are actually good for
They are not useless, they are just not for environments. A workspace is a different state key inside one backend, which makes it a decent fit for a short-lived sandbox: a spike branch, a demo environment, a migration rehearsal that exists for a day and should vanish with the branch.
The tell is lifetime. If something lives longer than the change that created it, it deserves its own directory and its own backend. If it dies with the branch, a workspace keeps the overhead at zero.
The dangerous part is the ergonomics. terraform workspace select prod is one
keystroke from whatever you were editing, gives no visual warning, and the
default state file you thought you were writing to may not be the one you are.
Environments that matter should be hard to reach by accident.
How values reach each environment
The structure above gives you separate state; you still need somewhere for the numbers. Three options, in the order I would rule them in:
terraform.tfvarsper environment directory. Read automatically, visible next to the code that uses it, impossible to apply to the wrong directory.-var-filepassed explicitly in CI. Best when the values are generated or sealed rather than committed.- A single shared tfvars file. This is the one that causes the incident, for the same reason shared state does.
Whichever you pick, keep the shape identical across environments: same
variable names, same types, so that a missing value in staging fails at plan
time instead of silently taking a default into prod.
Guardrails in the pipeline
Directory separation stops most mistakes; CI stops the rest. The three worth having: plan on every pull request with the output posted as a comment, apply only from CI so that no laptop holds production credentials, and block applies that contain a destroy without an explicit approval step.
That last rule is cheap and prevents the failure this post opened with: a plan showing three resources to destroy should require a human to type something, regardless of who opened the pull request.
Promoting through environments
The promotion question is: does prod run exactly what staging ran? Close to it:
- Same module versions, same source revision
- Environment-specific differences confined to variables and remote state
- No
localsthat secretly branch on env name: it hides real divergence
CI/CD for infra
Apply only from CI, never a laptop (unless the laptop is the person on-call fixing an outage). Plan in PRs as a pull request comment:
terraform plan -out=tfplan
terraform show -json tfplan # comment the summary on the PR
Summary
Isolated state per environment, versioned modules, drift alerts, and plans gated by CI. That's the whole trick: the rest is avoiding the temptation to make it clever.
Divyesh Parmar
Founder & editor, SDP Clouds
Writes SDP Clouds: hands-on DevOps, Kubernetes, CI/CD, and cloud engineering. Every article comes from work actually done in a terminal, not from rewriting documentation.
More about the author →