Skip to main content

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.

Bash
terraform workspace new staging
terraform workspace select prod
terraform workspace show

Use terraform.workspace in code to vary settings. Workspaces fit PR preview environments and developer sandboxes.

Common Mistake

Separating 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.

Remember

Terraform 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.

Found a mistake, an outdated step or a broken link?