首页 > 解决方案 > terragrunt 不接受 vars 文件

问题描述

我正在尝试使用 2-repo IaC,所谓的后端采用terragrunt模块的形式,而前端(或live)具有正在填充变量的此类模块的实例化。

下图描述了这 2 个 repos 的结构(顾名思义是terragrunt后端和实时的)。terraform-live

在此处输入图像描述

在 myterragrunt/aws-vpc/variables.tf中,有以下声明:

variable "remote_state_bucket" {
  description = "The bucket containing the terraform remote state"
}

但是,当尝试terragrunt apply在实时目录中执行时,我得到以下信息:

var.remote_state_bucket
  The bucket containing the terraform remote state

  Enter a value:

这是我的terraform-live/environments/staging/terragrunt.hcl

  remote_state {
    backend = "s3"
    config = {
      bucket  = "my-bucket-staging"
      key  = "terraform/state/var.env_name/${path_relative_to_include()}"
      region = "eu-west-1"
    }
  }
  # Configure root level variables that all resources can inherit
  terraform {
    extra_arguments "extra_args" {
      commands = "${get_terraform_commands_that_need_vars()}"
      optional_var_files = [
          "${get_terragrunt_dir()}/${find_in_parent_folders("config.tfvars", "ignore")}",
          "${get_terragrunt_dir()}/${find_in_parent_folders("secrets.auto.tfvars", "ignore")}",
      ]
    }
  }

更重要的是,该变量似乎是在terragrunt指示从以下位置读取变量的文件之一中声明的:

➢  cat terraform-live/environments/staging/config.tfvars
remote_state_bucket = "pkaramol-staging"

为什么terragrunt(或terraform?)无法读取特定变量?

➢  terragrunt --version
terragrunt version v0.19.29

➢  terraform --version
Terraform v0.12.4

标签: terraforminfrastructure-as-codeterragrunt

解决方案


因为config.tfvars不在父文件夹中:)

find_in_parent_folders在父文件夹中查找,但不在当前文件夹中。你和你config.tfvars的在同一个文件夹中terragrunt.hcl

尝试使用类似的东西:

optional_var_files = [
    "${get_terragrunt_dir()}/config.tfvars",
    "${get_terragrunt_dir()}/secrets.auto.tfvars",
]

推荐阅读