1. Several environments #
Folders, tfvars or workspaces.
Comparison #
| Approach | Advantages | Limits |
|---|---|---|
| Folder per environment | Strong isolation, separate backends | Some duplication (reduced by modules) |
| tfvars files | One codebase, values per environment | Same backend: be careful |
| CLI workspaces | Several states for the same code | Easy 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.tfvars2. Splitting into layers #
Smaller states.
Layers #
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.
