How do you manage multiple environments (dev/staging/prod) in Terraform? Workspaces vs. directory structure.

Hard Topic: Terraform May 24, 2026

Two main approaches:

Terraform Workspaces: Use the same code but switch workspace to change state. Simple, but the same code runs for all environments — hard to have different variable values per environment. Suitable for simple differences.

Separate Directories (recommended): Each environment has its own directory with its own terraform.tfvars and remote state. This is explicit, auditable, and allows environments to diverge safely.

environments/
  dev/
    main.tf → calls shared module
    terraform.tfvars
  staging/
    main.tf
    terraform.tfvars
  prod/
    main.tf
    terraform.tfvars
modules/
  vpc/
  eks/
← Previous What is Terraform state and why must it... Next → What does terraform plan do and why should...

Practice Similar Questions

Back to Terraform Topics