- Amazon AWS Transit Gateway·续3
- Keep provider behavior within the minor versions tested by this guide.
Amazon AWS Transit Gateway·续3
Trace the route at all three forwarding layers:
- The workload subnet route table must send the destination prefix to the Transit Gateway.
- The dedicated Transit Gateway route table must send the destination through the VPN attachments.
- Cloudflare WAN must have a return route for
192.168.0.0/16learned from the AWS BGP sessions.
If only one direction works, compare source and destination prefixes in the flow logs and confirm the return route is present rather than assuming the IPsec tunnel is the cause.
Tunnel health is degraded but IPsec is up
- Confirm the health check rate is Medium, type is Request, and direction is Bidirectional
- Confirm no custom health check target is set; the target should be the AWS inside address in the same
/30 - Verify the Cloudflare interface and AWS inside addresses belong to the same
/30 - Confirm the corresponding AWS tunnel is using the expected inside CIDR
- Check whether a network policy blocks ICMP to the AWS tunnel inside address
Traffic uses fewer tunnels than expected
- Confirm VPN ECMP support is enabled on the Transit Gateway
- Confirm every configured IPsec tunnel and BGP session is up
- Confirm each tunnel advertises the same prefix with equivalent BGP attributes
- Confirm every configured VPN attachment is associated with and propagated into the dedicated route table
- Remember that ECMP distributes flows, not individual packets; a small number of test flows may not use every tunnel
Unexpected prefixes appear in AWS or Cloudflare
- Audit every route and propagation in the dedicated Transit Gateway route table
- Confirm the workload VPC attachment and every configured VPN attachment are propagated
- Confirm no unrelated attachment is propagated into the dedicated table
- Review the eligible route inventory in the Unified Routing dataplane and each tunnel's Additional advertised prefix list
- Remove an unintended route at its source rather than relying on a less-specific route to override it
Small packets work but applications fail
- Do not rely on Path MTU Discovery, which AWS Site-to-Site VPN does not support
- Use controlled packet-size and DF-bit tests to identify the effective path MTU
- Apply a TCP MSS clamp of no more than
1360bytes where the architecture permits - Check whether ICMP fragmentation-needed messages are filtered
Design notes
- Each AWS Site-to-Site VPN connection provides two tunnels in different AWS Availability Zones
- AWS does not support creating a single-tunnel Site-to-Site VPN connection; both tunnels use the same customer gateway and therefore the same Cloudflare anycast IP
- The baseline VPN connection provides two paths through one Cloudflare anycast IP; a second anycast IP adds a second VPN connection and two more paths
- AWS requires
/30inside tunnel networks, so this integration cannot use the/31interface addresses that Cloudflare recommends for other devices - AWS Transit Gateway can use ECMP across equal BGP paths when VPN ECMP support is enabled
- Cloudflare automatically withdraws a route learned from a failed BGP session while equivalent paths remain available through healthy sessions
- Standard AWS Site-to-Site VPN tunnels support the AWS-published bandwidth limit per tunnel; Transit Gateway also supports Large Bandwidth Tunnels in eligible Regions, but validate Cloudflare interoperability and commercial requirements before using that option
- Modifying an AWS VPN tunnel option can interrupt that tunnel for several minutes
- Configure every tunnel with an 8-64 character PSK containing only letters, numbers, periods, and underscores; a PSK cannot begin with
0, and console-configured tunnels can use unique PSKs or reuse one PSK across all tunnels, while the Terraform workflow requires a unique PSK for each tunnel
Summary
The primary topology connects AWS Transit Gateway through:
- One Cloudflare anycast IP
- One AWS customer gateway
- One AWS Site-to-Site VPN connection
- Two IPsec tunnels and two BGP sessions
Secondary anycast IP customers
Add:
- One additional Cloudflare anycast IP
- One additional AWS customer gateway
- One additional AWS Site-to-Site VPN connection
- Two additional IPsec tunnels and two additional BGP sessions
For each configured anycast IP, the Terraform workflow requires:
- Exactly two unique
/30tunnel CIDRs - Exactly two AWS-compatible pre-shared keys
Configuration record
Use this table during implementation and handoff. Do not record pre-shared key values in the document.
| Placeholder | Description |
|---|---|
<AWS_TGW_ID> |
Transit Gateway ID |
<AWS_TGW_ROUTE_TABLE_ID> |
Dedicated route table associated with the workload and VPN attachments |
<AWS_VPC_ATTACHMENT_ID> |
Workload VPC attachment ID |
<AWS_CUSTOMER_GATEWAY_01_ID> |
Customer gateway representing 162.159.135.1 |
<AWS_VPN_CONNECTION_01_ID> |
VPN connection using 162.159.135.1 |
<AWS_OUTSIDE_IP_01> |
AWS public IPv4 address for VPN 1, tunnel 1 |
<AWS_OUTSIDE_IP_02> |
AWS public IPv4 address for VPN 1, tunnel 2 |
<TUNNEL_01_PSK> and <TUNNEL_02_PSK> |
Pre-shared keys for tunnels 01 and 02 |
<EXPECTED_RFC1918_PREFIX_LIST> |
RFC 1918 prefixes AWS is expected to learn from Cloudflare WAN |
Secondary anycast IP customers
Also record:
<AWS_CUSTOMER_GATEWAY_02_ID>for the customer gateway representing172.64.135.1<AWS_VPN_CONNECTION_02_ID>for the VPN connection using172.64.135.1<AWS_OUTSIDE_IP_03>for VPN 2, tunnel 1<AWS_OUTSIDE_IP_04>for VPN 2, tunnel 2<TUNNEL_03_PSK>for tunnel 03<TUNNEL_04_PSK>for tunnel 04
The completed record should let an operator correlate an AWS tunnel alarm, Cloudflare tunnel-health event, BGP session, and route-table path without guessing which resources belong together.
Terraform reference deployment
The self-contained Terraform configuration creates a minimal proof of concept with:
- One workload VPC
- One public workload subnet
- Two Transit Gateway attachment subnets in separate Availability Zones
- One Internet Gateway
- One workload route table with split-egress routes
- One Transit Gateway and one dedicated Transit Gateway route table
- One VPC attachment
- One customer gateway and one Site-to-Site VPN connection per configured anycast IP
- Two AWS IPsec/BGP tunnels per VPN connection
- Two matching Cloudflare WAN tunnels with BGP per VPN connection
- The required Transit Gateway route-table associations and propagations
The configuration does not create:
- Virtual machines or test hosts
- Standalone Elastic Network Interfaces (ENIs); AWS creates ENIs for the Transit Gateway VPC attachment in the attachment subnets
- Security groups
- Elastic IP addresses
- NAT gateways or other NAT services
- Cloudflare routes
- Cloudflare network firewall policies
AWS assigns two public IPv4 addresses per VPN connection for use as the Cloudflare Customer endpoint values.
The deployment propagates the complete VPC CIDR into the Transit Gateway route table for advertisement to Cloudflare and propagates all routes learned from Cloudflare into that table. In the VPC workload route table, each configured vpn_routed_prefixes destination is a more-specific route to the Transit Gateway and Site-to-Site VPNs, while 0.0.0.0/0 sends all remaining IPv4 egress directly through the Internet Gateway. AWS longest-prefix matching selects a configured VPN route over the Internet default when both match.
The workload subnet enables public IPv4 assignment on launch, but this configuration does not create a workload to receive an address. Direct Internet Gateway egress requires a public IPv4 address on the workload ENI and appropriate security controls; an Internet Gateway does not provide NAT for private-only hosts.
Route availability for VPN-routed prefixes
A destination matching vpn_routed_prefixes has no fallback to the Internet Gateway and will blackhole unless the Transit Gateway has an active matching route learned through the AWS VPN attachments for Cloudflare WAN.
Billable AWS resources
Terraform creates billable AWS resources. Run this deployment in a non-production AWS account. Review the plan against your organization's security and tagging requirements. Remove the resources when testing is complete.
Reference deployment layout
| Resource | Reference value |
|---|---|
| AWS Region | us-east-2 |
| VPC CIDR | 192.168.0.0/16 |
| Public workload subnet | 192.168.125.0/24 |
| Transit Gateway attachment subnet A | 192.168.124.0/28 |
| Transit Gateway attachment subnet B | 192.168.124.16/28 |
| VPN-routed prefix example | 172.16.0.0/16 |
| Remaining IPv4 egress | Internet Gateway |
| AWS Transit Gateway ASN | 65020 |
| CF Account ASN | 13335 (default) |
| Cloudflare anycast IP 1 | 162.159.135.1 |
The baseline reference layout uses one anycast IP.
Secondary anycast IP customers
Use 172.64.135.1 as the second anycast IP.
Terraform prerequisites
Install Terraform 1.11.0 or later. The included tests use override_during, which requires Terraform 1.11.0. Configure AWS credentials for the target account. Discover the active AWS account ID with the following command:
aws sts get-caller-identity --query Account --output text
Set the returned value as expected_account_id. The Terraform AWS provider verifies the active account against this value before creating resources.
Create a Cloudflare API token scoped to the target account with Magic WAN Write or Magic Transit Write permission. Make the token available to the Terraform process as CLOUDFLARE_API_TOKEN:
macOS or Linux:
export CLOUDFLARE_API_TOKEN="<API_TOKEN>"
PowerShell:
$env:CLOUDFLARE_API_TOKEN = "<API_TOKEN>"
Run terraform plan and terraform apply from the same environment where the token is set. In the Cloudflare dashboard, select the target account and copy its Account ID from the account overview; avoid relying on a fixed navigation path because the dashboard can change. Set this 32-character value as cloudflare_account_id in terraform.tfvars. Set cloudflare_asn to the account's CF Account ASN, recorded under Networking > Routes > WAN configuration. Terraform accepts 13335 or a private ASN from 64512 through 65534 that differs from aws_transit_gateway_asn. Terraform does not change the CF Account ASN because Cloudflare provider v5.27 does not expose the account-wide BGP settings as a resource.
Confirm that every value in cloudflare_anycast_ips appears under Address Space > Leased IPs in the account identified by cloudflare_account_id. These values are the public Cloudflare anycast IPs, not Cloudflare WAN routed prefixes or link-local tunnel interface addresses.
Create an empty Terraform directory and add the following files from the complete listings in this guide:
| File | Purpose |
|---|---|
versions.tf |
Configures Terraform, the AWS and Cloudflare providers, and account guard |
variables.tf |
Declares and validates all deployment inputs |
main.tf |
Creates the AWS network, IGW, selective routes, Transit Gateway, and VPNs |
cloudflare.tf |
Creates the Cloudflare WAN tunnels, health checks, and BGP sessions |
outputs.tf |
Returns resource IDs, egress routes, tunnel mappings, and status |
terraform.tfvars.example |
Provides the reference input values for one anycast IP |
terraform.tfvars.two-anycast-ips.example |
Provides the reference input values for two anycast IPs |
tests/reference.tftest.hcl |
Tests reference invariants and invalid inputs with mock providers |
Do not create .terraform.lock.hcl from a listing. terraform init generates it from the provider constraints, and you should commit the generated file to preserve the provider selections reviewed for your deployment. A .gitignore and README.md are optional repository support files and are not required to execute this configuration or its tests, so they are not included.
Existing deployments
Terraform does not automatically adopt AWS or Cloudflare resources created outside this configuration, such as by the console workflow in this guide. Import each existing resource into Terraform state, or remove the existing deployment before applying this configuration. Do not apply it over an existing deployment without first choosing one of those options.
Existing Cloudflare tunnels
Terraform does not automatically adopt manually created Cloudflare tunnels. Import matching tunnels before applying this configuration, or remove them first.
Cloudflare rejects a tunnel whose name or interface address matches another tunnel in the same Cloudflare account, including tunnels from another deployment of this configuration. Before applying, review the existing tunnels on the Connectors page and choose:
- A
deployment_nameorcloudflare_tunnel_namesvalue that produces names no other tunnel uses tunnel_inside_cidrsvalues whose Cloudflare interface addresses do not overlap any existing tunnel
To list the names and interface addresses already in use, refer to Check existing tunnel names and interface addresses.
Configure the Terraform variables
After creating the files below, copy terraform.tfvars.example to terraform.tfvars. Replace both account IDs and the two PSK placeholders.
Secondary anycast IP customers
Copy terraform.tfvars.two-anycast-ips.example to terraform.tfvars instead, and replace all four PSK placeholders.
Terraform derives the deployment size from the number of entries in cloudflare_anycast_ips. One anycast IP creates one customer gateway, one Site-to-Site VPN connection, and two Cloudflare WAN tunnels. Two anycast IPs create two of each AWS resource and four Cloudflare WAN tunnels. Validation rejects zero or more than two anycast IPs, duplicate anycast IPs, and any tunnel CIDR or PSK count other than two per anycast IP.
Cloudflare tunnel names default to <deployment_name>-tun-01, -02, and so on. To choose the names, set cloudflare_tunnel_names to two unique names per anycast IP in tunnel order. Names can contain letters, numbers, hyphens, and underscores, and must be unique within the Cloudflare account.
Configure exactly two tunnel CIDRs and two PSKs per anycast IP. Every PSK must be unique. Review every CIDR, anycast IP, ASN, vpn_routed_prefixes destination, and additional advertised prefix. The VPN-routed list accepts unique canonical IPv4 prefixes more specific than 0.0.0.0/0 that do not overlap the VPC CIDR; use it only for destinations that should traverse Cloudflare WAN.
The anycast IP list order defines Terraform resource identity. Keep the first anycast IP unchanged. Append a second anycast IP to expand, or remove only the final anycast IP to shrink. Reordering anycast IPs or replacing the first anycast IP can replace every customer gateway and VPN path, causing an outage. Review those changes in a maintenance window.
The resource_ids output returns vpn_connection_ids and vpn_attachment_ids maps keyed by "01" and optional "02".
Each customer gateway uses the device name <deployment_name>-cgw-NN, so AWS creates a distinct customer gateway instead of returning an existing gateway with the same IP address and ASN. Changing deployment_name replaces every customer gateway and briefly interrupts each VPN connection. Review those changes in a maintenance window.
The aws_transit_gateway_asn default is 65020. AWS does not allow changing the ASN of a Transit Gateway that has VPN attachments, so set aws_transit_gateway_asn before the first apply and do not change it afterward.
The empty cloudflare_additional_advertised_prefixes list does not suppress routes already eligible for BGP advertisement from the Cloudflare routing table. Add only prefixes that Cloudflare must advertise in addition to those routes.
Complete Terraform configuration
versions.tf
Create versions.tf to constrain Terraform and provider versions, configure provider authentication, guard the AWS account, and apply default tags.
```hcl terraform { # terraform test uses override_during, which requires Terraform 1.11.0 or later. required_version = ">= 1.11.0"
Keep provider behavior within the minor versions tested by this guide.
required_providers { aws = { source = "hashicorp/aws" version = "~> 6.67.0" }
