Skip to content
AnswerWell geo Documentation

Research · 9 min read · Answerwell

Transit Gateway IPsec BGP WAN Tunnel Architecture, Routing, and Troubleshooting

How to connect AWS Transit Gateway to Cloudflare WAN with redundant IPsec tunnels, BGP, split egress, and common routing mistakes.

This page summarizes Cloudflare’s documentation on connecting an Amazon Web Services (AWS) Transit Gateway to Cloudflare WAN using route-based IPsec tunnels and Border Gateway Protocol (BGP). The design is a reference example that Cloudflare has tested for compatibility; it is not described as an officially supported AWS configuration.

When you need Transit Gateway with Cloudflare WAN

Section titled “When you need Transit Gateway with Cloudflare WAN”

Teams typically look for this when AWS workloads in a VPC must reach prefixes on Cloudflare WAN (RFC 1918 and optionally public space you enable), while other traffic still exits the VPC directly to the Internet. You need dynamic routing—not static VPN routes—so multiple tunnels can stay active and withdraw paths when a session fails.

The baseline topology is:

  • One Cloudflare IPv4 anycast IP (a second anycast IP is optional)
  • One AWS customer gateway per anycast IP
  • One dynamic-routing AWS Site-to-Site VPN connection per customer gateway
  • Two AWS-managed IPsec tunnels per VPN connection (AWS does not offer single-tunnel Site-to-Site VPN)
  • One BGP session per tunnel

AWS Transit Gateway uses equal-cost multipath (ECMP) to distribute traffic across equal BGP paths. BGP dynamically exchanges routes and withdraws a path when its session fails. This design avoids the active/standby limitation of an AWS static-routing Site-to-Site VPN connection.

— Cloudflare, Amazon AWS Transit Gateway

Architecture: split egress and dedicated Transit Gateway route table

Section titled “Architecture: split egress and dedicated Transit Gateway route table”

A dedicated Transit Gateway route table controls what AWS advertises toward Cloudflare WAN. The workload VPC attaches to that Transit Gateway; VPN attachments associate with the same table. VPC and VPN propagations feed routes into it.

The reference split-egress pattern works like this:

Traffic destination VPC route table Transit Gateway behavior
Prefixes you approve for Cloudflare WAN (vpn_routed_prefixes in Terraform, or manual more-specific routes) Target: Transit Gateway Forwards into Site-to-Site VPN tunnels
All other IPv4 (0.0.0.0/0) Target: Internet Gateway (or NAT) Direct Internet egress, unchanged

AWS longest-prefix matching prefers your more-specific VPN routes over the default route. Do not point 0.0.0.0/0 at the Transit Gateway unless Cloudflare advertises an active default route and you intend Cloudflare Gateway egress; otherwise Internet-bound traffic can blackhole.

On Cloudflare, each leased anycast IP is global—not tied to one data center. Both tunnels in one VPN connection terminate on the same anycast IP because the customer gateway has one public IP.

Before building anything, confirm on Cloudflare WAN:

  • Unified Routing and BGP over IPsec are enabled (WAN configuration in the dashboard).
  • Leased anycast IPs (for example 162.159.135.1, or 172.64.135.1 for a second connection) appear under Address Space → Leased IPs.
  • The CF Account ASN (default 13335) is recorded; each AWS customer gateway uses this BGP ASN.
  • Eligible Unified Routing prefixes that AWS should learn are inventoried; an empty Additional advertised prefix list does not stop advertisements—Cloudflare still advertises eligible routes.

On AWS:

  • Transit Gateway with VPN ECMP support enabled and a private Amazon-side ASN unique among your Cloudflare WAN sites (example 65020; avoid reusing another site’s Transit Gateway ASN because of AS path loop rejection).
  • Workload VPC attached in at least two Availability Zones.
  • VPC CIDR and Cloudflare-learned prefixes do not overlap.

This configuration is a reference example demonstrating known compatibility with Cloudflare services. It is not an officially supported AWS configuration.

— Cloudflare, Amazon AWS Transit Gateway

Route-based VPN with BGP and active/active ECMP is the intended mode. Match these settings on AWS tunnel options and Cloudflare WAN tunnels:

Setting Value
IKE IKEv2, pre-shared key
Phase 1 AES-256-CBC, SHA-256, DH group 20, lifetime 28,800 s
Phase 2 AES-256-CBC, SHA-256, PFS group 20, lifetime 3,600 s
AWS replay window 2048 (maximum); enable replay protection on Cloudflare
DPD Restart
Tunnel startup (AWS) Start (IKEv2 only); AWS must initiate—Cloudflare responds to IKE and does not initiate
BGP No MD5 on Cloudflare; AWS initiates TCP 179 toward the Cloudflare interface address
ASNs AWS Transit Gateway: site-specific private ASN; customer gateway: CF Account ASN

Anti-replay on AWS cannot be disabled. Cloudflare notes replay protection can pin a tunnel to one server, which can reduce some anycast benefits.

AWS requires 8–64 characters, letters, numbers, periods, and underscores only, and the key must not begin with 0. Console workflows may reuse one PSK across tunnels; the Terraform reference requires a unique PSK per tunnel so the AWS provider can map each tunnel’s generated public IPv4 to the correct Cloudflare Customer endpoint.

Tunnel inside addressing: AWS /30 vs Cloudflare host format

Section titled “Tunnel inside addressing: AWS /30 vs Cloudflare host format”

AWS Site-to-Site VPN requires a unique /30 inside network per tunnel from 169.254.240.0/20. AWS takes the first usable host; Cloudflare uses the second as the IPv4 interface address with /30 prefix length—not the network address.

Side Example tunnel 01
AWS Inside IPv4 CIDR input 169.254.240.44/30
AWS inside IP (BGP) 169.254.240.45
Cloudflare interface address 169.254.240.46/30

AWS generates two outside public IPv4 addresses per VPN connection after creation. Each address is the IKE ID and must be set as Cloudflare Customer endpoint on the matching tunnel. Create the AWS VPN connection before Cloudflare tunnels, and pair tunnels by VPN connection ID and inside CIDR—not by sorting public IPs.

Enable on Cloudflare tunnels: health checks (bidirectional, medium rate), replay protection, Automatic return routing disabled (BGP handles paths). On each tunnel, BGP Customer device ASN is the Transit Gateway ASN (e.g. 65020).

Direction Source Control
AWS → Cloudflare Propagated routes in the dedicated Transit Gateway route table VPC + VPN attachment propagation
Cloudflare → AWS Unified Routing eligible routes + optional additional advertised prefixes Route inventory; block unwanted prefixes in AWS if needed

AWS advertises the VPC CIDR on every established BGP session. Cloudflare learns the VPC prefix over BGP—do not add a static Cloudflare route for the VPC CIDR. AWS accepts up to 1,000 routes per VPN connection and advertises up to 5,000 (fixed quotas).

Cloudflare preserves AS paths between WAN sites; AWS Transit Gateway has no allowas-in. AWS silently rejects routes from another Cloudflare site that reuses your Transit Gateway ASN. Give each AWS site a unique Transit Gateway ASN.

To stop AWS using a Cloudflare prefix, remove it from Cloudflare advertisement or add a blackhole static route for the exact CIDR in the dedicated Transit Gateway route table (static routes beat VPN-propagated routes for the same CIDR, not more-specific VPN routes).

Cloudflare recommends Terraform because AWS and Cloudflare settings are interdependent:

Terraform is the recommended deployment method for this integration because it consistently applies the interdependent AWS, tunnel, routing, and Cloudflare WAN settings described in this guide.

— Cloudflare, Amazon AWS Transit Gateway

Use one workflow only—do not mix manual and Terraform on the same deployment without import/state planning.

  1. Gather Cloudflare anycast IPs, CF Account ASN, and existing tunnel names or interface addresses (names and /30 addresses must be unique account-wide).

  2. Enable Transit Gateway VPN ECMP; create a dedicated route table; create customer gateway(s) pointing at Cloudflare anycast IP(s).

  3. Create dynamic Site-to-Site VPN connection(s) to the Transit Gateway; configure both tunnels (IKE/IPsec, inside /30, PSK, startup Start).

  4. Record each tunnel’s AWS public IPv4; create matching Cloudflare WAN IPsec tunnels with Customer endpoint set to those addresses.

  5. Enable BGP on each Cloudflare tunnel; associate and propagate VPC and VPN attachments in the dedicated route table; add VPC routes for approved destinations only.

  6. Verify tunnels UP, BGP Established, route counts, and end-to-end traffic—including failover on one tunnel.

The same guide ships a proof-of-concept module (Terraform ≥ 1.11.0, hashicorp/aws ~> 6.67.0, cloudflare/cloudflare ~> 5.27.0). It creates VPC, public workload subnet, Internet Gateway, selective Transit Gateway routes, VPN(s), and Cloudflare tunnels with BGP. It does not create VMs, security groups, NAT, or Cloudflare firewall policies.

Set CLOUDFLARE_API_TOKEN with Magic WAN Write or Magic Transit Write; guard AWS account via expected_account_id. PSKs and tokens live in state and plan files—use an encrypted remote backend and never commit terraform.tfvars.

After apply, use terraform output cloudflare_tunnel_mapping and terraform output workload_egress_routes as the authoritative mapping.

AWS VPC routing vs Cloudflare public egress (172.64.0.0/13)

Section titled “AWS VPC routing vs Cloudflare public egress (172.64.0.0/13)”

Separate from tunnel setup, workloads that reach Cloudflare over the public Internet can fail if VPC routes steal 172.64.x.x traffic. Cloudflare uses 172.64.0.0/13 as public egress space—it is not RFC 1918 (172.16.0.0/12 is private and does not overlap).

If this route points to an internal target rather than an Internet Gateway, it can capture Cloudflare’s 172.64.x.x traffic before it reaches the Internet Gateway, causing connection errors (521, 522) from Cloudflare data centers that use this range.

— Cloudflare, Cloudflare IP addresses

Fix by adding a more-specific route 172.64.0.0/13 → Internet Gateway, or narrowing a broad 172.x route to 172.16.0.0/12 only. Security groups will not surface this problem—it is routing-layer only.

This differs from using 172.64.135.1 as a leased WAN anycast endpoint in the Transit Gateway guide; that anycast is for IPsec termination, not the same issue as mistaken RFC-style routing to Transit Gateway for 172.64.0.0/13.

Symptom Likely cause What to check
All tunnels down IKE mismatch or wrong anycast on customer gateway Startup Start, IKEv2/AES-256/SHA-256/group 20, PSK match, customer gateway IP = Cloudflare anycast
One tunnel down Wrong Customer endpoint or duplicated /30/PSK AWS tunnel’s public IP and inside CIDR for that tunnel only
IPsec up, BGP down ASN or interface host wrong Customer gateway ASN = CF Account ASN; Cloudflare Customer device ASN = Transit Gateway ASN; Cloudflare host = second address in /30; no BGP MD5 on Cloudflare
BGP up, no routes Propagation or loop prevention VPN and VPC propagation in dedicated table; unique Transit Gateway ASN per site; Cloudflare routes eligible in Unified Routing
Routes but no traffic Subnet or security path Workload route to Transit Gateway for destination; return route for VPC CIDR on Cloudflare; security groups, NACLs, host firewalls, Cloudflare network firewall
Fewer tunnels used than expected ECMP hashes flows VPN ECMP enabled; all sessions up; equivalent BGP attributes; ECMP spreads flows, not packets—few test flows may use one tunnel

Modifying AWS VPN tunnel options restarts tunnels (often several minutes of disruption)—use a maintenance window.

  1. In the AWS VPC console, open each Site-to-Site VPN connection and confirm both tunnels show UP and AcceptedRouteCount matches your expected Cloudflare prefix inventory (verification steps).

  2. In the Transit Gateway route table used for Cloudflare, confirm the VPC CIDR is propagated to the VPC attachment and Cloudflare prefixes appear via VPN attachments.

  3. In Cloudflare, under Network health, confirm IPsec tunnels are healthy and each BGP session is Established; under Routes, confirm the VPC prefix is learned via AWS BGP sessions.

  4. From a workload subnet, test approved destinations over the VPN path and an unlisted destination over Internet Gateway egress; during a window, disable one tunnel and confirm traffic shifts after BGP convergence.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.