gpt4 book ai didi

terraform - 访问 terraform.tfvars 中的 terragrunt 变量

转载 作者:行者123 更新时间:2023-12-03 08:25:35 37 4
gpt4 key购买 nike

我在以下位置有这个 terraform.tfvars 文件:

root
|_prod
|_eu-west-2
|_dev
|_terraform.tfvars
|_cognito
|_terragrunt.hcl

它有这些值:

terragrunt = {
terraform {
extra_arguments "custom_vars" {
commands = [
"apply",
"plan",
"import",
"push",
"refresh"
]

# With the get_tfvars_dir() function, you can use relative paths!
arguments = [
"-var-file=terraform.tfvars"
]
}
}
}

reply_to_email_address = "blah.blah@blah.scot"

我在文档中找不到如何访问它。我试过 get_env:

include {
path = find_in_parent_folders()
}

terraform {
// double `//` before module are important!
source = "../../../../../terraform-modules//cognito"
}

inputs = {
name = "pvg-online-${local.env}"
reply_to_email_address = get_env("reply_to_email_address", "")
}

但它被设置为默认的 ""

最佳答案

这实际上是一个非常常见的用例,以至于 terragrunt 为其内置了功能。

在您的 terragrunt.hcl 中,包含一个 terraform{} block ,如下所示:

terraform {
# Note that the double-slash (//) syntax is not needed for local relative paths
source = "../../../../../terraform-modules/cognito"

extra_arguments "common_var" {
commands = get_terraform_commands_that_need_vars()
arguments = ["-var-file=${get_terragrunt_dir()}/../terraform.tfvars"]
}
}

inputs = {
name = "pvg-online-${local.env}"
# Since reply_to_email_address is provided in the parent terraform.tfvars file,
# it is not needed as an input
}

注意 get_terraform_commands_that_need_vars() 的使用这样你就可以避免列出所有参数,以及 get_terragrunt_dir()用于查找 terragrunt.hcl 的目录。

关于terraform - 访问 terraform.tfvars 中的 terragrunt 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60790583/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com