Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
A unit = one folder with a terragrunt.hcl that deploys one module (for example prod/vpc). A root file (root.hcl) holds the shared settings that every unit includes: remote state per unit, provider generation, common inputs. Dependencies pass outputs from one unit to another (VPC ID → database).
| Item | Value |
|---|---|
| Program | terragrunt (one binary from github.com/gruntwork-io/terragrunt/releases) |
| Runs | tofu (default when installed) or terraform |
| Files | root.hcl (shared), terragrunt.hcl (per unit) |
| Cache | .terragrunt-cache/ in each unit (do not commit) |
| Modules | in a separate repo or folder, used by Git tag (?ref=v1.2.0) |
Install on RHEL and Ubuntu
Install OpenTofu or Terraform first. Then download the Terragrunt binary (choose the newest version from the releases page):
TGVER=v0.93.3
curl -fsSLo terragrunt https://github.com/gruntwork-io/terragrunt/releases/download/$TGVER/terragrunt_linux_amd64
curl -fsSLO https://github.com/gruntwork-io/terragrunt/releases/download/$TGVER/SHA256SUMS
grep ' terragrunt_linux_amd64$' SHA256SUMS
sha256sum terragrunt
install -m 0755 terragrunt /usr/local/bin/terragrunt
terragrunt --versionCompare the two checksums. Work tip: tools like mise or tenv can install and pin Terragrunt, OpenTofu and Terraform versions per project.
First configuration: the live layout
live/
├── root.hcl
├── dev/
│ ├── env.hcl
│ ├── vpc/terragrunt.hcl
│ └── db/terragrunt.hcl
└── prod/
├── env.hcl
├── vpc/terragrunt.hcl
└── db/terragrunt.hclThe root file: one state file per unit (by folder path), the backend and the provider are generated for every unit:
locals {
env = read_terragrunt_config(find_in_parent_folders("env.hcl")).locals
}
remote_state {
backend = "s3"
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
config = {
bucket = "acme-tfstate-${local.env.name}"
key = "${path_relative_to_include()}/terraform.tfstate"
region = "eu-central-1"
encrypt = true
use_lockfile = true
}
}
generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
provider "aws" {
region = "eu-central-1"
default_tags {
tags = { env = "${local.env.name}", managed_by = "terragrunt" }
}
}
EOF
}locals {
name = "prod"
}A unit: which module, which version, which inputs:
include "root" {
path = find_in_parent_folders("root.hcl")
}
terraform {
source = "git::git@github.com:ORG/infra-modules.git//vpc?ref=v1.4.0"
}
inputs = {
name = "prod"
cidr = "10.20.0.0/16"
azs = ["eu-central-1a", "eu-central-1b", "eu-central-1c"]
}A unit that needs outputs of another unit:
include "root" {
path = find_in_parent_folders("root.hcl")
}
terraform {
source = "git::git@github.com:ORG/infra-modules.git//postgres?ref=v2.1.0"
}
dependency "vpc" {
config_path = "../vpc"
mock_outputs = {
vpc_id = "vpc-00000000"
private_subnets = ["subnet-00000000"]
}
mock_outputs_allowed_terraform_commands = ["validate", "plan"]
}
inputs = {
vpc_id = dependency.vpc.outputs.vpc_id
subnet_ids = dependency.vpc.outputs.private_subnets
}| Part | Why |
|---|---|
remote_state + path_relative_to_include() | Every unit gets its own state key automatically — no copy-paste of backends. |
generate "provider" | Same provider and tags everywhere. |
source … ?ref=v1.4.0 | Each environment pins a module version; promote a new version from dev → prod by changing the tag. |
dependency + mock_outputs | Order between units, and plan works before the VPC exists. |
Add .terragrunt-cache/ to .gitignore.
Daily use: the commands and the real uses
cd live/prod/vpc
terragrunt plan
terragrunt apply
terragrunt output
cd live/prod
terragrunt run --all plan
terragrunt run --all apply
terragrunt dag graph
terragrunt hcl fmt
terragrunt render --format json(run --all runs every unit below the current folder in dependency order. Older Terragrunt versions used run-all plan and hclfmt.)
Real uses at work:
- Many AWS/Azure accounts and environments with the same modules and small differences.
- Promotion: test module
v1.5.0in dev, then change therefin prod through a pull request. - Small states per unit = faster plans and a smaller blast radius.
Make it safe
Security checklist:
- One state per unit, encrypted, versioned bucket, locking on.
- Separate state buckets (and cloud accounts) for prod and non-prod.
- Pin module versions with Git tags; never
ref=mainin prod. run --all applyin prod only from CI after a reviewed plan.- Short-lived credentials; different roles per environment (for example with
iam_rolein the root config). - Pin Terragrunt and OpenTofu/Terraform versions.
Update and remove
Download the new binary and replace /usr/local/bin/terragrunt. Read the release notes — Terragrunt changes CLI commands between versions. Remove: rm /usr/local/bin/terragrunt and the .terragrunt-cache folders.
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
… has no outputs / dependency error at plan | The dependency unit is not applied yet. | Add mock_outputs for validate/plan, or apply the dependency first. |
Could not find a root.hcl / find_in_parent_folders error | Wrong file name or folder. | Name the file root.hcl (or pass the name); run from inside live/. |
| Old module code is used | The cache has an old copy. | Delete .terragrunt-cache/, or change the ref. |
backend.tf already exists and is not managed by Terragrunt | A hand-written file. | Remove it, or set if_exists = "overwrite" carefully. |
Uses terraform but you want tofu (or the other way) | Binary choice. | Set terraform_binary in the root config or the TG_TF_PATH environment variable. |
| A command was "not found" after an update | CLI redesign between versions. | Read the migration notes (run --all instead of run-all). |
Next steps
- Quick how-to: Separate environments in Terraform.
- Command page: terragrunt, Command page: tofu.
- The engine underneath: OpenTofu, Terraform. Modules: Terraform — Modules.
- At work: Many environments without copy-paste.
1How does every Terragrunt unit get its own state file without writing a backend in each folder?
The root remote_state uses path_relative_to_include() in the key, and every unit includes the root.
2Why use mock_outputs in a dependency?
So validate and plan work before the other unit is applied.