TExcelTAD

Workspaces

The roadmap asks for "state isolation per environment: workspaces vs folder separation". This lesson covers the first option. A workspace is a named, separate state for the same configuration in the same backend. Every configuration starts in the workspace default; you can create more and switch between them, and the code can read the current name as terraform.workspace.

Workspaces in action

bash · try it yourself
mkdir app && cd app
cat > main.tf <<'HCL'
locals {
  sizes = { default = 1, dev = 1, prod = 3 }
}

resource "local_file" "config" {
  filename = "app-${terraform.workspace}.conf"
  content  = "replicas=${local.sizes[terraform.workspace]}\n"
}
HCL
tofu init >/dev/null
tofu workspace new dev
tofu apply -auto-approve | tail -1
tofu workspace new prod
tofu apply -auto-approve | tail -1
tofu workspace list
tofu workspace show
echo "--- one state per workspace"
find . -name '*.tfstate' | sort
cat app-dev.conf app-prod.conf
echo "--- back to dev: its state only knows its own file"
tofu workspace select dev
tofu state list
tofu state show local_file.config | grep filename
Created and switched to workspace "dev"!

You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "tofu plan" OpenTofu will not see any existing state
for this configuration.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Created and switched to workspace "prod"!

You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "tofu plan" OpenTofu will not see any existing state
for this configuration.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  default
  dev
* prod

prod
--- one state per workspace
./terraform.tfstate.d/dev/terraform.tfstate
./terraform.tfstate.d/prod/terraform.tfstate
replicas=1
replicas=3
--- back to dev: its state only knows its own file
Switched to workspace "dev".
local_file.config
    filename             = "app-dev.conf"

workspace new also switches to the new workspace (the * in the list marks the current one). The local backend keeps non-default states under terraform.tfstate.d/<name>/; the S3 backend under env:/<name>/<key> (workspace_key_prefix).

Command Does
tofu workspace list / show list all / print the current one
tofu workspace new NAME create and switch
tofu workspace select NAME (-or-create) switch
tofu workspace delete NAME delete an empty workspace's state
TF_WORKSPACE=prod select a workspace through the environment (CI)

Where workspaces fall short

Workspaces look like the obvious way to have dev, staging and prod. In practice they are a weak fit for environments:

  • Same backend, same credentials. All workspaces live in one bucket, accessed with one role. The dev pipeline's role can read and write the prod state. Real environments usually live in different AWS accounts, with different backends and roles.
  • Same code, always. You can't roll a change out to dev first: all workspaces share the code on your branch. Differences become terraform.workspace lookups spread across the code.
  • Invisible. Which workspace is selected is local CLI state, not in the code. "I thought I was in dev" is a classic way to change production.
  • Same providers and versions. You can't upgrade the AWS provider in dev only.

The Terraform docs say the same: workspaces are not a good fit when deployments need separate credentials and access controls. Good uses remain:

  • short-lived copies of the same stack: a preview environment per pull request (tofu workspace select -or-create pr-123, apply, destroy and delete when the PR closes);
  • a feature branch's experimental copy of a dev stack;
  • the same configuration deployed per region or per tenant, all with equal access requirements.

For dev/staging/prod, the next lesson's folder layout is the common answer.

Note: HCP Terraform also has "workspaces", which are a different thing: each is a separate configuration run with its own variables, state and permissions, closer to the folders of the next lesson.

Test yourself

What does terraform.workspace return in a new configuration you never switched?

  1. main
  2. default
  3. an empty string
  4. the folder name

Why are CLI workspaces a weak fit for separating prod from dev?

  1. They are slower
  2. All workspaces share one backend and its credentials, and the same code at the same time
  3. They don't support S3
  4. They can't use variables

Which is a good use of workspaces?

  1. Production vs development in different AWS accounts
  2. A throw-away preview environment per pull request
  3. Different provider versions per environment
  4. Storing secrets