Terragrunt referencing root locals from child HCLs

90 Views Asked by At

I'm new to Terragrunt and have been trying to setup a particular structure but have been having some issues. It could be i'm just not thinking "terragrunt-ly" but want to make sure i'm not misunderstanding something.

For my app i have a structure similiar to this:

live/
  nonprod/
    account.hcl
    dev/
      env.hcl
      databases/
        terragrunt.hcl
modules/
  databases/
     ...tf files
terragrunt.hcl

My main issue is I have been trying to figure out how to replace a globals module we had in pure terraform originally. It was essentially just a massive file of locals and outputs.

I have a local like an AWS KMS alias that is often used throughout the terraform, but it's name is built kind of like alias/key-${local.account == "prod" ? local.env : "dev"}. This alias uses both an environment variable as well as an account level variable.

From the Terragrunt live example https://github.com/gruntwork-io/terragrunt-infrastructure-live-example/blob/master/terragrunt.hcl, it seems that it would do exactly what I would want:

locals {
  # Automatically load account-level variables
  account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))

  # Automatically load region-level variables
  region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))

  # Automatically load environment-level variables
  environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))

  # Extract the variables we need for easy access
  account_name = local.account_vars.locals.account_name
  account_id   = local.account_vars.locals.aws_account_id
  aws_region   = local.region_vars.locals.aws_region
}

and even at the bottom it has:

# ---------------------------------------------------------------------------------------------------------------------
# GLOBAL PARAMETERS
# These variables apply to all configurations in this subfolder. These are automatically merged into the child
# `terragrunt.hcl` config via the include block.
# ---------------------------------------------------------------------------------------------------------------------

# Configure root level variables that all resources can inherit. This is especially helpful with multi-account configs
# where terraform_remote_state data sources are placed directly into the modules.
inputs = merge(
  local.account_vars.locals,
  local.region_vars.locals,
  local.environment_vars.locals,
)

So now my locals in my root terragrunt.hcl looks like this:

locals {
  account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
  environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))

  ...

  kms = {
    item_authoring = "alias/key-${local.account == "prod" ? local.env : "dev"}"
  }
}

I believe this is exactly what I want. However, in my databases/terragrunt.hcl, when I try to pass in the value, I keep getting This object does not have an attribute named "kms_alias".

my databases terragrunt.hcl looks something like this:

include "root" {
  path = find_in_parent_folders()
  expose = true
  merge_strategy = "deep"
}

terraform {
  source = "${path_relative_from_include()}/modules/databases"
}

locals {
  account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
  environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
}

inputs = {
  is_prod = local.account_vars.locals.account == "prod"
  env = local.environment_vars.locals.env
  kms_alias = include.root.locals.kms_alias
  fallback_region = include.root.locals.regions.fallback_region
}

It does not like the include.root.locals.kms_alias. Am i referencing the root wrong somehow? Is that where like the inputs merge in the root comes into play?

0

There are 0 best solutions below