Lesson 5 of 9 · 22m
Workspaces Versus Directory Isolation
Choose environment topology based on blast radius, credentials, state isolation, and operational clarity rather than habit or tool fashion.
Every few years the industry rediscovers the same argument: should environments be Terraform workspaces, separate directories, separate backends, or separate accounts? The correct answer is boring. Choose the topology that gives you clear ownership, isolated credentials, and a blast radius you can explain during an incident.
Workspaces are state instances, not environments by magic
A workspace is a named state instance for the same configuration:
terraform workspace new staging
terraform workspace select prod
terraform workspace showInside configuration, terraform.workspace can alter names or counts:
locals {
environment = terraform.workspace
name = "billing-${terraform.workspace}"
instance_count = terraform.workspace == "prod" ? 3 : 1
}This works when environments truly share the same shape and differ mostly in scale or names. It fails when prod needs different providers, regions, compliance controls, or resources that staging intentionally lacks.
The hidden danger is shared control planes
Workspaces often reuse one backend role and one configuration checkout. That means a credential able to apply staging can often apply prod, and a sloppy local workspace select mistake can send production changes to the wrong place. State paths differ; human risk does not disappear.
staging workspace -> staging state key
prod workspace -> prod state key
same IAM role -> same potential blast radiusConvenience is not isolation.
Directory-per-environment makes differences explicit
A common production layout looks like this:
envs/
staging/
backend.tf
main.tf
variables.tf
terraform.tfvars
prod/
backend.tf
main.tf
variables.tf
terraform.tfvars
modules/
service/Each root has its own backend, state, provider configuration, and ideally its own automation role. Shared modules keep duplication controlled without forcing identical infrastructure.
Separate accounts are stronger than naming discipline
For serious production isolation, put environments in separate cloud accounts or subscriptions when the platform supports it. Terraform cannot accidentally destroy what its credentials cannot reach. Organizational guardrails — service control policies, permission boundaries, separate secret scopes — are stronger than a README saying be careful.
Workspaces still have good uses
Use workspaces for ephemeral review environments, disaster recovery rehearsals, or short-lived copies of the same stack. They are useful when lifetime is short, shape is identical, and cleanup is automated. They are less attractive as the primary production/staging governance boundary.
The rule I give new hires
If you cannot answer 'who can apply this, what credentials are used, what state is touched, and what breaks if it goes wrong' in one breath, the topology is too clever. Make the boundary visible in the repository layout, backend configuration, and cloud permissions.
Field notes
- Never use
terraform.workspacefor secrets. Workspaces select state; they are not authorization boundaries. - If staging and prod differ by more than a few values, duplicated root modules are usually clearer than conditional sprawl.
- Automation should pass the working directory explicitly. Interactive shell state is not a deployment strategy.
- Review environment promotion as configuration change plus variable change, not merely copying a tfvars file upward.
- A separate backend without separate credentials is only partial isolation.
Was this lesson helpful?