🎓 A1Learner
🔍
0 of 50 lessons completed

Workspaces vs Environment Directories

Environment directories are folders that hold a full copy of a configuration for each environment. They solve the same problem as workspaces in a different way.

One configuration, two workspaces

The workspace name picks the size from a map.

locals {
  size = { dev = "small", prod = "large" }[terraform.workspace]
}

One folder per environment

Each folder holds a full copy of the configuration.

environments/
  dev/
    main.tf
  prod/
    main.tf

When directories win

Prod needs a resource that dev never has. A workspace cannot add it cleanly.

# prod/main.tf holds an extra resource.
# dev/main.tf does not.
resource "local_file" "audit" {
  content  = "Audit log."
  filename = "${path.module}/audit.txt"
}

Example

# main.tf
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.0"
    }
  }
}

locals {
  size = { dev = "small", prod = "large" }[terraform.workspace]
}

resource "local_file" "note" {
  content  = "Size: ${local.size}."
  filename = "${path.module}/note-${terraform.workspace}.txt"
}

# commands
terraform init
terraform workspace new dev
terraform apply -auto-approve
cat note-dev.txt
terraform workspace new prod
terraform apply -auto-approve
cat note-prod.txt

Key points

  • Workspaces are quick. One configuration serves every environment.
  • Directories let each environment differ freely. Each folder holds its own files.
  • Workspaces suit small differences. A value or a name changes, and the rest stays the same.
  • Directories scale when environments diverge. Prod can hold resources that dev never needs.
  • A workspace and a directory can mix. A folder per environment can still use workspaces inside.

The workspace name picks the size from a map, so one configuration serves both environments. This works while the difference is only a value. When prod needs a whole extra resource, a folder per environment is the cleaner choice.

Note

moving from workspaces to directories is a rewrite, not a switch. Choose early.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

When do environment directories beat workspaces?