RousselTMLEARNING

Automation

Terraform multi-environment, workspaces and repository organization

Organize Terraform for several environments: one folder per environment, tfvars files, CLI workspaces and their limits, splitting into layers (network, data, applications), reading neighboring states and Terragrunt.

1. Several environments #

Folders, tfvars or workspaces.

Comparison #

ApproachAdvantagesLimits
Folder per environmentStrong isolation, separate backendsSome duplication (reduced by modules)
tfvars filesOne codebase, values per environmentSame backend: be careful
CLI workspacesSeveral states for the same codeEasy to pick the wrong workspace; no permission isolation

Example #

envs/
  recette/  main.tf  backend.tf  recette.tfvars
  prod/     main.tf  backend.tf  prod.tfvars
modules/
  reseau/ app/ base/

cd envs/prod && terraform plan -var-file=prod.tfvars

2. Splitting into layers #

Smaller states.

Layers #

Infrastructure layers
An application change cannot destroy the network by mistake.
data "terraform_remote_state" "reseau" {
  backend = "s3"
  config  = { bucket = "etat-terraform-123", key = "reseau/prod.tfstate", region = "eu-west-3" }
}

module "app" {
  source     = "../../modules/app"
  subnet_ids = data.terraform_remote_state.reseau.outputs.private_subnets
}

3. Complementary tools #

Reducing repetition.

Tools #

  • Terragrunt: DRY configuration of backends and dependencies between layers.
  • HCP Terraform: remote workspaces with variables, permissions and runs per team.
  • Reading outputs through data sources (SSM parameters, tags) rather than terraform_remote_state to reduce coupling.

Related articles

Terraform: principles, architecture and workflow

Understand Terraform: declarative infrastructure as code, architecture (core, providers, state), the init, pl…

The HCL language: resources, variables, outputs, data and expressions

Writing Terraform: terraform, provider, resource and data blocks, typed variables with validation, locals, ou…

Terraform state: remote backends, locking, import and refactoring

Manage Terraform state: the role of the state file, sensitive data, remote backends (S3, azurerm, gcs, HCP Te…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!