Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 162IaC · Terraform

How to separate environments in Terraform

Dev, staging and production must never share a state — one wrong apply in "dev" must not touch production. There are three common ways to separate them in Terraform: folders per environment (calling shared modules), workspaces, or Terragrunt. This how-to compares them, sets up the folder layout most teams use for production, and adds the account-level separation that really protects prod.

  • 5 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

162.1

The quick answer

layout
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.tfvars
cd infra/envs/prod
terraform init
terraform plan -out=tfplan

Each envs/X folder has its own backend (state) and values, and calls the same modules.

162.2

Step by step

  1. Compare the options:
WayGoodWatch out
Folders per envClear, 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.
TerragruntDRY backends/inputs for many envs/regions.Another tool to learn and run.

For production most teams choose folders (or Terragrunt for very many).

  1. One environment folder:
envs/prod/main.tf
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"
}
envs/prod/backend.tf
terraform {
  backend "s3" {
    bucket       = "acme-tfstate-prod"
    key          = "infra/terraform.tfstate"
    region       = "eu-central-1"
    use_lockfile = true
  }
}
  1. 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.
  2. 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.
  3. Workspaces when they fit (short-lived feature environments):
terraform workspace new feature-login
terraform workspace show
terraform plan -var-file=feature.tfvars

Show the workspace in your prompt and in CI logs; never use workspaces for prod/dev separation with the same credentials.

162.3

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.

162.4

Common problems

ProblemCauseFix
Apply in the wrong environmentWorkspaces/credentials shared.Folders + separate accounts and CI roles per env.
Copy-paste between env foldersLogic in env folders.Move logic into modules; envs only pass values.
Prod changed before staging was testedShared module on a branch.Pin module versions per env; promote step by step.
Backend configuration changed when switchingOne folder used for several backends.One folder per env (no switching).
Envs drift apart over timeManual changes, different module versions.Drift checks; regular promotion of the same versions.
Too many folders for many regionsRepetition grows.Terragrunt or a small generator for backends.

162.5

Go further

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.

Alle commando's in dit hoofdstuk