BlogIaC

Terragrunt: keeping 30 environments DRY

Stop copy-pasting backend and provider blocks across environments — generate them and keep inputs in one place.

Nov 19, 2024·11 min readAdvanced

At thirty environments, plain Terraform means thirty near-identical backend and provider blocks that drift the moment someone edits one. Terragrunt generates the boilerplate from a single root file and keeps only the per-environment inputs in each leaf — DRY without magic.

Define the backend once

root terragrunt.hcl
remote_state {
backend = "s3"
generate = { path = "backend.tf", if_exists = "overwrite" }
config = {
bucket = "acme-tfstate"
key = "${path_relative_to_include()}/terraform.tfstate"
region = "eu-west-1"
encrypt = true
}
}

Each environment is just inputs

live/prod/app/terragrunt.hcl
include "root" { path = find_in_parent_folders() }
terraform { source = "git::git@github.com:acme/modules.git//app?ref=v2.1" }
inputs = {
instance_count = 6
environment = "prod"
}
One backend definition, many states
The generated key uses the folder path, so every environment gets an isolated state file automatically — no copy-paste, no collisions.

Operate the whole tree

bash — plan everything at oncelive
terragrunt run-all plan
live/dev/app no changes
live/staging/app + 1 to add
live/prod/app no changes
Go deeper in a courseTerragruntDRY Terraform at scale — generate config, run-all, dependencies.View course

Related posts