gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.09k stars 981 forks source link

AWS provider default_tags variables #3446

Closed m-parrella closed 1 month ago

m-parrella commented 1 month ago

Hi! I am working with AWS on a multi-account architecture and we are using Terragrunt latest version (v0.66.9) to manage the infrastructure. We are looking to implement default_tags with AWS provider but we are struggling to define account level tags with project level tags.

To simplify, let assume that the content of dev/account.yaml is:

environment: "sandbox"

And the content of /dev/department-a/project.yaml is:

owner: "department-a"

└── dev
  └── provider.hcl
  └── terragrunt.hcl
  └── account.yaml
  └── department-a
      ├── project.yaml
      ├── rds
      |   └── terragrunt.hcl
      └── vpc
          └── terragrunt.hcl
  └── department-b
      ├── project.yaml
      ├── rds
      |   └── terragrunt.hcl
      └── vpc
          └── terragrunt.hcl

We are trying to edit the provider.hcl to use both sources in order to populate the default_tags:

locals  {
  account          = yamldecode(file("account.yaml"))
  environment      = local.account.environment
  project          = yamldecode(file(join("/", [element(split("/", get_path_from_repo_root()), 2), "project.yaml"])))
  owner            = local.project.owner

}

generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
provider "aws" {
  region  = "us-east-1"
  profile = "sandbox"
  default_tags {
    tags = {
      Environment = ${local.environment}
      Owner = ${local.owner}
      }
  }
}
EOF
}

When we run terragrunt apply, we are not able to make local.project get the correct path for example "dev/project-a/project.yaml". Is this the correct approach or is this a limitation of how locals works? Is there any workaround?

Thanks in advance!

denis256 commented 1 month ago

Hi, It seems like the issue might be related to decoding/finding the YAML files, though it would help to have the exact error message.

Based on the directory structure, you could try resolving it by usingfind_in_parent_folders

project     = yamldecode(file("${find_in_parent_folders("project.yaml")}"))

Simplified end-to-end example in: https://github.com/denis256/terragrunt-tests/tree/master/issue-3446-v2

m-parrella commented 1 month ago

Hi Denis, thanks for the example! I was indeed using the yamldecode(), but I had an issue with a dependency block which caused confusion, I am closing the issue. Thanks again!