Terraform 嵌套循环,每个错误:“for_each”参数必须是您提供的元组类型值的地图

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

我尝试在 Terraform 中创建嵌套循环来创建多个 aws TGW 表。以及我使用的每个表的多重传播和合并函数来创建字典,键是路由表 ID,值是传播 ID,然后用于每个在字典中循环。 代码

variable "tgw_route_tables" {
  description = "Map of project names to configuration."
  type        = map(any)
  default = {
    TestEgypt = {
      tgw_route_table_name  = "",
      association = "",
      propagations    = ["", "", ],
    },
  }
} 

locals {
  tgw_route_table_id = [aws_ec2_transit_gateway_route_table.transit_gateway_route_table[*].id]
  propagation_map = [
    for table_id in local.tgw_route_table_id : flatten([
      for table in var.tgw_route_tables : [
        for propagation in table.propagations : {
            table_id = propagation
        }
      ]
    ])
  ]
  
}

resource "aws_ec2_transit_gateway_route_table" "transit_gateway_route_table" {
  transit_gateway_id = aws_ec2_transit_gateway.ENZA-TransitGateway-Ireland-PRD-001.id
  for_each = var.tgw_route_tables
  tags = {
    "Name" = each.value.tgw_route_table_name
  }
}

resource "aws_ec2_transit_gateway_route_table_association" "tgw_route_table_association" {
  for_each = var.tgw_route_tables
  transit_gateway_attachment_id  = each.value.association
  transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.transit_gateway_route_table[each.key].id
}

resource "aws_ec2_transit_gateway_route_table_propagation" "tgw_route_table_propagation" {
  for_each = local.propagation_map
  transit_gateway_attachment_id  = each.value
  transit_gateway_route_table_id = each.key
}

但是我得到了这个错误:

"aws_ec2_transit_gateway_route_table_propagation" "tgw_route_table_propagation":
for_each = local.propagation_map
local.propagation_map is tuple with 1 element

我也试过了

table_id = propagation

我得到了错误。

amazon-web-services terraform nested-loops hcl
1个回答
0
投票

您不需要内括号。应该是:

locals {
  tgw_route_table_id = aws_ec2_transit_gateway_route_table.transit_gateway_route_table[*].id
  propagation_map = merge({
    for table_id in local.tgw_route_table_id : {
      for table in var.tgw_route_tables : {
        for propagation in table.propagations :  table_id => propagation 
      }
    }
  })
}
© www.soinside.com 2019 - 2024. All rights reserved.