Terragrunt and common variables

14.2k views Asked by At

I'm trying to something fairly simple, but can't seem to get my head around it. I have the following structure:

- terragrunt.hcl
-----dummy/
---------main.tf
---------terragrunt.hcl

I'm looking to set some common variables at the root level, and use them in main.tf. Howe would I go about declaring the varibale in the root terragrunt level, and have them available downstream?

I've tried setting them as inputs in the root, but then have to explicitly declare "variables" at the dummy level for the inputs to get picked up. I'm looking to somehow define these things at the root level and not repeat variable declarations at dummy/ level. Is this doable?

3

There are 3 answers

0
mon On

Other tools like Ansible has directory hierarchy where child can refer to or override the value of a variable set at the parent level.

Terraform does not have such a mechanism and each directory having tf files is a separate Terraform module. So directory hierarchy cannot be used to pass/inherit/reference Terraform variables.

Perhaps better to let the idea of "downstream or upstream" go.

One way to define common variables and share them among other modules is Data-only Modules . Extension of this and make the common variable world-wide available is using Terraform registry although it is not the intended use.

0
openCivilisation On

You can indeed do this documented here: https://terragrunt.gruntwork.io/docs/reference/built-in-functions/#read_terragrunt_config

You can merge all inputs defined in some file above any module.

From the docs:

read_terragrunt_config(config_path, [default_val]) parses the terragrunt config at the given path and serializes the result into a map that can be used to reference the values of the parsed config. This function will expose all blocks and attributes of a terragrunt config.

For example, suppose you had a config file called common.hcl that contains common input variables:

inputs = {
  stack_name = "staging"
  account_id = "1234567890"
}

You can read these inputs in another config by using read_terragrunt_config, and merge them into the inputs:

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

inputs = merge(
  local.common_vars.inputs,
  {
    # additional inputs
  }
)

This function also takes in an optional second parameter which will be returned if the file does not exist:

locals {
  common_vars = read_terragrunt_config(find_in_parent_folders("i-dont-exist.hcl", "i-dont-exist.hcl"), {inputs = {}})
}

inputs = merge(
  local.common_vars.inputs, # This will be {}
  {
    # additional inputs
  }
)
1
Aaron Evans On

Per the Terragrunt documentation: "Currently you can only reference locals defined in the same config file. Terragrunt does not automatically include locals defined in the parent config of an include block into the current context."

However, one way you can do this is as follows:

  1. Create a file containing the common variables (e.g. myvars.hcl)
  2. Load that in the child terragrunt:

    locals {
      myvars = read_terragrunt_config(find_in_parent_folders("myvars.hcl"))
      foo = local.myvars.locals.foo
    }
    

Hope that helps!