4

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",

Marcin
  • 215,873
  • 14
  • 235
  • 294
Maya Ray
  • 523
  • 1
  • 7
  • 21

1 Answers1

3

You will have only 0 or 1 aws_ec2_transit_gateway_route_table.route, depending on the value of create_tgw. So it should be:

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
}
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • This says "Invalid expression resource \"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 : [] A static list expression is required.\n\u001b[0m\u001b[0m\n"," – Maya Ray Jun 09 '21 at 04:48
  • @MayaRay Depends on is not needed, and can be removed. – Marcin Jun 09 '21 at 04:50
  • It gives this error: Error: Missing required argument in resource \"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\nThe argument \"transit_gateway_route_table_id\" is required, but no definition\nwas found – Maya Ray Jun 09 '21 at 04:58
  • If your `create_tgw` is false you will not have `aws_ec2_transit_gateway_route` as I exlaiend in the answer. – Marcin Jun 09 '21 at 04:58
  • I am trying to share one transit gateway through multiple accounts, if I mark create_tgw as true for all, it will create transit gateway, attachments, routes everything in both the accounts. Is there any alternative? – Maya Ray Jun 09 '21 at 05:24
  • @MayaRay Your new design issue would be better asked as a new question with all the relevant details. In your current question, the error that you posted has been hopefully addressed. – Marcin Jun 09 '21 at 05:39