Thuta Learning
IntermediateDevOpsintermediate

Workspaces

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Workspaces without any of the intimidation
  • Get hands-on running terraform commands and HCL code yourself
  • Apply this concept in a real project right away

Let's think about it this way for a second

Modules (from the previous lesson) are about code reuse, while Workspaces are about state isolation — from the very same config folder, running `terraform workspace new dev` and `terraform workspace new prod` creates two workspaces, and Terraform stores their state files separately (`terraform.tfstate.d/dev/`, `terraform.tfstate.d/prod/`) — applying in the dev workspace won't touch a single resource in the prod workspace. You can reference the built-in `terraform.workspace` variable in your config yourself, and vary a value depending on the workspace.

Let's connect this to a real scenario

With the sequence `terraform workspace new staging` → `terraform workspace select staging` → `terraform apply`, you can run the staging environment separately from the default environment — you can also vary a value by workspace with an expression like `instance_type = terraform.workspace == "prod" ? "t3.large" : "t3.micro"`.

Let's look at it together

bash
# Create and switch to a new workspace
terraform workspace new staging

# List all workspaces
terraform workspace list

# Switch back to default
terraform workspace select default

# See which workspace is currently active
terraform workspace show
You should see
$ terraform workspace list
  default
* staging
  production

5-minute try-it

Create a new workspace with `terraform workspace new dev`, then try applying the `local_file` resource (from the Basic chapter) — switch back to the `default` workspace and confirm the state is split.

A quick word of caution

All workspaces share the same backend config (state storage location) — access control can't be split at the workspace level, so every developer may still have access to the production workspace.

Easy traps

  • Thinking of workspaces as a replacement for modules (as if they're just another way to split environments) — most teams prefer modules plus a separate root config, and reserve workspaces for lighter-weight cases like feature-branch testing
  • Forgetting you switched workspaces and applying against the wrong one — always confirm with `terraform workspace show`

Now try it yourself

Create a new workspace with `terraform workspace new dev`, then try applying the `local_file` resource (from the Basic chapter) — switch back to the `default` workspace and confirm the state is split.

You'll know it worked when: $ terraform workspace list default * staging production