For one team, Terragrunt is mostly a way to stop repeating backend and provider configuration. For many teams on one AWS platform, the hard questions are different. Where does each piece of code live? Who can change what? How does a fix in a shared building block reach every environment without breaking any of them?
This article describes a layout that answers those questions. It is the one behind a Terragrunt platform our lead engineer built for the domain DevOps teams of an online gaming and crypto group: the outcome and the CI and access model are covered in separate articles. The examples here are generic, plus a few patterns from our own Terragrunt repository.
Three layers, three kinds of repository
modules/ platform team · plain Terraform · tags v1.4.0, v1.5.0, ...
eks/
dns-zone/
argocd/
stacks/ platform team · Terragrunt units · tags v3.2.0, v3.2.1, ...
units/
account-bootstrap/terragrunt.hcl
eks/terragrunt.hcl
dns/terragrunt.hcl
argocd/terragrunt.hcl
monitoring/terragrunt.hcl
security/terragrunt.hcl
live-payments/ one per domain team · values and versions only
root.hcl
stage/terragrunt.stack.hcl
prod/terragrunt.stack.hcl
Modules are plain Terraform: resources, variables, outputs. They know nothing about Terragrunt, backends or teams, so they can be tested on their own.
Stacks wire modules into the pieces a team actually needs. Each unit pins the module version it was tested with and declares how it connects to other units. This is the layer that carries the contracts.
Live repositories belong to domain teams. They describe environments with values and pinned stack versions. If a live repository needs a resource block for something every team needs, that is a missing feature in the stacks.
Separate repositories give each layer its own owner, its own review rules and its own release tags. A monorepo can work too, but path-scoped tags and permissions take more effort than three kinds of repository.
What a contract between stacks is
A contract has three parts.
Inputs. What a unit accepts, with types, defaults and validation, so that a wrong value fails at plan time with a clear message instead of somewhere inside AWS:
variable "cluster_name" {
type = string
description = "EKS cluster name, also the prefix for related resources"
validation {
condition = can(regex("^[a-z][a-z0-9-]{2,38}$", var.cluster_name))
error_message = "Use 3-39 lowercase letters, digits and dashes, starting with a letter."
}
}
Outputs. What other units may rely on, and nothing else. If the Argo CD unit needs the cluster endpoint, the EKS unit outputs it. The Argo CD unit does not look the cluster up by name and does not read another unit’s state.
Version. The contract changes only with a release, and a breaking change means a new major version. The CI article lists what counts as breaking.
In Terragrunt, the consuming side of a contract is a dependency block:
# stacks/units/argocd/terragrunt.hcl
include "root" {
path = find_in_parent_folders("root.hcl")
}
terraform {
source = "git::https://gitlab.example.com/platform/modules.git//argocd?ref=v1.5.0"
}
dependency "eks" {
config_path = "../eks"
mock_outputs = {
cluster_name = "mock-cluster"
cluster_endpoint = "https://mock.invalid"
cluster_certificate_authority_data = "bW9jaw=="
}
mock_outputs_allowed_terraform_commands = ["init", "validate", "plan"]
}
inputs = {
cluster_name = dependency.eks.outputs.cluster_name
cluster_endpoint = dependency.eks.outputs.cluster_endpoint
cluster_ca_data = dependency.eks.outputs.cluster_certificate_authority_data
hostname = values.hostname
}
The contract is wider than the variables. This unit also expects a root.hcl somewhere above it that configures state, and a sibling unit at ../eks. Write those assumptions down in the stack’s documentation: they bind consumers just as much as the inputs do.
There are looser alternatives: data source lookups by name or tag, or outputs published to SSM Parameter Store. They decouple units from each other, but the contract becomes invisible, and nothing in the code tells you that renaming a tag breaks another team. When one platform team maintains all the units, explicit dependency blocks are easier to reason about.
The live repository
A team’s environment is a terragrunt.stack.hcl file: a list of units, each with a source pinned to a stacks release and its own values.
# live-payments/prod/terragrunt.stack.hcl
unit "eks" {
source = "git::https://gitlab.example.com/platform/stacks.git//units/eks?ref=v3.2.1"
path = "eks"
values = {
cluster_name = "payments-prod"
}
}
unit "argocd" {
source = "git::https://gitlab.example.com/platform/stacks.git//units/argocd?ref=v3.2.1"
path = "argocd"
values = {
hostname = "argocd.payments.example.com"
}
}
terragrunt stack generate turns this file into ordinary units under .terragrunt-stack/, a generated directory that stays out of git, and terragrunt stack run plan plans them in dependency order. Inside a unit, the values are available as values.<name>, as in the Argo CD unit above.
The root of the live repository holds what every unit shares. Here is the backend part of the root.hcl from our own repository, trimmed. Our state lives in Cloudflare R2, which speaks the S3 API, hence the endpoint. On AWS, the same block has a real region and no endpoint:
# root.hcl (trimmed): one backend definition for every unit below it
generate "backend" {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
contents = <<-EOF
terraform {
backend "s3" {
bucket = "${local.state_bucket}"
key = "${path_relative_to_include()}/terraform.tfstate"
region = "auto"
endpoints = { s3 = "${local.state_endpoint}" }
use_lockfile = true
}
}
EOF
}
Two details are worth copying. The key comes from the unit’s path, so every unit gets its own state file without anyone thinking about it. And use_lockfile uses S3 native locking (Terraform 1.10 and later), so there is no DynamoDB table to manage. We tested that R2 refuses a second lock file with HTTP 412 and that a plan stops on someone else’s lock. On AWS, Terragrunt’s remote_state block can also create the bucket for you; we generate the backend block ourselves because R2 does not support the bucket settings Terragrunt tries to apply.
Shared defaults go into one file that units include. All our Cloudflare zones take their TLS, HSTS and cache settings from a common file, and each zone overrides only what differs:
include "root" {
path = find_in_parent_folders("root.hcl")
merge_strategy = "deep"
}
include "common" {
path = "${get_repo_root()}/live/_common/cloudflare-site.hcl"
merge_strategy = "deep"
}
On a multi-team platform the same mechanism carries organization-wide defaults, such as tags and regions.
How teams consume stacks
- A new environment is a new
terragrunt.stack.hclwith values, written by following the Quick Start. No code is copied. - An upgrade is a merge request that changes the tag, first in stage, then in prod. Its plan shows what the new version does to this environment.
- Something the stacks don’t cover can live as the team’s own units in its live repository, consuming platform outputs through
dependencyblocks. What a team should not do is patch a platform unit locally: the fix belongs in the stacks repository, where every team gets it.
Pitfalls
Mock outputs. A dependency on a unit that has never been applied has no outputs, so plan fails without mocks. Three rules keep mocks safe:
- Allow them only for
init,validateandplan, never forapply. A missing output should stop an apply. - Make them valid for the types the module expects. A mock that does not look like a real ARN or URL can fail validation in the provider.
- When you add an output to a unit that is already applied, its consumers cannot plan until the new output exists.
mock_outputs_merge_strategy_with_state = "shallow"fills the missing keys from the mocks.
One limit no setting fixes: a unit whose providers connect to the cluster, like Kubernetes or Helm, cannot plan against a mock endpoint. For a brand-new environment, plans of those units mean something only after the cluster exists, so apply in dependency order and plan them again.
State per unit, and what it means for refactoring. Per-unit state keeps the blast radius small and lets teams plan in parallel without waiting on each other’s locks. The catch is that the state key comes from the path. Rename a directory or change a unit’s path in the stack file, and Terraform sees an empty state and plans to create everything again. Move the state object to the new key before the first plan. Inside one state, moved blocks handle renames. In our own repository, DNS records that switched from one resource to another moved without being recreated:
moved {
from = cloudflare_dns_record.tunnel["example.com"]
to = cloudflare_dns_record.pages["example.com"]
}
Between two states, use a removed block with destroy = false in the old unit and an import block in the new one (Terraform 1.7 and later).
Version drift. There are two kinds. Release drift: one team is on v3.2, another is still on v1.8, and every fix has to be backported. Decide how many major versions you support, and keep the versions in use visible:
# which stack versions the live repositories use
grep -rhoE --include='*.hcl' 'ref=v[0-9]+\.[0-9]+\.[0-9]+' live-*/ | sort | uniq -c
Configuration drift: someone changes a resource in the console. A scheduled pipeline that runs plan -detailed-exitcode (exit code 2 means there are changes) catches it before the next merge request runs into it.
Too many small units. Terragrunt reads the outputs of every dependency on every plan, and a long chain of tiny units makes plans slow and failures hard to trace. Cut units along what changes together and who owns it, not one unit per resource type.
When plain Terraform is enough
Terragrunt adds a tool, its own configuration files and a layer of indirection. It pays off with many states, several teams and real dependencies between parts. You probably do not need it if:
- one team owns all the infrastructure;
- there are two or three environments, each a root module that calls shared modules with pinned versions;
- a backend block repeated in a few places bothers nobody.
Our own repository sits in between. It has about a dozen units for Cloudflare zones, GitHub organizations, backups and an uptime monitor, all managed by one person. Terragrunt gives us one backend definition, secrets read from a SOPS-encrypted file, and run --all plans with a summary of changes. We do not use stacks or dependency blocks there: the units are independent, and adding them would be structure without a problem to solve.
Checklist
- Modules are plain Terraform, released with semver tags
- Units pin module versions; live repositories pin stack versions
- Every contract is written down: inputs, outputs and assumptions such as paths
- One state per unit, keyed by path; renames are planned as state moves
- Mock outputs only for
init,validateandplan - The versions in use are visible, with a clear policy on which are supported
- Drift detection runs on a schedule
A layout like this is mostly a set of agreements written as code. Terragrunt helps enforce them, and the agreements are what let many teams share one platform without getting in each other’s way.
