Home DevOpsTerraform Workspaces: Manage Multiple Environments

Terraform Workspaces: Manage Multiple Environments

by Knowledge Base Linux
Terraform workspaces provisioning development, staging, and production environments from one blueprint

You have one Terraform configuration and three environments — development, staging, and production — that should be nearly identical but must never share state. Copying the whole configuration three times is the usual first answer, and it is also how environments drift apart. Terraform workspaces solve this cleanly: one configuration, one backend, and a separate state file per workspace, so the same code provisions each environment independently.

Terraform workspaces splitting one state into parallel environment branches

This guide shows how to create and switch workspaces, use the workspace name inside your configuration, and decide when workspaces are the right tool versus separate directories. If you are new to Terraform on AWS, start with our Terraform first steps guide, and for remote state fundamentals, see our Terraform S3 backend guide.

Prerequisites

  • Terraform 1.x installed (terraform version should print a version).
  • A configured backend — local state works for learning, but an S3 backend with state locking is the right choice for real teams.
  • AWS credentials or another provider configured, if you will apply against real infrastructure.

Creating and Switching Terraform Workspaces

Terraform always runs in a workspace; the initial one is called default. Create a new workspace and switch to it in one command:

terraform init
terraform workspace new staging
terraform workspace list
terraform workspace show

workspace list marks the current workspace with an asterisk. Switching is just as simple:

terraform workspace select production
terraform workspace select staging

Each workspace keeps its own state. With the S3 backend, Terraform stores states under separate keys derived from the workspace name (for example under an env:/ prefix), so applying in staging cannot touch production resources. That isolation is the whole point — and the reason you must always check terraform workspace show before running apply on shared infrastructure.

Using the Workspace Name in Your Configuration

Workspaces are most useful when the configuration adapts to the current workspace. The terraform.workspace variable holds the active workspace name, and you can use it to vary instance sizes, counts, and tags:

variable "instance_types" {
  type = map(string)
  default = {
    dev     = "t3.micro"
    staging = "t3.small"
    prod    = "t3.medium"
  }
}

locals {
  environment = terraform.workspace == "default" ? "dev" : terraform.workspace
}

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_types[local.environment]

  tags = {
    Name        = "web-${local.environment}"
    Environment = local.environment
  }
}

This pattern keeps the differences explicit and reviewable: a map lookup instead of three copies of the file. Note the guard for default: if someone runs the configuration without creating a workspace, it maps to dev rather than crashing on a missing map key.

You can also vary counts by workspace. A common pattern is one instance in development and two behind a load balancer in production, expressed with a map of numbers and a count argument — still one configuration, still one code review.

Terraform Workspaces in a Team Workflow

On a team, two habits prevent workspace accidents. First, make the workspace visible: print it in your CI logs and in the plan output before every apply, and consider a wrapper script that refuses to apply to prod unless a confirmation flag is passed. Second, protect production state with backend permissions and, for AWS, a DynamoDB lock table, so a mistaken apply cannot run concurrently with a legitimate one.

A typical workflow looks like this:

terraform workspace select staging
terraform plan -out staging.tfplan
terraform apply staging.tfplan
terraform workspace select prod
terraform plan -out prod.tfplan
# Review the plan carefully before applying to production
terraform apply prod.tfplan

Never run terraform apply without a saved plan file against production. The plan step is your last chance to see that the workspace is correct and that the changes are the ones you intended.

Workspaces vs Separate Directories: When to Choose Each

Workspaces are ideal when environments differ only in values: instance sizes, counts, regions, tags. They become awkward when environments differ in structure — when production has a multi-AZ database cluster and development has a single instance, or when different teams own different environments with different access rules. In those cases, separate directories (or separate root modules) with shared child modules are cleaner, because structural differences stay visible instead of hiding behind conditionals.

HashiCorp’s own guidance makes the same distinction: workspaces are for multiple deployments of the same configuration, while fundamentally different configurations deserve separate roots. A practical rule: if your configuration needs more than a few terraform.workspace conditionals, split it. If it needs only value maps, workspaces are the simpler, better tool.

Troubleshooting Terraform Workspaces

Applied to the wrong workspace. Stop and assess before re-applying. If the change only created extra resources, destroy them from the wrong workspace with terraform destroy after switching to it. If it modified shared resources, restore from the correct workspace’s state and plan. Prevention — printing the workspace in CI and requiring confirmation for production — matters more than any recovery procedure.

“Workspace does not exist” on select. The workspace was never created in this backend, or you are pointed at a different backend. Run terraform workspace list to see what exists, and terraform init if you recently changed backend configuration.

State locking errors. A previous apply was interrupted and left a lock. With the S3 backend, check the DynamoDB lock table, confirm no apply is actually running, then use terraform force-unlock <lock-id> — carefully, because force-unlocking a live apply can corrupt state.

Resources duplicated after switching workspaces. This usually means the configuration does not use the workspace name anywhere, so both workspaces provision resources with identical names in the same account. Add the workspace (or environment) to resource names and tags, as in the example above.

Conclusion

Terraform workspaces give you one configuration and safely separated state per environment, with the workspace name available as a variable for the values that genuinely differ. Use them when environments share structure, split into separate roots when they do not, and always verify the active workspace before applying to production. The full reference is in the Terraform workspaces documentation. Done right, promoting a change from staging to production becomes a workspace switch and a plan review — not a copy-paste exercise with drift built in.

You may also like

KbLinux

Typically replies within a day

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