How it works
The explanation, with examples you can run.
CLI workspaces
$ terraform workspace new stagingCreated and switched to workspace "staging"! You're now on a new, empty workspace. Workspaces isolate their state,so if you run "terraform plan" Terraform will not see any existing statefor this configuration. $ terraform workspace list default* staging(Terraform 1.5.7, local backend, where the new state went into terraform.tfstate.d/staging/.) The configuration can read terraform.workspace to vary names or sizes. Workspaces are convenient for short-lived copies, such as one per feature branch or a developer's sandbox.
Their weakness is that the only difference between environments is which state is selected. The same code, the same backend and, usually, the same credentials can reach every workspace, and one forgotten terraform workspace select applies dev's change to prod. The Terraform documentation says plainly that workspaces are not appropriate for system decomposition or for deployments that need separate credentials and access controls.
Separate state per environment
modules/ service/ # shared, versioned building blocks network/envs/ dev/ main.tf # module calls with dev inputs backend.tf # key = "checkout/dev/terraform.tfstate" prod/ main.tf # same modules, prod inputs backend.tf # key = "checkout/prod/terraform.tfstate", prod accountEach environment directory is a small root module that calls shared modules with its own values. Each has its own backend key, often its own bucket in its own AWS account, and CI uses a different role for each, so a dev pipeline physically cannot write prod state. Differences between environments are visible as different inputs in code review, not hidden behind terraform.workspace conditionals.
Variants of the same idea: one root with -backend-config and -var-file per environment (less duplication, but the CI job must pass the right pair), wrapper tools such as Terragrunt that generate the per-environment boilerplate, and HCP Terraform workspaces, which are full units with their own state, variables and permissions and so behave more like separate roots than like CLI workspaces. Terraform Stacks, newer in HCP Terraform, model several deployments of the same components explicitly.
Splitting by layer, too
Environments are one axis; layers are another. Keep long-lived, rarely changed infrastructure (accounts, networks, DNS) in different states from fast-moving application resources. Smaller states plan faster, lock less often, and limit what a single bad apply can damage. Pass values between them through outputs published to a parameter store, or narrow data sources, rather than one giant state.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.