Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
infra/
modules/
network/ app/ database/
envs/
dev/ main.tf backend.tf terraform.tfvars
staging/ main.tf backend.tf terraform.tfvars
prod/ main.tf backend.tf terraform.tfvarsEach envs/X folder has its own backend (state) and values, and calls the same modules.
Step by step
- Compare the options:
| Way | Good | Watch out |
|---|---|---|
| Folders per env | Clear, each env can differ, separate backends/credentials, easy CI per folder. | Some repetition (keep it small with modules). |
Workspaces (terraform workspace) | Same code, quick copies (feature envs). | Same backend and credentials; easy to apply in the wrong workspace. |
| Terragrunt | DRY backends/inputs for many envs/regions. | Another tool to learn and run. |
For production most teams choose folders (or Terragrunt for very many).
- One environment folder:
module "network" {
source = "../../modules/network"
cidr = "10.20.0.0/16"
env = "prod"
}
module "app" {
source = "../../modules/app"
env = "prod"
subnet_ids = module.network.private_subnet_ids
instance_type = "m6i.large"
}terraform {
backend "s3" {
bucket = "acme-tfstate-prod"
key = "infra/terraform.tfstate"
region = "eu-central-1"
use_lockfile = true
}
}- Separate accounts/subscriptions — the strongest wall: dev and prod in different AWS accounts (or Azure subscriptions), each with its own state bucket and its own CI role. A dev credential then can not change prod.
- Promote changes: change a module → apply in
dev→staging→prod, each folder with its own plan and approval. Pin module versions per environment if they move at different speeds. - Workspaces when they fit (short-lived feature environments):
terraform workspace new feature-login
terraform workspace show
terraform plan -var-file=feature.tfvarsShow the workspace in your prompt and in CI logs; never use workspaces for prod/dev separation with the same credentials.
Check that it worked
terraform state list in envs/dev and envs/prod shows different resources, each folder's backend points to a different state (and account), and a plan in dev can not see or change prod resources.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Apply in the wrong environment | Workspaces/credentials shared. | Folders + separate accounts and CI roles per env. |
| Copy-paste between env folders | Logic in env folders. | Move logic into modules; envs only pass values. |
| Prod changed before staging was tested | Shared module on a branch. | Pin module versions per env; promote step by step. |
Backend configuration changed when switching | One folder used for several backends. | One folder per env (no switching). |
| Envs drift apart over time | Manual changes, different module versions. | Drift checks; regular promotion of the same versions. |
| Too many folders for many regions | Repetition grows. | Terragrunt or a small generator for backends. |
Go further
- Command pages: terraform, terragrunt.
- Next how-tos: Use Terraform variables per environment, Write a Terraform module, Store Terraform state in S3 with locking.
- Learn it: Terraform — Workspaces in HCP, Remote state, A full Azure project layout, Environments.
- Full guides: Terraform, Terragrunt.
1Why do many teams use folders instead of workspaces for dev and prod?
Each folder has its own backend, values and credentials, so applying in the wrong environment is much harder.
2What is the strongest separation between dev and prod?
Different cloud accounts/subscriptions with separate state and separate CI roles.