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

问题描述 投票:1回答:1

我在以下位置有此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 = "[email protected]"

我在文档中找不到如何访问此文件。我已经尝试过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个回答
4
投票

这实际上是一个非常常见的用例,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_terraform_commands_that_need_vars()列出get_terragrunt_dir()的目录。

© www.soinside.com 2019 - 2024. All rights reserved.