Terraform Workspace
A Terraform workspace is a named, separate state within one backend and configuration. Workspaces run the same code against different state files, which suits short-lived environments more than permanent dev, staging, and production.
What Is a Workspace?
Every configuration starts in the default workspace. Creating staging gives a second independent state for the same code.
terraform workspace new stagingterraform workspace select prodterraform workspace showUse terraform.workspace in code to vary settings. Workspaces fit PR preview environments and developer sandboxes.
Common MistakeSeparating prod from dev with workspaces on a large team. They share one backend and credentials, so a wrong-workspace apply can hit production. Use separate directories and accounts instead.
RememberTerraform Cloud workspaces are a different, more isolated concept.
Frequently Asked Questions
How is a workspace different from a directory per environment?
A workspace changes only which state file is used. A directory per environment has its own backend key and variables, so access can be isolated.
Why do many teams avoid workspaces for production?
They share credentials and backend access, so a stray apply in the wrong workspace is easy to make.