Workspaces and what they are not
A CLI workspace is a second copy of state in one directory with one backend and one set of credentials. Here is when that is enough and when it is not.
Remote backends: S3, GCS and azurermRepository layouts for Terraform
Workspaces and what they are not
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A CLI workspace is a named state file inside one working directory, sharing the same backend and credentials.
- On S3 a non-default workspace's state lives under env:/<workspace>/<key>, in the same bucket as default.
- Workspaces do not separate access, so they are weak environments for dev, staging and production. The Terraform docs recommend separate configurations with separate backends for that.
- They suit short-lived parallel copies for testing a change, as long as you destroy them afterwards. HCP Terraform workspaces are a different, larger concept.
The word that means two things
You inherit a repo with three environments, and someone tells you “we use workspaces.” Two engineers can say that and mean different things, and one of them is describing a safe setup while the other is describing a trap.
A CLI workspace is a named copy of state inside one working directory. You run terraform workspace new staging, and the same configuration now has a second state file. That is the whole feature. An HCP Terraform workspace is a much bigger thing: its own configuration source, variables, state, run history, credentials and access rules, closer to a separate working directory than to a state copy. HashiCorp’s CLI docs draw exactly this line. This lesson is about the CLI one, because it is the one people mistake for an environment system. The managed one comes back in HCP Terraform and managed platforms.
What a CLI workspace does
Every initialised directory starts with a workspace called default. The commands are small:
terraform workspace list
terraform workspace new staging
terraform workspace select staging
terraform workspace show
terraform workspace delete staging
Only one workspace is selected per directory at a time, and the selection is stored locally in the .terraform directory, which you do not commit. The docs describe a workspace as technically equivalent to renaming your state file. Inside the configuration, the expression terraform.workspace gives you the current name, so you can write terraform.workspace == "prod" ? 3 : 1 for a count.
Where the state ends up depends on the backend. The S3 backend docs say non-default workspaces are stored at <workspace_key_prefix>/<workspace_name>/<key>, with the prefix defaulting to env:. I wrote that rule out as a few lines of Python to see what a bucket looks like. This is the documented rule applied by hand, not Terraform’s output:
key = "app/terraform.tfstate"
default -> app/terraform.tfstate
staging -> env:/staging/app/terraform.tfstate
prod -> env:/prod/app/terraform.tfstate
Same bucket, same lock mechanism, same credentials. That is the point to hold on to.
Where it stops being an environment
Think of an office with one filing cabinet and three drawers labelled dev, staging and prod. The drawers keep the paper apart, and the cabinet has one key. Whoever can open the cabinet can open every drawer.
Run the checklist for “environment” against that cabinet:
- Separate access. Everyone who can run Terraform against the backend can reach every workspace’s state. The state of prod is readable by the person who only needed dev, and state can hold secrets. The S3 permissions needed for workspaces are on the bucket prefix, not per workspace, unless you write finer IAM policy by hand.
- Visible difference. Dev and prod differ by one word that is not in any file. In a pull request, the diff shows the code and says nothing about which workspace will receive it.
- A deliberate step to touch prod. The thing between you and an apply to production is which workspace happened to be selected when you opened the terminal. HashiCorp’s docs say workspaces “are not designed to solve all problems” and warn against using them where deployments need different access controls. They recommend separate configurations, each with its own backend, for that case.
- Different shapes. If prod has a read replica and dev does not, the configuration fills up with
terraform.workspace == "prod" ? ... : ..., and the conditionals are the bug surface.
The OpenTofu docs say the same: workspaces are a way to test changes, not an isolation mechanism.
What they are good for
A temporary parallel copy. You are changing a networking module and want to see it work without touching the shared dev environment:
terraform workspace new my-change
terraform apply -var-file=dev.tfvars # a full copy, in your own state
# ...test...
terraform destroy
terraform workspace select default
terraform workspace delete my-change
The docs describe the same pattern for feature-branch testing. Two things make it safe enough: the copy needs its own names for anything that must be globally unique, and you actually destroy it afterwards. Putting terraform.workspace into resource names handles the first, and is the one use of the expression I would defend.
The trade you are actually making
Workspaces are cheap now and expensive later. They need no new directories and no backend per environment, and the first week feels fast. The cost shows up when the second team joins, when you need prod credentials that dev engineers do not have, or when an audit asks who could read production state. At that point the move is to a directory per environment, and the migration is a state move per workspace, not a rename.
About the video
The embed is Ned in the Cloud’s “Terraform Workspaces Are Bad Actually, And Here’s Why.”, 20 minutes 32 seconds, April 2023. Its description argues that CLI workspaces in open-source Terraform struggle with promoting changes between environments, protecting state data and giving developers and pipelines least-privilege access, and also says workspaces are not always bad. The title is blunter than the content. I judged it English from title, description and channel and its English auto-caption track. I took its argument from the description, not by watching all of it. It is from 2023, but the CLI workspace behaviour it discusses is unchanged in the current docs I read on 28 November 2026.
The commands and the S3 path rule here are from the Terraform CLI and S3 backend documentation and I did not run Terraform.
Where this fits
If workspaces are a copy and not an environment, the next question is what an environment is. Directories and tfvars is the answer most teams ship.
Check yourself
What does a CLI workspace share with the other workspaces in the same directory?
The configuration, the backend and the credentials. Only the state file is separate.
Why is the selected workspace a weak guard for production?
It is local state you can forget about, it appears in no file or diff, and anyone who can use the backend can reach every workspace's state.
When is a CLI workspace a good tool?
A temporary parallel copy of infrastructure for testing a change, with names made unique by terraform.workspace and a destroy at the end.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.