【问题标题】:Error: Invalid index empty tuple The given key does not identify an element in this collection value错误:无效索引空元组给定键未标识此集合值中的元素
【发布时间】:2021-08-25 15:14:24
【问题描述】:

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

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 行,资源“aws_ec2_transit_gateway_route_table_association”“this”中的无效索引\n\n:\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"

【问题讨论】:

    标签: terraform terraform-provider-aws transit-gateway


    【解决方案1】:

    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
    }
    

    【讨论】:

    • 这可行,但是当我尝试与多个账户共享中转网关时,我无法在任何地方将 create_tgw 标记为 true。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-08-26
      • 2022-01-23
      • 2022-07-12
      • 2021-05-15
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多