All guides

Terraform workspaces or directories for environments

A Terraform CLI workspace gives one configuration several states inside one backend. HashiCorp's documentation says where that stops being enough, and it describes a root module per environment as the alternative.

A laptop screen showing JavaScript in a code editor

What a CLI workspace is

Workspaces in the Terraform CLI are separate instances of state data inside the same working directory. They let you switch between instances of a single configuration within its single backend. Terraform starts with one workspace named default, which you cannot delete.

Shell

terraform workspace select -or-create staging
terraform plan -var-file=staging.tfvars
terraform workspace list

The -or-create option creates the workspace if it does not exist. The workspace name and the file name are the example's choice.

Inside the configuration, terraform.workspace returns the name of the current workspace. The documentation's examples use it to change a count and to build a Name tag.

With the S3 backend, key is the path to the state file inside the bucket. For a non-default workspace the state path is <workspace_key_prefix>/<workspace_name>/<key>, and the prefix defaults to env:.

HashiCorp notes that CLI workspaces are different from workspaces in HCP Terraform. Each HCP Terraform workspace has its own configuration, variable values, state data, run history and settings.

Where workspaces stop fitting

HashiCorp's documentation carries this warning: Workspaces do not suit system decomposition, and they do not suit deployments that need separate credentials and access controls.

The CLI documentation explains both halves. On decomposition, it says workspaces alone do not do the job, since every subsystem should get a configuration and a backend of its own. On deployments of the same infrastructure for different development stages or internal teams, it says the CLI workspaces of one working directory share a backend and therefore do not isolate those deployments from each other.

The documentation's use case: a second, separate copy of a set of infrastructure that runs in parallel, created so a set of changes can be tested ahead of any change to production. In its example, a developer creates a workspace for a feature branch and deploys a temporary copy of the main infrastructure into it. Once the change is merged and deployed, the developer destroys the test infrastructure and deletes the workspace.

A directory per environment

The same page gives the alternative. Represent the common elements as one or more reusable modules, and represent each instance as a separate configuration with a different backend. Each configuration's root module then holds two things: a backend configuration and a few module blocks. The arguments of those blocks carry the small differences between the deployments.

This is one layout that follows that description. The directory names are yours to choose.

Text

modules/
  network/
  app/
environments/
  staging/
    main.tf
    backend.tf
    terraform.tfvars
  prod/
    main.tf
    backend.tf
    terraform.tfvars

A root module for production:

HCL

terraform {
  backend "s3" {
    bucket       = "my-org-terraform-state-prod"
    key          = "prod/app/terraform.tfstate"
    region       = "us-east-1"
    use_lockfile = true
  }
}

module "app" {
  source         = "../../modules/app"
  environment    = "prod"
  instance_count = 3
}

The bucket, key, region and module arguments are the example's choice. Each environment has its own backend block, and so its own bucket and key.

Variable files and backend keys

Terraform automatically loads files named terraform.tfvars or terraform.tfvars.json, and files whose names end in .auto.tfvars or .auto.tfvars.json. A file with any other name is passed with -var-file.

A second layout keeps one root module and passes two files per environment:

Shell

terraform init -reconfigure -backend-config=prod.s3.tfbackend
terraform plan -var-file=prod.tfvars

Two files are needed because a backend block cannot refer to named values such as input variables and locals. The backend settings go in a partial configuration file, passed to terraform init with -backend-config=PATH. The documentation recommends the naming pattern *.backendname.tfbackend.

-reconfigure disregards any existing backend configuration and prevents migration of existing state. In a test on Terraform v1.9.8, terraform init with a different backend file and no -reconfigure stopped with the error Backend configuration changed.

Pass the two files as a pair on every run, from a script or a pipeline.

Terraform Stacks

Stacks are a configuration layer in HCP Terraform. Each component block in a Stack includes a Terraform module as its source, and each deployment represents a group of components that work together. Every deployment has an isolated state file. The documentation says deployments let you replicate infrastructure across multiple environments, regions or accounts, and that Stacks are not available on legacy HCP Terraform team plans.

How to choose

  • For deployments that need separate credentials and access controls, use one root module per environment, each with its own backend. That is the documentation's alternative to workspaces.
  • For environments that differ only in values, one root module with a variable file and a backend file per environment is a second option. Let a pipeline pass the pair.
  • For a temporary copy made to test a change, use a CLI workspace. That is the documented use case.
  • Before the first apply in any layout, check which backend and which key the working directory points at.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides