Allow you to store your Terraform state in multiple, separate, named workspaces.

Terraform starts with a single workspace called default.

terraform workspace CLI commands are used to control workspaces.

Backend files structure

When using workspaces, Terraform will create a directory end:/

And inside this directory the name of the workspace will be prefixed before specified backend key value:

This way, if the configuration of the Terraform s3 backend is specified as:

terraform {
  backend "s3" {
    bucket         = "galaxy-portal-be-tf-state"
    key            = "services/users/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "galaxy-portal-be-tf-state-locks"
    encrypt        = true
  }
}

The path to the Terraform state file for the dev workspace will be: env:/dev/services/users/terraform.tfstate

Create new workspaces

terraform workspace new dev
terraform workspace new prod

Switch between workspaces

terraform workspace select dev
terraform workspace select prod

Access workspace name in the configuration

Use terraform.workspace variable.

resource "aws_s3_bucket" "report_generator_bucket" {
  bucket = "${terraform.workspace}-report-generator-bucket"
}