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.04k stars 975 forks source link

Make importer locals available to imported terragrunt file #2765

Closed jyoder-deloitte closed 3 weeks ago

jyoder-deloitte commented 1 year ago

Describe the solution you'd like I would like to be able to have some dynamic logic in my imported terragrunt file based on the module that is calling it.

The idea is to be able to do things like set what generate blocks are enabled/disabled, which files get loaded, and to control other logic with conditionals based on things set by the caller.

Super simple (and not terribly useful) example:

module/terragrunt.hcl:

locals {
  module_type = "data"
}

import "root" {
  path = "../terragrunt_root.hcl"
}

terragrunt_root.hcl:

locals {
  tags = {
    "managed-by" = "terraform"
    "ModuleType" = caller.locals.module_type
  }
}

inputs = {
  tags = local.tags
}

Describe alternatives you've considered Creating a module.hcl file that is read in by the root file. This will 100% work but requires maintaining another file alongside the terragrunt.hcl file in each folder.

module/module.hcl:

locals {
  module_type = "data"
}

terragrunt_root.hcl:

locals {
  module_vars = read_terragrunt_config(find_in_parent_folders("module.hcl"))

  tags = {
    "managed-by" = "terraform"
    "ModuleType" = local.module_vars.locals.module_type
  }
}

inputs = {
  tags = local.tags
}
github-actions[bot] commented 1 month ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for raising this issue.