I am trying to create routes in transit gateway route table. Below is the code block.
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,
]
}
Error:
Error: Invalid index\n\n on ../modules/tgw/main.tf line 85, in resource "aws_ec2_transit_gateway_route" "this":\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 is tuple with 1 element\n | count.index is 1\n\nThe given key does not identify an element in this collection value.\n\n",
You will have only 0 or 1
aws_ec2_transit_gateway_route_table.route
, depending on the value ofcreate_tgw
. So it should be: