TExcelTAD
Home › Infrastructure as Code: Terraform & OpenTofu › One Folder per Environment

One Folder per Environment

The common alternative to workspaces: put the shared infrastructure in a module, and give every environment its own small root configuration in its own folder, with its own backend, variables and provider settings.

infra/
├── modules/
│   └── app/                 # what an environment consists of
│       ├── main.tf
│       ├── variables.tf
│       └── outputs.tf
└── live/
    ├── dev/
    │   ├── main.tf          # module "app" { source = "../../modules/app" ... }
    │   ├── backend.tf       # key = "linkly/dev.tfstate", the dev account's bucket
    │   └── terraform.tfvars
    └── prod/
        ├── main.tf
        ├── backend.tf       # the prod account's bucket and role
        └── terraform.tfvars

What this buys you:

  • Separate state, backend and credentials per environment: the dev pipeline can't touch prod's state.
  • Visible differences. diff live/dev live/prod shows exactly how the environments differ.
  • Staged rollouts. Prod can pin an older module version (?ref=v1.3.0 for a Git source) while dev tries the new one.
  • No hidden "current environment": you are in the folder you are in.

The cost is some repetition (each folder has a backend and a module call), which tools like Terragrunt reduce (Toolkit).

Two environments, for real

-chdir=DIR runs a command as if from another folder, which keeps pipeline scripts simple:

bash · try it yourself
mkdir -p infra/modules/app infra/live/dev infra/live/prod && cd infra
cat > modules/app/main.tf <<'HCL'
variable "environment" {
  type = string
}
variable "replicas" {
  type = number
}
variable "log_level" {
  type    = string
  default = "info"
}

resource "local_file" "config" {
  filename = "${path.root}/app.conf"
  content  = "env=${var.environment}\nreplicas=${var.replicas}\nlog_level=${var.log_level}\n"
}

output "config_file" {
  value = local_file.config.filename
}
HCL
for env in dev prod; do
cat > live/$env/main.tf <<HCL
terraform {
  backend "local" {
    path = "../../state/$env.tfstate"     # stands in for a per-account S3 bucket
  }
}

module "app" {
  source      = "../../modules/app"
  environment = "$env"
  replicas    = var.replicas
  log_level   = var.log_level
}

variable "replicas" {
  type = number
}
variable "log_level" {
  type    = string
  default = "info"
}
HCL
done
echo 'replicas = 1
log_level = "debug"' > live/dev/terraform.tfvars
echo 'replicas = 3' > live/prod/terraform.tfvars
mkdir state
for env in dev prod; do
  tofu -chdir=live/$env init >/dev/null
  tofu -chdir=live/$env apply -auto-approve | tail -1
done
echo "--- the environments differ only where their folders differ"
diff live/dev/terraform.tfvars live/prod/terraform.tfvars
cat live/prod/app.conf
ls state
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
--- the environments differ only where their folders differ
1,2c1
< replicas = 1
< log_level = "debug"
---
> replicas = 3
env=prod
replicas=3
log_level=info
dev.tfstate
prod.tfstate

Each folder got its own state file, and prod used the module's default log_level. In a real setup each backend.tf points at a bucket in that environment's account, and the pipeline assumes that account's role.

The middle way: one root, per-environment files

Some teams keep a single root configuration and select the environment with partial backend configuration and a var file:

infra/
├── main.tf  variables.tf  backend.tf      # backend "s3" {} left empty
└── env/
    ├── dev.s3.tfbackend   dev.tfvars
    └── prod.s3.tfbackend  prod.tfvars
bash
tofu init -reconfigure -backend-config=env/prod.s3.tfbackend
tofu plan -var-file=env/prod.tfvars -out=prod.tfplan

Less repetition, but every environment always runs the same code, and forgetting -reconfigure when switching environments points the wrong state at the wrong variables. Wrap it in a script or Makefile that takes the environment name and does both steps.

Workspaces Folder per environment One root + env files
state isolation same backend full separate keys or buckets
per-env credentials no yes yes, if the pipeline switches roles
staged rollout of changes no yes (module versions) no
repetition none some little
risk of "wrong environment" high low medium

Test yourself

What does tofu -chdir=live/prod plan do?

  1. Copies prod's state into the current folder
  2. Runs plan as if it were started inside live/prod
  3. Switches to the prod workspace
  4. Plans every folder under live/

Which advantage do per-environment folders have over workspaces?

  1. Fewer files
  2. Each environment can have its own backend, credentials and module version
  3. Faster plans
  4. No need for modules

In the one-root layout, what happens if you switch from dev to prod without init -reconfigure?

  1. Nothing; init is automatic
  2. You may plan prod's variables against dev's state (or the CLI asks to migrate state)
  3. The plan is empty
  4. OpenTofu refuses all commands forever