Introduction
Every Terraform user hits the same problem within weeks of starting: the configuration works for one environment, and now there are three. Development, staging and production need the same infrastructure in different sizes, with different values, and — critically — with different state, so that destroying development never touches production. Terraform workspaces are the built-in answer to that problem, and they are also one of the most misunderstood features in Terraform. They are not environments. They are not folders. They are not a deployment strategy. They are a way of keeping several state files behind a single configuration, and understanding exactly what that means is the difference between using them well and using them to build a system nobody can reason about.
This guide explains workspaces as they stand in Terraform 1.17.2, the current release as of 30 November 2026: what a workspace actually is, the commands that manage them, how to use the workspace name inside your configuration, how they differ from separate state files and from modules, what workspaces mean in HCP Terraform — where the word means something related but different — and, most importantly, when not to use them at all. If you are new to Terraform itself, start with our Terraform Tutorial for Beginners first; this guide assumes you can already write a configuration, run terraform plan and terraform apply, and configure a backend. Our Terraform State Explained covers backends, locking and state surgery in detail, and it is the companion to read alongside this one, because workspaces are a state feature before they are anything else.
What a Workspace Actually Is
A workspace is a named container for state, attached to a single configuration. When you run Terraform in a directory, you are always in a workspace — the default workspace, unless you have created and selected another one. Each workspace keeps its own state file, and the state files are kept apart by the backend: with a local backend they live in separate files under terraform.tfstate.d/, and with a remote backend such as S3, each workspace gets its own state object under a workspace-specific key. The configuration is the same in every workspace. The state is not. That is the whole mechanism, and everything else follows from it.
The practical effect is that you can run the same configuration three times — once in a dev workspace, once in staging, once in production — and each run manages a separate set of real resources. Destroying the dev workspace's resources does not touch production, because the two runs read and write different state, and a change to the configuration applies to all three environments the next time each one is applied. That single-source-of-truth property is the reason workspaces exist, and it is also what makes them dangerous when the environments are not, in fact, the same shape.
It helps to say plainly what a workspace is not. It is not an environment in the organisational sense: it carries no permissions or approval process, so anyone who can run Terraform against the configuration can select the production workspace and apply to it. It is not a folder: the configuration files do not change when you switch workspaces. It is not a deployment strategy: workspaces do not know about promotion or testing, and they will not stop you applying an untested change to production. And it is not a substitute for a module: a module packages configuration for reuse across different systems, while a workspace reuses one configuration across copies of the same system.
There is one more thing a workspace is not, and it is the source of the most common confusion in Terraform: a workspace in the Terraform CLI and a workspace in HCP Terraform are related but different things. A CLI workspace is the state container described above. An HCP Terraform workspace is a much larger object — a group of infrastructure resources managed by Terraform, with its own variables, its own run history, its own permissions, and its own connection to a version control repository. The two share a name and a purpose, but an HCP Terraform workspace is a managed environment with a UI and an API, while a CLI workspace is a state partition. The HCP Terraform section below covers the difference in full.
The Workspace Commands
There are only five workspace commands, and they do exactly what their names suggest. Learn them in one sitting and you will never need to look them up again.
| Command | What it does |
|---|---|
terraform workspace list |
Lists all workspaces, marking the current one with an asterisk |
terraform workspace show |
Prints the name of the current workspace |
terraform workspace new <name> |
Creates a workspace with the given name and switches to it |
terraform workspace select <name> |
Switches to an existing workspace |
terraform workspace delete <name> |
Deletes a workspace (it must not be the current one) |
The workflow is the same every time. You start in default. You run terraform workspace new staging, which creates the staging workspace, switches to it, and gives it an empty state — the next terraform plan will show every resource as needing to be created, because staging has no state yet. You apply, and the staging resources are created and recorded in staging's state. You switch back with terraform workspace select default, and the plan goes back to showing the default workspace's resources. The asterisk in terraform workspace list is the only visible sign of which workspace you are in, which is why the first habit of workspace use is to run terraform workspace show before every apply — and the reason the second habit is to put the workspace name in your prompt or your CI output, so that a command run in the wrong workspace is visible before it does damage.
Two details deserve attention. The first is that terraform workspace new copies nothing: the new workspace starts with empty state, even if the default workspace already manages resources. The second is that terraform workspace delete refuses to delete a workspace that still tracks resources unless you pass -force — deleting a workspace's state without destroying its resources orphans real infrastructure that no state file knows about. Always destroy first, then delete the workspace.
Workspaces and backends interact in a way that is easy to get wrong. Workspaces are supported by the local backend and by the standard remote backends — S3, Azure Blob Storage, Google Cloud Storage, and the HCP Terraform remote backend — but the way each backend stores workspace state differs, and not every backend supports them. Before relying on workspaces, verify the behaviour in a scratch configuration: create two workspaces, apply a trivial resource in each, and confirm in the backend that two separate state objects exist.
Using the Workspace Name in Your Configuration
A workspace is only useful if the configuration can tell which workspace it is running in, and Terraform provides exactly one mechanism for that: the terraform.workspace value. Inside your configuration, terraform.workspace evaluates to the name of the current workspace — default, staging, production, or whatever you named it — and you can use it anywhere a value is expected: in a resource argument, in a variable default, in a local value, in a tag.
The standard pattern is a map lookup. Define a local value that maps workspace names to the values that differ between environments — instance sizes, replica counts, feature flags — and look the current workspace up in the map:
locals {
instance_size = {
default = "t3.micro"
staging = "t3.small"
production = "t3.large"
}
}
resource "aws_instance" "web" {
ami = var.ami
instance_type = local.instance_size[terraform.workspace]
}
In the default workspace this creates a small instance; in production it creates a large one; and the configuration is a single file that describes all three. The same pattern works for tags — Environment = terraform.workspace is the single most common tag in workspace-based setups — and for counts, such as running one replica in development and three in production. The pattern is powerful precisely because it is small: one value, one lookup, and the environment differences are visible in one place.
The discipline around the pattern is what separates a good workspace setup from a bad one. First, keep the differences small. If the map has three entries that differ only in size, workspaces are working as intended. If the map has thirty entries, or the configuration is full of conditionals on terraform.workspace, the environments have diverged past the point where one configuration describes them honestly. Second, **always provide a default entry in every map, because a workspace whose name is not in the map will fail the lookup and the plan will error. Third, do not branch on the workspace name for security or access decisions**: a workspace name is a label, not a boundary. If production needs different credentials or approval from development, that boundary belongs in your backend permissions and your CI pipeline, not in a conditional in your configuration.
Variable files are the other half of the pattern. Many teams combine workspaces with per-environment variable files — terraform apply -var-file=dev.tfvars in the dev workspace, -var-file=prod.tfvars in production — and that combination is fine, provided the two mechanisms do not fight: the variable file sets the values, the workspace selects the state, and the configuration stays the same. The mistake is to use variable files to paper over environments that have genuinely diverged, so that the same configuration means different things in different workspaces and nobody can tell what production actually contains without reading three files. If you find yourself maintaining a variable file per workspace that is longer than the configuration itself, that is the signal to stop and read the section on when not to use workspaces.
Workspaces vs Separate State Files vs Modules
Workspaces are one of three ways to manage several environments, and choosing between them is the most consequential decision in this guide. The other two are separate state files — usually separate directories, one per environment, each with its own backend configuration — and modules, which package reusable configuration. The three are not interchangeable, and each one fits a different situation.
| Approach | What changes per environment | What stays the same | Best for |
|---|---|---|---|
| Workspaces | State only, plus values looked up by workspace name | The whole configuration | Near-identical environments that differ in size and values |
| Separate state files / directories | State and the configuration that references it | The module or shared code the directories call | Environments that differ in structure, resources or providers |
| Modules | Nothing — a module is called with different inputs | The module's own code | Repeating a pattern (a VPC, a service, a database) across systems |
Separate directories are the most explicit approach: a dev/ directory, a staging/ directory and a production/ directory, each with its own backend configuration pointing at its own state, and each calling the same module with different inputs. The cost is duplication — three directories to keep in step — and the benefit is honesty: each environment's configuration is visible in its own directory, and the differences are explicit in the inputs. When environments differ in structure — production has a multi-region database and development does not — separate directories are the right answer, because the configuration genuinely differs and pretending otherwise with workspace conditionals only hides the difference.
Modules answer a different question. A module, covered in our Terraform Modules Tutorial, is a reusable package of configuration: a VPC module, a service module, a database module. Modules are how you repeat a pattern across different systems, and they compose with either of the other two approaches — a workspace-based setup calls a module once with workspace-dependent inputs, and a directory-based setup calls the same module three times with different inputs. A workspace is not a module, and a module is not an environment.
The decision rule is simple enough to state in one paragraph. Use workspaces when the environments are the same shape and differ only in size, count and values — the classic dev, staging and production of a single application. Use separate directories when the environments differ in structure, in providers, in resources, or in who may change them — or when the team is large enough that an accidental apply to the wrong workspace is a real risk, because a directory makes the target visible in a way a workspace name in a prompt does not. And use modules in both cases, because the shared code should live in a module either way. If you are unsure which of the first two fits, choose separate directories: the cost of duplication is small, the cost of a production incident caused by a workspace mistake is large, and the duplication can always be reduced later by moving the shared code into a module.
Workspaces in HCP Terraform
In HCP Terraform — HashiCorp's managed Terraform platform, formerly Terraform Cloud — the word "workspace" means something larger, and the difference matters if you move from the CLI to the platform. An HCP Terraform workspace is a group of infrastructure resources managed by Terraform, with its own variables, its own run history, its own permissions, and its own connection to a version control repository. Each workspace belongs to a project, and its name must be unique within the organisation. You create workspaces in the HCP Terraform UI, through the workspaces API, with the tfe provider's tfe_workspace resource, or through no-code provisioning from a registry module — and a deleted workspace can be recovered within 30 days, provided the organisation has recovery enabled and holds a paid subscription.
The important difference is that an HCP Terraform workspace is an environment with a workflow, not just a state partition. It has an execution mode — remote, or agent-based on your own agents — and a workflow type, most commonly the VCS workflow, where a push to a connected repository triggers a plan automatically. It has variables of its own, including sensitive variables never shown in plain text, and run triggers, so a successful apply in one workspace can trigger a plan in another. And it has permissions: creating and managing workspaces requires a team with the Manage all projects, Manage all workspaces or project Admin permission. That permission model is the boundary CLI workspaces lack — in HCP Terraform, you can genuinely prevent an engineer from applying to production.
The relationship is straightforward: a CLI workspace partitions state within one backend configuration, while an HCP Terraform workspace is a managed environment that holds its own state, variables and history. When you use the HCP Terraform remote backend, your CLI configuration connects to one HCP Terraform workspace, and switching environments means switching HCP Terraform workspaces, not CLI workspaces. If your team is moving to HCP Terraform, plan for that shift of mental model explicitly.
One practical note on cost: HCP Terraform is a paid platform, and workspace recovery requires a paid subscription. The legacy free tier ended on 31 March 2026, according to a 2026 guide that tracked the change — check the current pricing page before planning a migration. The CLI, by contrast, is free, and CLI workspaces cost nothing beyond backend storage. HCP Terraform workspaces buy you a permission model, a run history and a VCS workflow, and the price is a subscription.
When Not to Use Workspaces
Workspaces are a good tool for a narrow job, and the rest of this section is the list of situations where they are the wrong tool. Each one is a pattern seen in real teams, and each one ends the same way: a production incident, or a configuration nobody can change safely.
When the environments differ in structure. If production has resources that development does not — a multi-region database, a private network, a backup vault — then one configuration with workspace conditionals is lying about what it describes. The conditionals multiply, the maps grow, and eventually nobody can tell what production contains without running a plan against the production workspace, which is exactly the operation you wanted to avoid. Use separate directories, and let the difference be explicit.
When the environments need different access. A workspace has no permission model. Anyone who can run Terraform against the configuration can select the production workspace and apply to it, and the only thing standing between them and production is their own care. If your organisation needs to restrict who may change production — and most organisations do, once they are large enough to have a production incident — then workspaces cannot provide that boundary. Use separate directories with separate backend permissions, or HCP Terraform workspaces with their team-based permissions.
When the team is large or the stakes are high. The failure mode of workspaces is applying to the wrong workspace, and the probability rises with the number of people running Terraform. The asterisk in terraform workspace list is a thin defence. If a wrong-workspace apply would be a serious incident, the visibility of a directory — where the target is in the path you typed — is worth the duplication. Ask concretely: if someone applied to production thinking they were in staging, how would you know, and how fast?
When you need different providers or provider versions per environment. A workspace shares one configuration, and therefore one provider configuration. If staging needs a different provider, a different region, or a different provider version from production, workspaces cannot express that without conditionals that make the configuration unreadable. Separate directories can, because each directory has its own provider block.
When the state itself is the problem. Workspaces do not solve state size, state locking contention, or the blast radius of a single state file. If your state is large enough that plans are slow, or your team is large enough that applies queue behind one another's locks, splitting by workspace does not help — each workspace still holds a whole environment's state. The answer is to split the configuration into smaller pieces, with separate state per piece.
The short version of all five: workspaces are for near-identical environments that differ in size and values, run by a small team, where an accidental apply to the wrong workspace would be embarrassing rather than catastrophic. Outside that envelope, separate directories or HCP Terraform workspaces are the better answer — and the cost of choosing wrong is a production incident, which is why this section is the longest in the guide.
A Practical Pattern That Works
For a team that fits the workspace envelope — a small team, one application, three near-identical environments — here is a pattern that works, end to end, and that you can adopt without inventing anything.
Keep one configuration, one module, three workspaces. The module holds the resources; the root configuration calls it once, with inputs looked up from maps keyed by terraform.workspace. The differences between environments live in those maps, and nowhere else: instance sizes, replica counts, and the environment tag. The backend is remote — S3 with native state locking, or the equivalent on your cloud — and the backend configuration is the same for all three workspaces, so that the state partition is the backend's job, not yours. Our Terraform State Explained covers the S3 backend and its locking in detail, including the native locking that arrived in Terraform 1.10 and became generally available in 1.11.
Run Terraform from CI, not from laptops. The CI pipeline selects the workspace explicitly — the workspace name is a parameter of the job, visible in the pipeline's output — and the plan is posted for review before any apply. The production job requires an approval, and the approval is a human clicking a button. This is the permission boundary that workspaces lack, built in the pipeline instead: the pipeline, not the workspace, decides who may apply to production.
And keep a written record of what each workspace contains. A short file in the repository — three lines, one per workspace, naming the environment, the workspace, and the variable file it uses — is enough. The file's purpose is not documentation for its own sake; it is to make the answer to "which workspace is production?" a fact in the repository rather than a fact in one person's head. The day that fact lives only in someone's head is the day someone applies to the wrong workspace, and the file costs nothing to write.
That is the whole pattern: one configuration, one module, three workspaces, a remote backend, CI-driven applies with approval for production, and a written record. It is deliberately boring, and boring is the point — the exciting workspace setups are the ones that produce incidents, and the boring ones are the ones that run for years without anyone thinking about them.
FAQ
What is a Terraform workspace?
A workspace is a named container for state, attached to a single configuration. Each workspace keeps its own state file, so the same configuration can manage separate sets of resources — typically development, staging and production. You are always in a workspace; the default is called default.
How do I create and switch workspaces?
terraform workspace new staging creates a workspace called staging and switches to it. terraform workspace select production switches to an existing workspace. terraform workspace list shows all workspaces with the current one marked by an asterisk, and terraform workspace show prints the current workspace name.
How is a workspace different from a module?
A workspace reuses one configuration across copies of the same system, differing only in state and in values looked up by workspace name. A module packages configuration for reuse across different systems. They compose: a workspace-based setup usually calls a module with workspace-dependent inputs.
When should I not use workspaces?
When the environments differ in structure, when they need different access controls, when the team is large enough that a wrong-workspace apply is a real risk, when different providers or provider versions are needed per environment, or when the problem is state size or locking contention rather than environment separation. In those cases use separate directories, or HCP Terraform workspaces with their permission model.
What is the difference between a CLI workspace and an HCP Terraform workspace?
A CLI workspace is a state partition within one backend configuration. An HCP Terraform workspace is a managed environment with its own variables, run history, permissions and version-control connection, and its name must be unique within the organisation. When you use the HCP Terraform remote backend, the HCP Terraform workspace is the unit of state, and CLI workspace commands do not apply in the same way.
Conclusion
Terraform workspaces are a small, precise tool: a named container for state behind a single configuration, letting you run the same infrastructure three times — development, staging, production — with different sizes and values and separate state. The commands are five and the mechanism is one value, terraform.workspace, looked up in a map. Used inside their envelope — near-identical environments, a small team, differences of size and value rather than structure — they are the simplest answer to the environments problem, and they have been since workspaces were introduced. Used outside it, they are a way to hide real differences behind conditionals, to give everyone access to production, and to produce the wrong-workspace apply that every team dreads.
The decision rule is the guide in one paragraph: workspaces for same-shape environments that differ in size and values; separate directories when the environments differ in structure, providers or access; modules in both cases for the shared code; and HCP Terraform workspaces when you need a permission model, a run history and a VCS workflow, at the price of a subscription. And whatever you choose, run Terraform from CI with the workspace as a named parameter, keep a written record of which workspace is which environment, and run terraform workspace show before every apply — because the cheapest production incident is the one you never have. For the foundations, our Terraform Tutorial for Beginners covers the workflow, our Terraform State Explained covers backends and locking, and our Terraform Modules Tutorial covers the modules this guide's pattern is built on.
Sources checked 30 Nov 2026: GitHub hashicorp/terraform releases (1.17.2, 11 Nov 2026; 1.17.1, 28 Oct 2026; 1.17.0, 21 Oct 2026; Terraform Policy generally available in 1.17.0); HashiCorp Developer HCP Terraform workspaces documentation (a workspace is a group of infrastructure resources managed by Terraform; each workspace belongs to a project; creation via UI, workspaces API, tfe provider or no-code provisioning; deleted workspaces recoverable within 30 days with recovery enabled and a paid subscription; workspace names 90 characters or fewer recommended); Terraform CLI workspace commands via command references checked 30 Nov 2026 (workspace list, show, new, select, delete, delete -force; terraform.workspace in configuration; install page showed 1.17.1 as latest on 27 Nov 2026, superseded by the 1.17.2 release listing of 11 Nov 2026). NOT VERIFIED on an official page and therefore not stated as fact above: the HCP Terraform legacy free tier end date of 31 March 2026 (reported by a 2026 guide, not verified on HashiCorp's pricing page — stated in this article only as a reported claim, with a direction to check the pricing page); HCP Terraform bundle prices and any per-workspace pricing (not stated). External references: HashiCorp Developer — Create workspaces in HCP Terraform · GitHub — hashicorp/terraform releases.
—



