Amazon AWS Transit Gateway·续7
expect_failures = [var.cloudflare_anycast_ips] }
run "reject_vpn_prefix_overlapping_vpc" { command = plan
variables { vpn_routed_prefixes = ["192.168.64.0/18"] }
expect_failures = [var.vpn_routed_prefixes] }
run "accept_custom_tunnel_names" { command = plan
variables { cloudflare_tunnel_names = ["aws-usw1-tun-a", "aws-usw1-tun-b"] }
assert { condition = cloudflare_magic_wan_ipsec_tunnel.aws["01"].name == "aws-usw1-tun-a" && cloudflare_magic_wan_ipsec_tunnel.aws["02"].name == "aws-usw1-tun-b" error_message = "Supplied tunnel names must be applied in tunnel order." } }
run "reject_duplicate_tunnel_names" { command = plan
variables { cloudflare_tunnel_names = ["aws-tun", "aws-tun"] }
expect_failures = [var.cloudflare_tunnel_names] }
run "reject_mismatched_tunnel_name_count" { command = plan
variables { cloudflare_tunnel_names = ["aws-tun-01"] }
expect_failures = [var.cloudflare_tunnel_names] }
The configuration validates the following conditions before creating resources:
- The active AWS account matches `expected_account_id`
- The Region provides at least two Availability Zones
- All three subnet CIDRs are inside the VPC and do not overlap
- `vpn_routed_prefixes` contains unique canonical IPv4 networks more specific than `0.0.0.0/0` that do not overlap the VPC CIDR
- `tunnel_inside_cidrs` contains exactly two unique `/30` networks within `169.254.240.0/20` per configured anycast IP
- `vpn_preshared_keys` contains exactly two unique PSKs per configured anycast IP, and each PSK meets the AWS requirements of 8-64 letters, numbers, periods, or underscores without a leading `0`
- `cloudflare_anycast_ips` contains one or two distinct IPv4 addresses
- `cloudflare_asn` is `13335` or a private ASN from `64512` through `65534`, and differs from `aws_transit_gateway_asn`
- `cloudflare_tunnel_names` is empty or contains two unique names per configured anycast IP, using only letters, numbers, hyphens, and underscores
- The Cloudflare account ID is a 32-character hexadecimal identifier
- Additional advertised prefixes are unique IPv4 network CIDRs
Protect Terraform state
The AWS and Cloudflare providers store IPsec tunnel pre-shared keys in Terraform state as plaintext. Use an encrypted remote backend with restricted access for shared or production use. For local testing, protect `terraform.tfvars`, saved plan files, and all Terraform state files. Do not commit them to source control.
Public workload subnet
The workload subnet assigns public IPv4 addresses on launch and sends unmatched IPv4 destinations to the Internet Gateway. Direct Internet Gateway egress requires a public IPv4 address and appropriate network and host controls. The Internet Gateway does not perform NAT for private-only hosts.
Terraform does not create:
- Virtual machines
- Standalone Elastic Network Interfaces (ENIs); AWS creates ENIs for the Transit Gateway VPC attachment in the attachment subnets
- Security groups
- Elastic IP addresses
- Test hosts
Add and review those controls separately before deploying a workload.
### Initialize and validate Terraform
Run these commands from the Terraform directory you created:
```bash
terraform init
terraform fmt -check -recursive
terraform validate
terraform test
Initialization resolves the provider constraints, downloads the AWS and Cloudflare providers, and creates or updates .terraform.lock.hcl. Validation checks the Terraform syntax and provider schemas without creating resources. The tests use mock providers and do not create resources.
Review the Terraform plan
Create a saved plan:
terraform plan -out cloudflare-wan.tfplan
Review every proposed resource and value. For the baseline, the plan should create:
- One VPC
- One public workload subnet
- Two Transit Gateway attachment subnets
- One Internet Gateway
- One workload route table
- One Internet default route
- One Transit Gateway route for each
vpn_routed_prefixesentry - One Transit Gateway
- One dedicated Transit Gateway route table
- One VPC attachment
- One customer gateway
- One Site-to-Site VPN connection containing two AWS IPsec/BGP tunnels
- Two Cloudflare WAN IPsec tunnels with BGP
- The required Transit Gateway route-table associations and propagations
Secondary anycast IP customers
The plan also creates:
- One additional customer gateway
- One additional Site-to-Site VPN connection containing two AWS IPsec/BGP tunnels
- Two additional Cloudflare WAN IPsec tunnels with BGP
The plan should 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 services
Saved plan files can contain sensitive values. Protect and delete the plan file according to your organization's secrets-handling requirements.
Apply the Terraform plan
Apply the reviewed plan:
terraform apply cloudflare-wan.tfplan
Terraform waits for AWS to assign each VPN public IPv4 address before creating the corresponding Cloudflare tunnel. If an operation fails, correct the reported issue and run terraform plan again. Terraform retains successfully created resources in state and proposes only the remaining work.
If the apply creates the AWS resources but the Cloudflare requests fail with error 9106 stating that authorization headers are missing, CLOUDFLARE_API_TOKEN was not available to the Terraform process. Set the environment variable, create and review a fresh saved plan, and apply the new plan. Do not reuse the partially applied saved plan because Terraform state changed during the failed apply.
Cloudflare error 1016 with local IP not in Cloudflare assigned IPs means a configured Cloudflare anycast IP is not leased to the account identified by cloudflare_account_id. In this message, local IP refers to cloudflare_endpoint, not the link-local interface_address. Verify the account ID and every configured anycast IP under Address Space > Leased IPs, update terraform.tfvars, and create a fresh plan. Changing an anycast IP replaces its AWS customer gateway and updates the VPN connection to use the replacement gateway. This can interrupt its tunnels, so review the actions and use a maintenance window.
The Terraform configuration creates a replacement customer gateway before destroying the existing gateway. This ordering lets AWS retarget the VPN connection before the old customer gateway is removed. If an apply fails with IncorrectState: The customer gateway is in use, confirm that the aws_customer_gateway lifecycle includes create_before_destroy = true, discard the failed saved plan, and create a fresh plan.
Delete cloudflare-wan.tfplan after the apply completes. The saved plan can contain the VPN pre-shared keys.
Display Terraform outputs
Display the generated tunnel values after the apply completes:
terraform output cloudflare_tunnel_mapping
terraform output workload_egress_routes
The tunnel output provides each Cloudflare tunnel ID, AWS public IPv4 address, Cloudflare anycast IP, inside CIDR, AWS inside IP, and Cloudflare interface address. The egress output lists destinations routed through the VPN path and the Internet Gateway default. Terraform has already applied these values to the managed resources. The outputs are authoritative when you change any reference input.
| Tunnel | Inside CIDR | AWS inside IP | Cloudflare interface address |
|---|---|---|---|
cloudflare-wan-reference-tun-01 |
169.254.240.44/30 |
169.254.240.45 |
169.254.240.46/30 |
cloudflare-wan-reference-tun-02 |
169.254.240.48/30 |
169.254.240.49 |
169.254.240.50/30 |
The table uses the default deployment_name; the tunnel field in cloudflare_tunnel_mapping shows the actual names.
Secondary anycast IP customers
The second anycast IP adds tunnel 03 with 169.254.240.52/30, AWS address 169.254.240.53, and Cloudflare address 169.254.240.54/30; and tunnel 04 with 169.254.240.56/30, AWS address 169.254.240.57, and Cloudflare address 169.254.240.58/30.
Match tunnels by VPN connection and inside CIDR. Do not infer tunnel order by sorting the generated AWS public IPv4 addresses.
The table shows the reference values. If you changed the tunnel CIDRs, use only the values returned by Terraform.
Skip the manual Cloudflare tunnel and BGP creation procedures when using Terraform. Continue with Verification. Use the manual sections only to understand or audit the values Terraform configured.
Verify the Terraform deployment
Refresh Terraform state after the apply:
terraform apply -refresh-only
Review the refresh-only plan before approving it. Then display AWS tunnel telemetry and Cloudflare BGP status:
terraform output vpn_telemetry
terraform output cloudflare_bgp_status
terraform output workload_egress_routes
Every configured tunnel must report UP. The accepted route count should match the number of prefixes that Cloudflare advertises. Confirm in the Cloudflare dashboard that every configured BGP session is Established. Confirm that Cloudflare learned the VPC CIDR from AWS. Compare workload_egress_routes with the approved routing inventory: AWS longest-prefix matching sends a destination matching a configured VPN prefix to the Transit Gateway even though the Internet Gateway default also matches, while unmatched IPv4 destinations use 0.0.0.0/0. Complete every route, traffic, and failover check in Verification.
Also verify these resources in the AWS console:
- The VPC attachment and every configured VPN attachment use the dedicated Transit Gateway route table
- Propagation is active for the VPC and every configured VPN attachment
- The complete VPC CIDR appears as an active propagated route to the VPC attachment
- The expected RFC 1918 prefixes and any explicitly enabled public prefixes appear as active propagated routes through the VPN attachments
- The workload subnet is associated with the intended route table and enables public IPv4 assignment on launch
- Every
vpn_routed_prefixesdestination targets the Transit Gateway - The workload route table sends
0.0.0.0/0to the Internet Gateway, not the Transit Gateway - No VMs, standalone ENIs, security groups, Elastic IP addresses, NAT services, or test hosts were created by this configuration
Remove the Terraform deployment
Create and review a destroy plan:
terraform plan -destroy -out cloudflare-wan-destroy.tfplan
Apply the reviewed destroy plan:
terraform apply cloudflare-wan-destroy.tfplan
Terraform removes the managed Cloudflare tunnels before removing their AWS dependencies, including the selective Transit Gateway routes, Internet default route, Internet Gateway, and VPC resources. It removes only resources tracked in the current Terraform state. It does not remove manually created Cloudflare or AWS resources unless you imported them into this state.
Delete the saved destroy plan after use. Follow the retention policy for local or remote state versions that contain PSKs.
Resources
Cloudflare documentation
- Configure tunnel endpoints
- Configure BGP routes
- GRE and IPsec tunnels
- Anti-replay protection
- Maximum transmission unit and maximum segment size
- Traffic steering
- Tunnel health
AWS documentation
- AWS Site-to-Site VPN attachments in AWS Transit Gateway ↗︎
- Transit Gateway route tables ↗︎
- Transit Gateway route evaluation order ↗︎
- Tunnel options for AWS Site-to-Site VPN ↗︎
- Modify AWS Site-to-Site VPN tunnel options ↗︎
- Static and dynamic routing in AWS Site-to-Site VPN ↗︎
- AWS Site-to-Site VPN tunnel initiation options ↗︎
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/cloudflare-wan/configuration/third-party/aws/#page","headline":"Amazon AWS Transit Gateway","description":"Connect AWS Transit Gateway to Cloudflare WAN with two redundant IPsec tunnels and dynamic BGP routing.","url":"https://developers.cloudflare.com/cloudflare-wan/configuration/third-party/aws/","inLanguage":"en","image":"https://developers.cloudflare.com/cloudflare-wan/configuration/third-party/aws/og.png?v=79d35e3e8c3cc1bb","dateModified":"2026-10-15","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Connect Microsoft Azure Virtual WAN to Cloudflare WAN. title: Microsoft Azure Virtual WAN image: https://developers.cloudflare.com/cloudflare-wan/configuration/third-party/azure/azure-virtual-wan/og.png?v=3f997ff34b5ec3f6
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/cloudflare-wan/llms.txt
Use this file to discover all available pages before exploring further.
Docs