Skip to content

path_relative_to_include

unitstack

Usage

path_relative_to_include([name: string]) --> string

A file that many units include can use path_relative_to_include to give each unit its own state key:

root.hcl
remote_state {
backend = "s3"
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
config = {
bucket = "my-tofu-state"
key = "${path_relative_to_include()}/tofu.tfstate"
region = "us-east-1"
}
}
prod/mysql/terragrunt.hcl
include "root" {
path = find_in_parent_folders("root.hcl")
}
  • root.hcl
  • Directoryprod
    • Directorymysql
      • terragrunt.hcl
  • Directorystage
    • Directorymysql
      • terragrunt.hcl

The key is prod/mysql/tofu.tfstate for prod/mysql and stage/mysql/tofu.tfstate for stage/mysql. Without an include block, the function returns ..

A unit with more than one include block passes the label of the one to measure from:

prod/mysql/terragrunt.hcl
include "root" {
path = find_in_parent_folders("root.hcl")
}
include "region" {
path = find_in_parent_folders("region.hcl")
}
inputs = {
name = replace(path_relative_to_include("root"), "/", "-")
}

Parameters

nameoptional(string)

The label of the include block to measure from. A unit with more than one include block needs it. In the included file, it has no effect.

Examples

When prod/mysql/terragrunt.hcl includes this file.

root.hcl
locals {
unit_path = path_relative_to_include() # "prod/mysql"
}