【问题标题】:Terraform : The "each" object does not support this operationTerraform:“每个”对象不支持此操作
【发布时间】:2021-06-18 07:22:46
【问题描述】:

我正在尝试使用 Terraform 从如下变量创建“父”和“子”资源:

variable "tags" {
  default = {
    parent1 = ["child1", "child2"]
    parent2 = ["child2", "child4", "child5"]
  }
}

对于我的变量中的每个键,我想创建一个父资源,我这样做并且工作正常:

resource "google_tags_tag_key" "parent" {
    for_each = toset(keys(var.tags))
    parent = "organizations/1234567890"
    short_name = each.key
    description = ""
}

我的问题是关于子资源...对于列表中的每个项目,我想创建一个子资源。但我似乎无法对“for_each”参数进行嵌套循环。 所以我尝试使用“setproduct”函数来展平我的变量。如下。

locals {
  helper_list = [for k, v in var.tags:
            setproduct(list(k), v)
      ]
}

这是一种解决方法,但尽管如此,我仍然无法通过尝试以下方式来解决我的问题。

resource "google_tags_tag_value" "child" {
    for_each = local.helper_list
    parent = "tagKeys/${google_tags_tag_key.parent[each.0]}"
    short_name = each.1
    description = ""
}

这将返回以下错误:The "each" object does not support this operation。 如何通过或不通过 h​​elper_list 来解决我的问题?

【问题讨论】:

    标签: google-cloud-platform terraform


    【解决方案1】:

    我会这样做:

    variable "tags" {
      default = {
        parent1 = ["child1", "child2"]
        parent2 = ["child2", "child4", "child5"]
      }
    }
    
    locals {
    
     # construct flat map
     helper_map = merge([
                   for parent, children in var.tags:
                    {
                      for child in children:
                      "${parent}-${child}" => {
                          "parent" = parent
                          "child" = child                      
                       }
                    }
               ]...)
    
     
    }
    
    output "helper_map" {
      value = local.helper_map
    }
    

    给出:

    helper_map = {                                                                                                                                                                             
      "parent1-child1" = {                                                                                                                                                                     
        "child" = "child1"                                                                                                                                                                     
        "parent" = "parent1"                                                                                                                                                                   
      }                                                                                                                                                                                        
      "parent1-child2" = {                                                                                                                                                                     
        "child" = "child2"  
        "parent" = "parent1"
      }                    
      "parent2-child2" = {  
        "child" = "child2"  
        "parent" = "parent2"
      }                    
      "parent2-child4" = { 
        "child" = "child4"  
        "parent" = "parent2"
      }                   
      "parent2-child5" = {
        "child" = "child5"  
        "parent" = "parent2"
      }
    }  
    

    假设我了解您想要的结果,那么您可以这样做:

    resource "google_tags_tag_value" "child" {
        for_each = local.helper_map
        parent = "tagKeys/${google_tags_tag_key.parent[each.value.parent]}"
        short_name = each.value.child
        description = ""
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2018-06-01
      • 2015-10-24
      相关资源
      最近更新 更多