【问题标题】:How to indicate custom configuration files for terragrunt modules?如何指示 terragrunt 模块的自定义配置文件?
【发布时间】:2021-02-04 10:04:58
【问题描述】:
我正在尝试构建 Terragrunt 脚本以将基础架构部署到 Microsoft Azure 云。一切运作良好,但我无法弄清楚一件事。
setup 的结构如下所示:
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 将正确的外部配置文件应用于正确的子模块。
有什么想法吗?
【问题讨论】:
标签:
terraform
terraform0.12+
terragrunt
【解决方案1】:
我通过以下方式解决了这个问题:
定义您计划使用的环境变量,其中应包含配置文件的位置。确保它不会与现有的任何东西发生冲突。在本例中,我们将使用 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>"