Referencing to AWS Direct connect gateway TGW attachment using terraform

Hi

I am using terraform(aws provider 5.8.0) to create and associate a route table to a direct connect gateway TGW association. The terraform resource block for “aws_ec2_transit_gateway_route_table_association” requires a “transit_gateway_attachment_id”. How do I reference to the TGW attachment to direct connect gateway in this case? I can’t seem to find any attribute for “aws_dx_gateway_association” resource that provides a “transit_gateway_attachment_id”.

Below is a sample code I am using. The last resource block for “aws_ec2_transit_gateway_route_table_association” is where I am having trouble finding a way to reference to the TGW attachment to the direct connect gateway.

resource “aws_dx_gateway_association” “dxg-to-tgw-assoc”{
dx_gateway_id = some dxg id
associated_gateway_id = some tgw id

allowed_prefixes = [
“E.F.G.H/24”
]
}

#Create route table
resource “aws_ec2_transit_gateway_route_table” “rt-A” {
transit_gateway_id = aws_ec2_transit_gateway.tgw-dx-opsnet.id
}

#Create routes
resource “aws_ec2_transit_gateway_route” “some-routes” {
destination_cidr_block = “A.B.C.D/24”
transit_gateway_attachment_id = some vpc attachment id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.rt-A.id
}

#Associate route table to TGW attachments
#Associate to DXGW association
resource “aws_ec2_transit_gateway_route_table_association” “assoc-rt-A-to-dxg-attachment” {
transit_gateway_attachment_id = <how do I reference to the dxg transit gw association?>
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.rt-A.id
}

Thank you.

Hi,

Anyone have any ideas?

Hi @halphyr , from dx_gateway_association doc I can see, attribute dx_gateway_association_id .

So, can you try :

resource “aws_ec2_transit_gateway_route_table_association” “foobar” {
transit_gateway_attachment_id = aws_dx_gateway_association.dxg-to-tgw-assoc.dx_gateway_association_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.rt-A.id
}

or I see just id as attribute as well, so you can also try :

resource “aws_ec2_transit_gateway_route_table_association” “foobar” {
transit_gateway_attachment_id = aws_dx_gateway_association.dxg-to-tgw-assoc.id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.rt-A.id
}

Hi @tanmay-bhat,

I wasn’t sure that the dx_gateway_association_id or id attribute you mentioned was the same as the EC2 Transit Gateway Attachment identifier expected from the transit_gateway_attachment_id arguement for ec2_transit_gateway_route resource. Thats why I posed the question here.

I will confirm this once I have my Telco Direct Connect established and DXG and TGW associated. Thanks!

This seems to be the only way to get the TGW attachment ID.
Allow aws_dx_gateway_association to return transit_gateway_attachment_id when attaching TGW to DXGW · Issue #12646 · hashicorp/terraform-provider-aws · GitHub

I found that the module was failing to create certain DNS records due to inconsistencies in the input data. Despite providing all the required parameters, the Terraform apply command consistently returned an error indicating "InvalidInput: Invalid request provided: A record with the same name and type but different attributes already exists.