How to Import an AWS Transit Gateway into Terraform
terraback aws import --method bulk. Terraback writes the matching aws_ec2_transit_gateway resource block and the Terraform 1.5+ import block for you, so you do not have to run terraform import by hand. The import ID is the transit gateway ID (for example, tgw-0a1b2c3d4e5f6a7b8).Import AWS Transit Gateway with Terraback (recommended)
Terraback reverse-engineers your live infrastructure: it reads the Amazon VPC resource with read-only credentials, generates the HCL, and produces the exact import block. Two commands take you from a live AWS Transit Gateway to managed Terraform.
Scan your AWS account
terraback scan all aws --region us-east-1Generate import blocks and import into state
terraback aws import --method bulkThe Terraform import block
Terraback emits a Terraform 1.5+ import block like the one below. Because the block lives in your configuration, the import is reviewable in a pull request and repeatable across environments.
import {
to = aws_ec2_transit_gateway.main
id = "tgw-0a1b2c3d4e5f6a7b8"
}Example aws_ec2_transit_gateway configuration
Here is a realistic AWS Transit Gateway block. Terraback generates a fuller version from your actual resource attributes; this is a minimal, valid starting point.
resource "aws_ec2_transit_gateway" "main" {
description = "Central transit gateway"
amazon_side_asn = 64512
default_route_table_association = "enable"
default_route_table_propagation = "enable"
tags = {
Name = "main-tgw"
}
}Gotchas when importing a AWS Transit Gateway
- VPC attachments, route tables, routes, associations, and propagations are all separate resources (aws_ec2_transit_gateway_vpc_attachment, aws_ec2_transit_gateway_route_table, and so on) that must be imported individually.
- amazon_side_asn is immutable; a mismatch forces replacement of the entire gateway.
- The default association and propagation route tables are created implicitly and use 'enable'/'disable' string values, not booleans.
- Cross-account attachments shared via AWS RAM require importing the accepter side as well.
Doing it manually with terraform import
The native approach is to write the aws_ec2_transit_gateway block by hand, then run terraform import aws_ec2_transit_gateway.example <import-id> for every resource, one at a time. That works for a handful of resources, but it does not scale: you author all the HCL yourself and repeat the command for each item. Terraback generates the HCL and the import blocks for your whole account in one pass.
Import other AWS resources
Import your whole AWS account in minutes
Terraback scans 80+ AWS resource types and emits clean Terraform plus import blocks, running locally with read-only credentials. $499 once, no SaaS.