Terragrunt for Beginners

Terragrunt Functions

path relative to include

In this lesson, we’ll dive into Terragrunt’s path_relative_to_include function. This built-in helper calculates the relative path between the current .hcl file and the file specified in its include block. Use it to:

  • Organize and modularize your configuration files
  • Construct dynamic file paths
  • Segregate remote state keys per module

Why Use path_relative_to_include?

BenefitDescription
Dynamic ConfigurationAdjust resource definitions based on their position in the directory hierarchy.
Modular StructuresBuild reusable, adaptable modules that survive folder reorganizations.
Remote State SegregationGenerate unique backend keys for each module, keeping your state isolated.

Note

When used without arguments, path_relative_to_include() returns the relative path from the current folder to where common.hcl (or your included file) lives.

Typical Terragrunt Setup

Imagine you have a shared common.hcl in a parent directory and multiple child modules that include it. You can tag each module’s remote state with its relative path:

terraform {
  source = "tfr://terraform-aws-modules/vpc/aws//?version=5.8.1"
}

locals {
  common_vars = read_terragrunt_config(
    find_in_parent_folders("common.hcl")
  )
}

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

inputs = {
  name = "${local.common_vars.inputs.project}-${local.common_vars.inputs.environment}"
  tags = {
    Path = path_relative_to_include()
  }
}

Here’s what happens when you run terragrunt plan:

Plan: 4 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + azs                                  = []
  + cgw_arns                             = []
  + cgw_ids                              = []
  + database_nat_gateway_route_ids       = []
  + database_route_table_association_ids = []
  + database_route_table_ids             = []
  + database_subnet_arns                 = []
  + database_subnets                     = []
  + database_subnets_cidr_blocks         = []
  + default_network_acl_id               = (known after apply)
  + default_route_table_id               = (known after apply)
  + default_security_group_id            = (known after apply)
  + elasticache_route_table_association_ids = []
  + elasticache_route_table_ids         = []
  + elasticache_subnet_arns             = []
  + elasticache_subnets                 = []

Notice how the Path tag reflects this module’s folder relative to common.hcl. By doing so, each module’s Terraform state is stored under a unique key in your remote backend, enhancing clarity and maintainability.

Further Reading

Watch Video

Watch video content

Previous
find in parent folders