gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.09k stars 981 forks source link

dependency block with a relative path does not resolve anymore when included with expose=true #3475

Open JeanFred opened 1 month ago

JeanFred commented 1 month ago

Describe the bug

If a dependency block with a relative path is present in a file that is included, the path resolution stops working when that file is included with expose=true.

Steps To Reproduce

I have a repo which follows the _envcommon pattern described in the live-example − here is a reconstructed, minimal reproduction:

.
├── environments
│   ├── _envcommon
│   │   └── b.hcl
│   ├── qc
│   │   └── eu-west-1
│   │       ├── a
│   │       │   └── terragrunt.hcl
│   │       └── b
│   │           └── terragrunt.hcl
│   └── terragrunt.hcl
└── layers
      ├── a
      │   └── main.tf
      └── b
          └── main.tf

b.hcl

terraform {
  source = "${dirname(find_in_parent_folders())}/..//layers/b"
}

dependency "a" {
  config_path = "../a/"
}

inputs = {
  region = dependency.a.outputs.region
}

qc/eu-west-1/a/terragrunt.hcl

include "root" {
  path = find_in_parent_folders()
}

terraform {
  source = "${dirname(find_in_parent_folders())}/..//layers/a"
}

qc/eu-west-1/b/terragrunt.hcl

include "root" {
  path = find_in_parent_folders()
}

include "envcommon" {
  path   = "${dirname(find_in_parent_folders())}/_envcommon/b.hcl"
  expose = true
}

When expose=true is commented out, all is fine − a Terraform plans runs successfully, and the output from a is passed to b

With expose=true, the plan fails with On latest Terragrunt 0.68.1:

21:06:42.299 ERROR  stat ../../../a: no such file or directory
21:06:42.299 ERROR  Unable to determine underlying exit code, so Terragrunt will exit with error code 1

on Terragrunt 0.60.1

level=warning msg=Error reading file terraform-test/environments/a: open terraform-test/environments/a: no such file or directory
level=error msg=open terraform-test/environments/a: no such file or directory
level=error msg=Unable to determine underlying exit code, so Terragrunt will exit with error code 1

Expected behavior

The dependency keeps working even after using expose=true − or it never works! But it behaves consistently.

Versions

Additional context

I guess I will try using some combination of path_relative_to_include, path_relative_from_include, get_terragrunt_dir and friends until it works :)

JeanFred commented 1 month ago

I guess I will try using some combination of path_relative_to_include, path_relative_from_include, get_terragrunt_dir and friends until it works :)

Ha! It was easier than I thought. This does the trick:

dependency "a" {
  config_path = "${get_terragrunt_dir()}/../a/"
}
denis256 commented 1 month ago

Hi, I think can be also used path_relative_to_include or get_repo_root(to pass path relative from repo root):

config_path = "${path_relative_to_include()}/../a/"

References:

https://terragrunt.gruntwork.io/docs/reference/built-in-functions/#path_relative_to_include

https://terragrunt.gruntwork.io/docs/reference/built-in-functions/#get_repo_root