【问题标题】:Error: Invalid Index The given key does not identify an element in this collection value Transit gateway routes错误:无效索引 给定键未标识此集合值中的元素 中转网关路由
【发布时间】:2021-08-26 01:39:44
【问题描述】:

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

locals {
  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" "route" {
  count = var.create_tgw ? 1 : 0
  transit_gateway_id = aws_ec2_transit_gateway.this[0].id
}

resource "aws_ec2_transit_gateway_route" "this" {
  count = length(local.vpc_attachments_with_routes)

  destination_cidr_block = local.vpc_attachments_with_routes[count.index][1]["destination_cidr_block"]
  blackhole              = lookup(local.vpc_attachments_with_routes[count.index][1], "blackhole", null)

  transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.route[count.index].id
  transit_gateway_attachment_id  = tobool(lookup(local.vpc_attachments_with_routes[count.index][1], "blackhole", false)) == false ? aws_ec2_transit_gateway_vpc_attachment.this[local.vpc_attachments_with_routes[count.index][0]["key"]].id : null
   depends_on = [
    aws_ec2_transit_gateway_route_table.route,
  ]
}

错误:

错误:../modules/tgw/main.tf 第 85 行,资源“aws_ec2_transit_gateway_route”“this”中的无效索引\n\n:\n 85:transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.route[count.index].id \n |----------------\n | aws_ec2_transit_gateway_route_table.route 是具有 1 个元素的元组\n | count.index 为 1\n\n给定的键未标识此集合值中的元素。\n\n",

【问题讨论】:

  • 变量的值是多少? create_tgw、vpc_attachments 等。
  • 进展如何?错误仍然存​​在?
  • 是的,正如我所说,如果 create_tgw 为 false,它就会出现。

标签: amazon-web-services terraform terraform-provider-aws transit-gateway


【解决方案1】:

您将只有 0 或 1 个 aws_ec2_transit_gateway_route_table.route,具体取决于 create_tgw 的值。所以应该是:

resource "aws_ec2_transit_gateway_route" "this" {
  count = length(local.vpc_attachments_with_routes)

  destination_cidr_block = local.vpc_attachments_with_routes[count.index][1]["destination_cidr_block"]
  blackhole              = lookup(local.vpc_attachments_with_routes[count.index][1], "blackhole", null)

  transit_gateway_route_table_id = var.create_tgw ? aws_ec2_transit_gateway_route_table.route[0].id : null 

  transit_gateway_attachment_id  = tobool(lookup(local.vpc_attachments_with_routes[count.index][1], "blackhole", false)) == false ? aws_ec2_transit_gateway_vpc_attachment.this[local.vpc_attachments_with_routes[count.index][0]["key"]].id : null
}

【讨论】:

  • 这表示“无效的表达式资源 \"aws_ec2_transit_gateway_route\" \"this\":\n 89: depends_on = \u001b[4mvar.create_tgw\u001b[0m ? aws_ec2_transit_gateway_route_table.route[0].id : [] 需要静态列表表达式。\n\u001b[0m\u001b[0m\n","
  • @MayaRay 依赖不需要,可以去掉。
  • 它给出了这个错误:错误:资源中缺少必需的参数 \"aws_ec2_transit_gateway_route\" \"this\":\n 85: transit_gateway_route_table_id = var.create_tgw ? aws_ec2_transit_gateway_route_table.route[0].id : null \n\n参数 \"transit_gateway_route_table_id\" 是必需的,但未找到\n定义
  • 如果您的create_tgw 为假,您将不会拥有aws_ec2_transit_gateway_route,正如我在答案中所解释的那样。
  • 我正在尝试通过多个帐户共享一个中转网关,如果我将 create_tgw 标记为全部为真,它将创建中转网关、附件、路由两个帐户中的所有内容。有没有其他选择?
猜你喜欢
  • 2021-08-25
  • 2022-01-23
  • 2022-07-12
  • 2021-05-15
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多