Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 53Terragrunt · IaC and automation

Install and set up Terragrunt

Terragrunt is a thin wrapper around OpenTofu and Terraform for many environments and accounts. Instead of copying the same backend, provider and module code into dev, test and prod, you write it once and each environment folder only says which module, which version, which inputs. It also runs many units in the right order with dependencies. This guide installs Terragrunt, builds the standard live/ layout with a root config that generates the backend and provider, wires dependencies between units, and shows the daily commands and fixes.

  • 8 Lessen
  • 8 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

53.1

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).

ItemValue
Programterragrunt (one binary from github.com/gruntwork-io/terragrunt/releases)
Runstofu (default when installed) or terraform
Filesroot.hcl (shared), terragrunt.hcl (per unit)
Cache.terragrunt-cache/ in each unit (do not commit)
Modulesin a separate repo or folder, used by Git tag (?ref=v1.2.0)

53.2

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 --version

Compare the two checksums. Work tip: tools like mise or tenv can install and pin Terragrunt, OpenTofu and Terraform versions per project.

53.3

First configuration: the live layout

layout
live/
├── root.hcl
├── dev/
│   ├── env.hcl
│   ├── vpc/terragrunt.hcl
│   └── db/terragrunt.hcl
└── prod/
    ├── env.hcl
    ├── vpc/terragrunt.hcl
    └── db/terragrunt.hcl

The root file: one state file per unit (by folder path), the backend and the provider are generated for every unit:

live/root.hcl
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
}
live/prod/env.hcl
locals {
  name = "prod"
}

A unit: which module, which version, which inputs:

live/prod/vpc/terragrunt.hcl
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:

live/prod/db/terragrunt.hcl
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
}
PartWhy
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.0Each environment pins a module version; promote a new version from dev → prod by changing the tag.
dependency + mock_outputsOrder between units, and plan works before the VPC exists.

Add .terragrunt-cache/ to .gitignore.

53.4

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.0 in dev, then change the ref in prod through a pull request.
  • Small states per unit = faster plans and a smaller blast radius.

53.5

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=main in prod.
  • run --all apply in prod only from CI after a reviewed plan.
  • Short-lived credentials; different roles per environment (for example with iam_role in the root config).
  • Pin Terragrunt and OpenTofu/Terraform versions.

53.6

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.

Commando's in deze les

53.7

Fix common problems

ProblemCauseFix
… has no outputs / dependency error at planThe 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 errorWrong file name or folder.Name the file root.hcl (or pass the name); run from inside live/.
Old module code is usedThe cache has an old copy.Delete .terragrunt-cache/, or change the ref.
backend.tf already exists and is not managed by TerragruntA 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 updateCLI redesign between versions.Read the migration notes (run --all instead of run-all).

53.8

Next steps

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.

Alle commando's in dit hoofdstuk