terragrunt icon indicating copy to clipboard operation
terragrunt copied to clipboard

Access the contents of the terraform source block from parent Terragrunt file

Open JeanFred opened this issue 1 year ago • 1 comments

Describe the solution you'd like I have an infrastructure repository looking like https://github.com/gruntwork-io/terragrunt-infrastructure-live-example, with a parent top-level Terragrunt file doing all the heavy lifting, and leaf Terragrunt file that are basically terraform { source = '/code/my-module' }

I want to access the contents of the source block from my parent Terragrunt file, to reuse that value. My use case is to tag my resources with a module=my-module.

Describe alternatives you've considered

By browsing terragrunt-infrastructure-live-example I considered doing something like

include "root" {
  path = find_in_parent_folders()
}

terraform {
  source = "${local.base_source_url}"
}

locals {
  base_source_url = "//my-module"
}

and in the top-level file

locals {
  leaf_vars = read_terragrunt_config("${get_terragrunt_dir()}/terragrunt.hcl")
  foo       = leaf_vars.base_source_url

but that results in a Go panic (I suppose since it becomes an infinite include loop). Even then, it would not work if things were more complex like using the [_envcommon](https://github.com/gruntwork-io/terragrunt-infrastructure-live-example/tree/master/_envcommon) pattern.

JeanFred avatar Nov 28 '23 13:11 JeanFred

Hello, I was thinking of extracting the configuration in a separate hcl file and load it from the parent...

# parent terragrunt.hcl
terraform {
  source = "${local.module_config.locals.url}"
}

locals {
  module_config = read_terragrunt_config("config.hcl")
}

# child terragrunt.hcl


include "root" {
  path = find_in_parent_folders()
}

locals {
  module_config = read_terragrunt_config("config.hcl")
}

# child config.hcl
locals {
  url = "/tmp/app2"
}

Simplified example in: https://github.com/denis256/terragrunt-tests/tree/master/issue-2812

denis256 avatar Dec 26 '23 19:12 denis256