0

在我的terragrunt.hcl文件中,我将几个变量传递给一个模块 - 其中一个是 map 和 list

terraform {
  source = "git@github.com:<my_account>/infrastructure-modules.git//iam?ref=v.0.0.9"
}

include {
  path = find_in_parent_folders()
}

inputs = {
  var_1 = "string"
  var_2 = { 
    object1 = { 
      val1 : "a", 
      val2 : "b" 
    },
    object2 = { 
      val3 : "c", 
      val4 : "d" 
  }
}

但是当我将它传递给一个模块时

resource "google_project_iam_binding" "members" {
  for_each = var.var2
  project  = var.project_name
  role     = "projects/${var.project_name}/roles/${each.key}"
  members  = each.value
}

它不理解它并将其视为一个字符串

Error: Invalid for_each argument

  on main.tf line 34, in resource "google_project_iam_binding" "members":
  34:   for_each = var.var2

The given "for_each" argument value is unsuitable: the "for_each" argument
must be a map, or set of strings, and you have provided a value of type
string.

有人对可能是什么问题有任何想法吗?

4

2 回答 2

2

解决方案很奇怪但还可以,必须添加jsondecode(var.var2)它才能工作

于 2020-07-12T14:59:49.780 回答
2

我遇到了类似的问题,不得不简单地将我的 terraform 变量定义为类型映射,这似乎允许 terragrunt 将其作为字符串传递,然后 terraform 为您解析它。

variable var2 {
  type = map
}
于 2021-03-24T11:43:47.980 回答