0

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

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
}

错误:错误:../modules/tgw/main.tf 第 118 行的无效索引\n\n,在资源“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。这是空元组\n\n给定的键未标识此集合值中的元素。\n\n"

4

2 回答 2

0

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
}
于 2021-06-08T16:01:48.117 回答
0

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

于 2021-06-16T04:48:29.957 回答