Terragrunt for Beginners

Terragrunt Blocks

dependencies Block

The dependencies block in Terragrunt ensures that modules are applied in a specific order when running commands like terragrunt run-all. By listing paths to prerequisite modules, you guarantee that upstream infrastructure is provisioned before downstream modules execute.

Key Attribute

AttributeTypeDescription
pathslist(string)A list of relative paths to modules that must finish before the current module.

Note

The dependencies block only enforces execution order. It does not retrieve outputs from those modules. To reference outputs, use the dependency block with config_path and outputs.

The image is a diagram titled "Dependencies Block" with two icons and descriptions: one for executing a module during run-all, and another for addressing dependencies across infrastructure. At the bottom, there's a "Considerations" label.

Usage Scenario

Imagine you have defined VPC and EC2 modules, and you want to add an S3 bucket that should only be provisioned after the EC2 instance. Even though the S3 bucket doesn’t consume any EC2 outputs, you can enforce this order:

The image illustrates a "Dependencies Block" with a sequence of module apps and an icon representing infrastructure dependencies, highlighting usage scenarios.

terraform {
  source = "tfr://terraform-aws-modules/s3-bucket/aws//?version=4.1.2"
}

include "root" {
  path   = find_in_parent_folders()
  expose = true
}

include "common" {
  path   = find_in_parent_folders("common.hcl")
  expose = true
}

dependencies {
  paths = ["../ec2"]
}

inputs = {
  bucket = include.common.locals.project
}

Applying with terragrunt run-all

From the root configuration directory, run:

terragrunt run-all apply

This command applies the EC2 module under ../ec2 first, then provisions the S3 bucket, preserving the correct module sequence and preventing race conditions.

Watch Video

Watch video content

Previous
dependency Block