joebegborg07
joebegborg07

Reputation: 839

Share values from child to parent

I would like to achieve common tags to be included with a set of other tags. Let's assume this is my working directory tree:

├── README.md
├── _envcommon
│   └── eks-vpc.hcl
├── non-production
│   ├── account.hcl
│   └── eu-west-3
│       ├── dev
│       │   ├── eks-vpc
│       │   │   └── terragrunt.hcl
│       │   └── env.hcl
│       ├── region.hcl
└── terragrunt.hcl

What I'm trying to achieve is that fron ./non-production/eu-west-1/dev/eks-vpc/terragrunt.hcl to fetch additional_tags from _envcommon/eks_vpc.hcl and then pass them to the generate "provider"block in theroot` config.

./non-production/eu-west-1/dev/eks-vpc/terragrunt.hcl:

# config common to all terragrunt repository
include "root" {
  path   = find_in_parent_folders()
  expose = true
}

# Module common values
include "envcommon"{
    path = "${dirname(find_in_parent_folders())}/_envcommon/vpc.hcl"
    # Should contain values that are common to all environments in which this module will be used in.
    expose = true
}

terraform {
  source = "${include.envcommon.locals.base_url}?ref=v1.0.0"
}

inputs = {
    ... other input values
}

./_envcommon/vpc.hcl:

locals {
  additional_tags={
    "Criticality"       = "critical"
    "Supporting:Tools"  = true
  }
}

./terragrunt.hcl:

locals {
  account_vs     = read_terragrunt_config(find_in_parent_folders("account.hcl"))

  environment_vs = read_terragrunt_config(find_in_parent_folders("env.hcl"))

  account_id         = local.account_vs.locals.aws_account_id
  profile            = "<aws_profile_name>"
  region             = "eu-west-1"

  common_tags = {
      env            = "${local.environment_vs.locals.environment}"
      controlledBy   = "terraform"
  }

  tags = merge(local.common_tags, additional_tags_fetched_from_envcommon)
}


generate "provider" {
  path      = "provider.tf"
  if_exists = "skip"
  contents  = <<EOF
provider "aws" {
  region = "${local.region}"
  profile = "${local.profile}"
  # Only these AWS Account IDs may be operated on by this template
  allowed_account_ids = ["${local.account_id}"]
  default_tags {
    tags = ${jsonencode(local.tags)}
  }
}
EOF
}

remote_state {
  ... remote_state_config
}

vpc configuration is an example and I actually have other modules I'm using and this approach would make life easier to inject different default_tags per module

Upvotes: 0

Views: 88

Answers (1)

nmishin
nmishin

Reputation: 3064

It looks like your configuration doesn't work because variable resolution in the include path isn't functioning properly, as mentioned in these this and this github issues.

However, I found a workaround that might be suitable for you.

In the ./non-production/eu-west-1/dev/eks-vpc/terragrunt.hcl, you can add the following include block:

include "test" {
  path = "${get_terragrunt_dir()}/../../../../_envcommon/eks_vpc.hcl"
  expose = true
}

Then, you can reference to this variables like:

additional_tags = "${include.test.locals.additional_tags}"

You can check this link for more info end examples.

Upvotes: 0

Related Questions