Skip to main content
Knowledge Base

Passing variable from child to parent terragrunt.hcl

Answer

I'm using yamldecode with templatefile and need to pass dependency outputs in the child as possible inputs to templatefile and was wondering what the easiest way of doing this would be? parent.hcl locals: ```hcl locals { parent_dir = get_parent_terragrunt_dir() child_path = path_relative_to_include() # test/foo--mod or test/mod. child_path_components = compact(split("/", local.child_path)) # test, foo--mod or test, mod. child_module = reverse(split("--", reverse(local.child_path_components)[0]))[0] # mod possible_value_paths = [ for i in range(0, length(local.child_path_components) + 1) : join("/", concat( [local.parent_dir], slice(local.child_path_components, 0, i), ["values.yaml"] )) ] raw_values = [ for path in local.possible_value_paths : yamldecode(file(path)) if fileexists(path) ] values = [ for path in local.possible_value_paths : # Terraform doesn't provide a templatestring function thus we need to yamldecode() twice. yamldecode(templatefile(path, local.raw_values)) if fileexists(path) # Parent templates can't reference child templates as they won't be templated. ] # TODO: get child dependency outputs somehow so yaml files can reference them. } ``` --- <ins datetime="2022-06-17T17:44:33Z"> <p><a href="https://support.gruntwork.io/hc/requests/108804">Tracked in ticket #108804</a></p> </ins>

You can't reference dependencies in locals due to [a limitation in terragrunt parsing logic](https://terragrunt.gruntwork.io/docs/getting-started/configuration/#configuration-parsing-order). However, you can kind of do what you want by taking advantage of the fact that `terragrunt` uses environment variables to pass terraform variables, and thus checks on `inputs` are more relaxed. See [this blog post](https://blog.gruntwork.io/even-more-dry-and-maintainable-code-with-terragrunt-5738d1ffc1c9), and more specifically [this section](https://blog.gruntwork.io/even-more-dry-and-maintainable-code-with-terragrunt-5738d1ffc1c9#f9bc).