Error: Invalid index empty tuple The given key does not identify an element in this collection value

Error: Invalid index empty tuple The given key does not identify an element in this collection value

我正在尝试在中转网关路由 table 中创建关联和路由。下面是代码块。

locals {
  vpc_attachments_without_default_route_table_association = {
    for k, v in var.vpc_attachments : k => v if lookup(v, "transit_gateway_default_route_table_association", true) != true
  }
  vpc_attachments_with_routes = chunklist(flatten([
    for k, v in var.vpc_attachments : setproduct([{ key = k }], v["tgw_route"]) if length(lookup(v, "tgw_route", {})) > 0
  ]), 2)
 
  }

resource "aws_ec2_transit_gateway_route_table" "this" {
  count = var.create_tgw ? 1 : 0

  transit_gateway_id = aws_ec2_transit_gateway.this[0].id

  tags = merge(
    {
      "Name" = format("%s", var.name)
    },
    var.tags,
    var.tgw_route_table_tags,
  )
}


resource "aws_ec2_transit_gateway_route_table_association" "this" {
  for_each = local.vpc_attachments_without_default_route_table_association

  transit_gateway_attachment_id  = aws_ec2_transit_gateway_vpc_attachment.this[each.key].id
  transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.this[0].id
}

错误: 错误:无效 index\n\n on ../modules/tgw/main.tf line 118, in resource "aws_ec2_transit_gateway_route_table_association" "this":\n 118: transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.this[ 0].id\n |----------------\n | aws_ec2_transit_gateway_route_table.this 为空 tuple\n\nThe 给定键未标识此集合值中的元素。\n\n"

aws_ec2_transit_gateway_route_table_association 资源取决于您的情况 aws_ec2_transit_gateway_route_table。你能试试下面的代码吗?

resource "aws_ec2_transit_gateway_route_table_association" "this" {
  for_each = var.create_tgw ? local.vpc_attachments_without_default_route_table_association : {}

  transit_gateway_attachment_id  = aws_ec2_transit_gateway_vpc_attachment.this[each.key].id
  transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.this[0].id
}

这给出了空元组,因为它是 运行 2 个模块,它无法找到路由 table。我分别创建了解决问题的路由和关联。