【问题标题】:accessing variable in terragrunt that is in terraform.tfvars访问 terraform.tfvars 中的 terragrunt 中的变量
【发布时间】:2020-07-02 13:26:21
【问题描述】:

我在以下位置有这个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", "")
}

但它被设置为默认的""

【问题讨论】:

    标签: terraform terragrunt


    【解决方案1】:

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

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

    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 的目录。

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 2021-06-01
      • 2020-07-23
      • 2020-07-10
      • 2021-05-01
      • 2020-04-15
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多