如何指示 terragrunt 模块的自定义配置文件?

How to indicate custom configuration files for terragrunt modules?

我正在尝试构建 Terragrunt 脚本以将基础结构部署到 Microsoft Azure 云。事情进展顺利,但我无法弄清楚一件事。

设置的结构如下所示:

rootdir
  terragrunt.hcl
  someconfig.hcl
    module1dir
      terragrunt.hcl
      config.auto.tfvars.json
    module2dir   
      terragrunt.hcl
      config.auto.tfvars.json
    module3dir   
      terragrunt.hcl
      config.auto.tfvars.json

每个模块都使用带有 config.auto.tfvars.json 的 Terraform 自动加载 tfvars 功能进行配置。我想要的是将这些文件放在目录结构之外,并以某种方式指示 Terragrunt 将正确的外部配置文件应用于正确的子模块。

有什么想法吗?

我通过以下方式解决了这个问题:

定义您计划使用的环境变量,其中应包含配置文件的位置。确保它不会与现有的任何东西发生冲突。在本例中,我们将使用 TGR_CFGDIR。在外部配置模块中放置模块配置文件并确保它们被正确命名。每个文件都应命名为模块并以 .auto.tfvars.json 结尾。因此,如果您的模块名为 foo,您应该有配置文件 foo.auto.tfvars.json。更改您的 terragrunt 模块 (terragrunt.hcl) 以具有以下语句:

locals {
  moduleconfig = get_env("TGR_CFGDIR")
  modulename   = basename(get_terragrunt_dir())
}

generate "configuration" {
  path              = "config.auto.tfvars.json"
  if_exists         = "overwrite"
  disable_signature = true
  contents          = file("${local.moduleconfig}/${local.modulename}.auto.tfvars.json")
}

最后像这样调用 terragrunt cli:

TGR_CFGDIR="<configdir>" terragrunt "<somecommand>"