CoursesTerragruntIntermediate

Dependencies & outputs

Wire modules together safely.

Advanced14 min · lesson 7 of 12

Separate state units need to share data — the app unit needs the vpc unit’s subnet IDs, the database unit’s endpoint. Terragrunt’s dependency block reads another unit’s Terraform outputs and exposes them, and in doing so declares an ordering edge: Terragrunt knows the app depends on the vpc and will apply them in the right order under run-all. This replaces fragile remote-state data sources with a first-class dependency graph.

live/prod/app/terragrunt.hcl
dependency "vpc" {
config_path = "../vpc"
mock_outputs = { # used during plan when vpc isn’t applied yet
vpc_id = "vpc-mock"
subnet_ids = ["subnet-mock"]
}
}
inputs = {
vpc_id = dependency.vpc.outputs.vpc_id
subnet_ids = dependency.vpc.outputs.subnet_ids
}

Mock outputs for planning

A dependency’s real outputs only exist once that unit is applied, which breaks planning a brand-new environment where nothing is applied yet. mock_outputs supply placeholder values so a plan can run before the dependency exists — essential for run-all plan on a fresh stack. The mocks are for planning only; the real outputs are used at apply. Getting mocks right is what lets you plan a whole environment from scratch.

terminal
$ terragrunt output -raw vpc_id # a single unit’s output
# under run-all, Terragrunt applies vpc before app automatically because of the dependency
Mock outputs can hide real problems
mock_outputs make plans succeed even when a dependency is misconfigured, so an over-broad or wrong mock can mask an error until apply. Keep mocks minimal and realistic, scope them to the plan/validate commands that need them (mock_outputs_allowed_terraform_commands), and never let a mock stand in for a value you should be verifying — the plan you trust must reflect reality at apply time.