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

问题描述

我正在尝试构建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

每个模块都使用terraform自动加载tfvars功能config.auto.tfvars.json进行配置。我想让这些文件位于目录结构之外,并以某种方式指示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>"