AWS Transit Gateway Connect: Creating Multiple Peers for a Single Attachment

In my AWS environment, I’ve successfully implemented Transit Gateway Connect using Terraform, leveraging the power of aws_ec2_transit_gateway_connect and aws_ec2_transit_gateway_connect_peer. The architecture involves connecting various networks through attachments, but I’ve encountered a scenario where I need to create two connect peers for a single connect attachment.

Is there a way to efficiently achieve this using only one instance of the aws_ec2_transit_gateway_connect_peer resource in Terraform? I’ve explored the documentation and experimented with different configurations, but I’m curious if there’s a more optimized or concise approach to handle multiple peers for a specific attachment.

Here’s a snippet of my Terraform code for better context:

resource “aws_ec2_transit_gateway_connect” “this” {
for_each = { for k, v in var.connect_attachments : k => v if v != null }

transport_attachment_id = each.value.transport_attachment_id
transit_gateway_id = var.transit_gateway_id
transit_gateway_default_route_table_association = try(each.value.transit_gateway_default_route_table_association, false)
transit_gateway_default_route_table_propagation = try(each.value.transit_gateway_default_route_table_propagation, false)

tags = merge(
var.tags,
{ Name = “${each.key}-attachment” },
try(each.value.tags, {}),
)
}

resource “aws_ec2_transit_gateway_connect_peer” “this” {
for_each = { for k, v in var.connect_attachments : k => v if v != null }

peer_address = each.value.peer_address
inside_cidr_blocks = each.value.inside_cidr_blocks
bgp_asn = try(each.value.bgp_asn, null)
transit_gateway_address = try(each.value.tgw_address, null)
transit_gateway_attachment_id = aws_ec2_transit_gateway_connect.this[each.key].id
}

variable “connect_attachments” {
description = “Maps of maps of Connect details to attach to TGW. Type ‘any’ to disable type validation by Terraform.”
type = any
default = {}
}