Home DevOpsTerraform Workspaces: Manage Dev, Staging, and Production

Terraform Workspaces: Manage Dev, Staging, and Production

by Knowledge Base Linux
Terraform workspaces illustration

Terraform workspaces let you manage several environments from a single codebase by giving each environment its own state file. Instead of copying a whole Terraform project for dev, staging, and production, you keep one set of configuration files and switch workspaces. Terraform workspaces then track which resources belong to which environment, so a change applied in dev does not touch production state by accident.

This guide explains when Terraform workspaces are the right tool, how to create and switch workspaces, how to vary configuration per workspace, and where workspaces stop being enough and separate configurations or modules become the better choice. The examples use Terraform 1.5 or later and a simple AWS-style example, but the workspace mechanics are identical on other providers.

Prerequisites for Terraform Workspaces

You need Terraform installed, a working configuration that already applies cleanly in one environment, and a remote backend configured. Workspaces work with local state too, but remote state is strongly recommended, because each workspace gets its own state object in the backend and local state files are easy to lose or confuse. Verify your setup with terraform init and terraform workspace show, which prints default before you create any workspace.

You should also understand Terraform state at a basic level: state is the mapping between your configuration and real resources. Workspaces do not copy configuration; they only separate state. If you change a resource definition, that change applies to whichever workspace is currently selected, so always confirm the active workspace before you run apply.

Step 1: Create and Switch Terraform Workspaces

Create a workspace with terraform workspace new staging. Terraform creates a new state for that workspace and switches to it immediately. List workspaces with terraform workspace list, switch with terraform workspace select staging, and check the current workspace at any time with terraform workspace show. Make that check part of your routine before every plan and apply, because applying in the wrong workspace is the most common workspaces mistake.

terraform workspace new dev
terraform workspace new staging
terraform workspace new prod
terraform workspace list
terraform workspace select staging
terraform plan
terraform apply

With an S3 backend, each workspace state lives under a separate key prefix, such as env:/staging/path/to/state. You do not need to configure that prefix yourself; Terraform manages it. With other backends, the mechanism differs but the idea is the same: one backend, one state per workspace. Confirm the backend behaviour in your provider documentation before you rely on workspaces for production.

Deleting a workspace is safe only when it is empty. Run terraform workspace select staging, destroy its resources if any exist, then switch to another workspace and run terraform workspace delete staging. Terraform refuses to delete the currently selected workspace and refuses to delete a non-empty workspace without a destroy, which protects you from accidentally removing state that still maps to live resources.

Step 2: Vary Configuration Per Workspace

Workspaces are useful only if environments can differ in size or settings. Use the terraform.workspace variable to branch on the current workspace name. A common pattern is a map of instance sizes or counts keyed by workspace, with a lookup that falls back to a safe default. Keep the differences small and explicit, because a configuration that behaves completely differently per workspace is hard to review.

variable "instance_counts" {
  type = map(number)
  default = {
    dev     = 1
    staging = 2
    prod    = 3
  }
}

resource "aws_instance" "app" {
  count         = lookup(var.instance_counts, terraform.workspace, 1)
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = terraform.workspace == "prod" ? "t3.medium" : "t3.micro"
}
Terraform workspaces environments illustration

For larger differences, such as different CIDR blocks, domain names, or feature flags, use a map variable per setting rather than long conditional chains. A locals block that resolves the current environment settings at the top of the configuration makes the rest of the code readable. Avoid using workspaces to hide completely different architectures, because reviewers cannot tell from a plan in one workspace what will happen in another.

Step 3: Know When Not to Use Terraform Workspaces

Workspaces are a good fit when environments share the same architecture and differ mainly in size, count, or naming. They are a poor fit when environments have different providers, different backends, different credentials, or different compliance requirements. In those cases, separate root configurations or separate Terraform projects per environment are clearer, even though they involve some duplication, because each environment can then evolve independently.

Another limit is access control. Workspaces do not provide per-workspace permissions in the open-source version of Terraform; anyone who can apply in dev can usually apply in prod if they can switch workspaces. If you need strict separation between production and non-production operators, use separate state, separate credentials, and separate CI pipelines per environment, and treat workspaces as a developer convenience, not a security boundary.

Finally, workspaces do not version configuration per environment. If production needs an older module version while dev tests a newer one, a single codebase with workspaces cannot express that. Pin module versions per environment in separate configurations instead, and use workspaces only where the same version is correct everywhere.

Troubleshooting Terraform Workspaces

If a plan shows unexpected changes, first run terraform workspace show and confirm you are in the intended workspace. A plan that looks like it wants to recreate everything usually means you are in a new or empty workspace whose state does not contain the existing resources. Do not apply that plan until you understand why the state is empty, because applying it can create duplicate resources.

If a workspace cannot be deleted, check that you are not currently in it and that its state is empty. Run terraform state list in that workspace to see what remains. If state list shows resources that no longer exist in the cloud, you may need to remove them from state explicitly, but only after confirming in the provider console that the resources are truly gone.

For related infrastructure patterns, see our guides to reusable Terraform modules and Terraform remote state with an S3 backend. The official reference is the Terraform workspaces documentation.

Conclusion

Terraform workspaces are a simple, effective way to run dev, staging, and production from one codebase when the architectures match. Create one workspace per environment, vary only size and settings through maps, always verify the active workspace before applying, and move to separate configurations when environments diverge in providers, credentials, or compliance needs. Used with those limits in mind, workspaces reduce duplication without hiding the differences that matter.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.