Terragrunt units and configuration reuse
Why Terragrunt exists
A team may run the same Terraform module for several environments, each with separate configuration and state. Repeating the same backend settings and inputs makes differences hard to review. Terragrunt is a separate command-line tool that organizes these executions; Terraform or OpenTofu still evaluates the infrastructure configuration and manages resources. A unit is an independently operated configuration, while a stack groups units. Do not confuse Terragrunt stacks with HCP Terraform Stacks. The inherited Terragrunt 1.1.2 candidate has not been verified or installed; examples follow the official documentation lane, not a tested version pair. See https://docs.terragrunt.com/features/units/.
Sketch the directory structure
This fictional training project has four units and one shared module. Each unit should have its own state address or path. The app consumes a network label from its own environment, rather than from whichever environment was most recently applied.
fixture/ root.hcl modules/label/main.tf live/dev/network/terragrunt.hcl live/dev/app/terragrunt.hcl live/test/network/terragrunt.hcl live/test/app/terragrunt.hclDependency: app requires network within the same environment.The folder names help people understand the layout but do not grant access or create an isolation boundary. Enforce separate state keys, identities and approvals. The fixture contains no cloud provider and uses the built-in terraform_data resource. It is staged for inspection, not execution.
The reusable engine configuration
The shared module accepts two strings and publishes a derived label. This is ordinary Terraform language; Terragrunt does not redefine the variable types or resource lifecycle.
variable "label" { type = string}
variable "upstream" { type = string default = "none"}
resource "terraform_data" "label" { input = "${var.label}:${var.upstream}"}
output "label" { value = terraform_data.label.output}If label is training-network and upstream is omitted, the intended output is training-network:none after a successful local apply. A consumer can use that value as its upstream input. Expected output is reasoned, not observed. Keep the configuration and the engine version under review.
Share settings with include and source
The parent file contains a synthetic owner label. Each unit finds the parent explicitly, makes its local values accessible and selects the shared module using a local source path.
locals { owner = "training"}include "root" { path = find_in_parent_folders("root.hcl") expose = true}
terraform { source = "${get_terragrunt_dir()}/../../../modules/label"}
inputs = { label = "${include.root.locals.owner}-network"}The local source path points directly to the self-contained module rather than copying the whole fixture tree. The actual copy and path handling must be tested with the pinned CLI. Remote sources can use a separate package and subdirectory convention; they are outside this fixture. The include is not textual concatenation: review merging rules, exposed values and overlapping settings before relying on them. Inputs are passed to the engine as variable values; the module declares the types. See https://docs.terragrunt.com/features/units/includes/.
Pass outputs from another unit
An application can depend on an already-managed network unit. Here the label is not a network ID, only synthetic data.
include "root" { path = find_in_parent_folders("root.hcl") expose = true}
terraform { source = "${get_terragrunt_dir()}/../../../modules/label"}
dependency "network" { config_path = "../network" mock_outputs = { label = "training-network:none" } mock_outputs_allowed_terraform_commands = ["validate"]}
inputs = { label = "${include.root.locals.owner}-app" upstream = dependency.network.outputs.label}For the app, the anticipated label is training-app:training-network:none once the dependency has a real local output. The mock is allowed for validate only: it is not evidence that the upstream exists and must not be used to approve an apply. Distinguish dependency output consumption from a dependencies block that expresses ordering without providing those values. If an output is absent, review the upstream contract rather than inventing a value. See https://docs.terragrunt.com/reference/hcl/blocks/.
Centralize state settings without merging ownership
A shared backend template can be convenient, but two independent units must not accidentally use the same state location. A path-derived key is one possible convention; changing the directory structure can then change the derived key. Treat such a change as a state migration, not merely a rename.
# Non-executable illustration; fictitious names.remote_state { backend = "s3" config = { bucket = "example-training-state-not-a-real-target" key = "${path_relative_to_include()}/terraform.tfstate" region = "eu-west-1" }}This remote backend example is deliberately non-executable. Actual backend setup can involve cloud operations, credentials, permissions and costs; the exact bootstrap behavior depends on release, flags and backend. Review the backend target, lock behavior and ownership before initialization. See https://docs.terragrunt.com/features/units/state-backend/.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Terragrunt units and configuration reuse — knowledge check
Review unit configuration, dependencies and execution boundaries.
What does a Terragrunt unit represent?
Why use a shared module?
What does dependency.network.outputs.label refer to?
A mock output is permitted only for validate. Can it authorize apply?
What risk arises when a backend key is derived from directory paths?