- Amazon AWS Transit Gateway·续5
- Route only approved destination prefixes through the Transit Gateway. AWS
- longest-prefix matching gives these routes precedence over the Internet default.
- All destinations not selected above use direct Internet Gateway egress.
- Each VPN consumes the two tunnel inputs assigned to its anycast IP.
Amazon AWS Transit Gateway·续5
tags = { Name = "${var.deployment_name}-tgw" } }
resource "aws_ec2_transit_gateway_route_table" "cloudflare" { transit_gateway_id = aws_ec2_transit_gateway.cloudflare.id
tags = { Name = "${var.deployment_name}-tgw-rt" } }
resource "aws_ec2_transit_gateway_vpc_attachment" "workload" { subnet_ids = [aws_subnet.tgw_a.id, aws_subnet.tgw_b.id] transit_gateway_id = aws_ec2_transit_gateway.cloudflare.id vpc_id = aws_vpc.workload.id appliance_mode_support = "disable" dns_support = "enable" ipv6_support = "disable" security_group_referencing_support = "disable" transit_gateway_default_route_table_association = false transit_gateway_default_route_table_propagation = false
tags = { Name = "${var.deployment_name}-vpc-attachment" } }
resource "aws_ec2_transit_gateway_route_table_association" "workload" { transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.workload.id transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.cloudflare.id }
resource "aws_ec2_transit_gateway_route_table_propagation" "workload" { transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.workload.id transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.cloudflare.id }
Route only approved destination prefixes through the Transit Gateway. AWS
longest-prefix matching gives these routes precedence over the Internet default.
resource "aws_route" "cloudflare" { for_each = toset(var.vpn_routed_prefixes)
route_table_id = aws_route_table.workload.id destination_cidr_block = each.value transit_gateway_id = aws_ec2_transit_gateway.cloudflare.id
depends_on = [aws_ec2_transit_gateway_vpc_attachment.workload] }
All destinations not selected above use direct Internet Gateway egress.
resource "aws_route" "internet" { route_table_id = aws_route_table.workload.id destination_cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.workload.id }
resource "aws_customer_gateway" "cloudflare" { for_each = local.customer_gateways
bgp_asn = var.cloudflare_asn ip_address = each.value.cloudflare_anycast_ip type = "ipsec.1" # AWS returns an existing customer gateway for an identical type, IP address, # and ASN unless the device name differs, so this name keeps the gateway unique. device_name = "${var.deployment_name}-cgw-${each.key}"
tags = { Name = "${var.deployment_name}-cgw-${each.key}" }
lifecycle { # AWS cannot delete an in-use customer gateway, so establish its replacement first. create_before_destroy = true } }
resource "aws_vpn_connection" "cloudflare" { for_each = local.customer_gateways
customer_gateway_id = aws_customer_gateway.cloudflare[each.key].id transit_gateway_id = aws_ec2_transit_gateway.cloudflare.id type = "ipsec.1" static_routes_only = false preshared_key_storage = "Standard"
local_ipv4_network_cidr = "0.0.0.0/0" remote_ipv4_network_cidr = "0.0.0.0/0" outside_ip_address_type = "PublicIpv4" tunnel_bandwidth = "standard" tunnel_inside_ip_version = "ipv4"
Each VPN consumes the two tunnel inputs assigned to its anycast IP.
tunnel1_inside_cidr = var.tunnel_inside_cidrs[each.value.tunnel1_index] tunnel1_preshared_key = var.vpn_preshared_keys[each.value.tunnel1_index] tunnel1_replay_window_size = 2048 tunnel1_dpd_timeout_action = "restart" tunnel1_phase1_encryption_algorithms = ["AES256"] tunnel1_phase2_encryption_algorithms = ["AES256"] tunnel1_phase1_integrity_algorithms = ["SHA2-256"] tunnel1_phase2_integrity_algorithms = ["SHA2-256"] tunnel1_phase1_dh_group_numbers = [20] tunnel1_phase2_dh_group_numbers = [20] tunnel1_phase1_lifetime_seconds = 28800 tunnel1_phase2_lifetime_seconds = 3600 tunnel1_ike_versions = ["ikev2"] tunnel1_startup_action = "start"
tunnel2_inside_cidr = var.tunnel_inside_cidrs[each.value.tunnel2_index] tunnel2_preshared_key = var.vpn_preshared_keys[each.value.tunnel2_index] tunnel2_replay_window_size = 2048 tunnel2_dpd_timeout_action = "restart" tunnel2_phase1_encryption_algorithms = ["AES256"] tunnel2_phase2_encryption_algorithms = ["AES256"] tunnel2_phase1_integrity_algorithms = ["SHA2-256"] tunnel2_phase2_integrity_algorithms = ["SHA2-256"] tunnel2_phase1_dh_group_numbers = [20] tunnel2_phase2_dh_group_numbers = [20] tunnel2_phase1_lifetime_seconds = 28800 tunnel2_phase2_lifetime_seconds = 3600 tunnel2_ike_versions = ["ikev2"] tunnel2_startup_action = "start"
tags = { Name = "${var.deployment_name}-vpn-${each.key}" } }
resource "aws_ec2_transit_gateway_route_table_association" "vpn" { for_each = aws_vpn_connection.cloudflare
transit_gateway_attachment_id = each.value.transit_gateway_attachment_id transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.cloudflare.id }
resource "aws_ec2_transit_gateway_route_table_propagation" "vpn" { for_each = aws_vpn_connection.cloudflare
transit_gateway_attachment_id = each.value.transit_gateway_attachment_id transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.cloudflare.id }
#### `cloudflare.tf`
Create `cloudflare.tf` to map the AWS tunnels to Cloudflare WAN, select Cloudflare host addresses, and enable health checks and BGP for every configured anycast IP.
```hcl
locals {
# Cloudflare tunnel names must be unique within the account. Use the supplied names, or
# prefix generated names with deployment_name. Periods are replaced because tunnel names
# avoid special characters.
cloudflare_tunnel_name_prefix = replace(var.deployment_name, ".", "-")
# Expand each anycast IP and AWS VPN connection into two Cloudflare WAN tunnel objects. Tunnel keys
# remain globally ordered as 01-02 for anycast IP 1 and 03-04 for anycast IP 2.
cloudflare_tunnels = merge([
for vpn_key, vpn in local.customer_gateways : {
(format("%02d", vpn.tunnel1_index + 1)) = {
name = length(var.cloudflare_tunnel_names) > 0 ? var.cloudflare_tunnel_names[vpn.tunnel1_index] : "${local.cloudflare_tunnel_name_prefix}-tun-${format("%02d", vpn.tunnel1_index + 1)}"
vpn_connection_key = vpn_key
cloudflare_anycast_ip = vpn.cloudflare_anycast_ip
customer_endpoint = aws_vpn_connection.cloudflare[vpn_key].tunnel1_address
aws_bgp_holdtime = aws_vpn_connection.cloudflare[vpn_key].tunnel1_bgp_holdtime
inside_cidr = var.tunnel_inside_cidrs[vpn.tunnel1_index]
psk = var.vpn_preshared_keys[vpn.tunnel1_index]
}
(format("%02d", vpn.tunnel2_index + 1)) = {
name = length(var.cloudflare_tunnel_names) > 0 ? var.cloudflare_tunnel_names[vpn.tunnel2_index] : "${local.cloudflare_tunnel_name_prefix}-tun-${format("%02d", vpn.tunnel2_index + 1)}"
vpn_connection_key = vpn_key
cloudflare_anycast_ip = vpn.cloudflare_anycast_ip
customer_endpoint = aws_vpn_connection.cloudflare[vpn_key].tunnel2_address
aws_bgp_holdtime = aws_vpn_connection.cloudflare[vpn_key].tunnel2_bgp_holdtime
inside_cidr = var.tunnel_inside_cidrs[vpn.tunnel2_index]
psk = var.vpn_preshared_keys[vpn.tunnel2_index]
}
}
]...)
}
resource "cloudflare_magic_wan_ipsec_tunnel" "aws" {
for_each = local.cloudflare_tunnels
account_id = var.cloudflare_account_id
name = each.value.name
description = "AWS Transit Gateway tunnel ${each.key} managed by Terraform"
customer_endpoint = each.value.customer_endpoint
cloudflare_endpoint = each.value.cloudflare_anycast_ip
# AWS uses host 1 and Cloudflare uses host 2 in each /30 inside network.
interface_address = "${cidrhost(each.value.inside_cidr, 2)}/30"
# The provider writes this secret to Terraform state; protect the backend.
psk = each.value.psk
replay_protection = true
# BGP and the Cloudflare routing table, not automatic return routing, select paths.
automatic_return_routing = false
# Leave target unset so bidirectional checks use the default target: the AWS
# inside address in the same /30.
health_check = {
enabled = true
rate = "mid"
type = "request"
direction = "bidirectional"
}
bgp = {
customer_asn = var.aws_transit_gateway_asn
extra_prefixes = var.cloudflare_additional_advertised_prefixes
}
}
outputs.tf
Create outputs.tf to expose resource IDs, workload egress routes, ordered tunnel mappings, and provider-refreshed status for every configured anycast IP.
output "resource_ids" {
description = "AWS resource IDs used to verify the deployment."
value = {
vpc_id = aws_vpc.workload.id
workload_subnet_id = aws_subnet.workload.id
internet_gateway_id = aws_internet_gateway.workload.id
transit_gateway_id = aws_ec2_transit_gateway.cloudflare.id
transit_gateway_route_table = aws_ec2_transit_gateway_route_table.cloudflare.id
vpc_attachment_id = aws_ec2_transit_gateway_vpc_attachment.workload.id
vpn_connection_ids = { for key, vpn in aws_vpn_connection.cloudflare : key => vpn.id }
vpn_attachment_ids = { for key, vpn in aws_vpn_connection.cloudflare : key => vpn.transit_gateway_attachment_id }
cloudflare_tunnel_ids = { for key, tunnel in cloudflare_magic_wan_ipsec_tunnel.aws : key => tunnel.id }
}
}
output "workload_egress_routes" {
description = "Workload route-table destinations split between the VPN and Internet Gateway paths."
value = {
vpn = sort(var.vpn_routed_prefixes)
internet_default = aws_route.internet.destination_cidr_block
}
}
output "bgp_asns" {
description = "BGP ASNs used to configure Cloudflare WAN."
value = {
aws_transit_gateway = var.aws_transit_gateway_asn
cloudflare = var.cloudflare_asn
}
}
output "cloudflare_tunnel_mapping" {
description = "AWS and Cloudflare values for the two or four Terraform-managed tunnels."
# Keep this list in tunnel-number order for direct operational correlation.
value = [
for key in sort(keys(local.cloudflare_tunnels)) : {
tunnel = cloudflare_magic_wan_ipsec_tunnel.aws[key].name
vpn_connection_id = aws_vpn_connection.cloudflare[local.cloudflare_tunnels[key].vpn_connection_key].id
aws_outside_ip = local.cloudflare_tunnels[key].customer_endpoint
cloudflare_anycast_ip = local.cloudflare_tunnels[key].cloudflare_anycast_ip
inside_cidr = local.cloudflare_tunnels[key].inside_cidr
aws_inside_ip = cidrhost(local.cloudflare_tunnels[key].inside_cidr, 1)
cloudflare_interface_address = "${cidrhost(local.cloudflare_tunnels[key].inside_cidr, 2)}/30"
# AWS sets this value; Cloudflare negotiates the lower of it and 240 seconds.
aws_bgp_holdtime = local.cloudflare_tunnels[key].aws_bgp_holdtime
cloudflare_tunnel_id = cloudflare_magic_wan_ipsec_tunnel.aws[key].id
}
]
}
output "cloudflare_bgp_status" {
description = "Cloudflare BGP status refreshed during the most recent Terraform operation."
# Provider status is a state snapshot, not continuous monitoring telemetry.
value = {
for key, tunnel in cloudflare_magic_wan_ipsec_tunnel.aws : key => tunnel.bgp_status
}
}
output "vpn_telemetry" {
description = "AWS tunnel telemetry refreshed during the most recent Terraform operation."
# Refresh state before relying on these provider-reported values.
value = {
for key, vpn in aws_vpn_connection.cloudflare : key => vpn.vgw_telemetry
}
}
terraform.tfvars.example
Create terraform.tfvars.example as the reviewed input template. Copy it to terraform.tfvars and replace every placeholder before planning. It configures the baseline of one anycast IP, two tunnel CIDRs, and two PSKs. Customers with two anycast IPs use the following template instead.
expected_account_id = "REPLACE_WITH_AWS_ACCOUNT_ID"
cloudflare_account_id = "REPLACE_WITH_CLOUDFLARE_ACCOUNT_ID"
deployment_name = "cloudflare-wan-reference"
aws_region = "us-east-2"
# These three subnets must be canonical, non-overlapping networks inside the VPC.
vpc_cidr = "192.168.0.0/16"
workload_subnet_cidr = "192.168.125.0/24"
tgw_subnet_a_cidr = "192.168.124.0/28"
tgw_subnet_b_cidr = "192.168.124.16/28"
# Only these destinations use the Site-to-Site VPNs. All other IPv4 traffic
# uses the Internet Gateway default route.
vpn_routed_prefixes = [
"172.16.0.0/16",
]
# Accounts using one anycast IP have two tunnels.
tunnel_inside_cidrs = [
"169.254.240.44/30",
"169.254.240.48/30",
]
# PSKs are sensitive and persist in Terraform state; never commit terraform.tfvars.
vpn_preshared_keys = [
"REPLACE_WITH_UNIQUE_TUNNEL_01_PSK",
"REPLACE_WITH_UNIQUE_TUNNEL_02_PSK",
]
# One anycast IP is the baseline and backs tunnels 01-02.
cloudflare_anycast_ips = ["162.159.135.1"]
aws_transit_gateway_asn = 65020
# Set to the CF Account ASN shown under Networking > Routes > WAN configuration.
cloudflare_asn = 13335
cloudflare_additional_advertised_prefixes = []
# Optional. Cloudflare tunnel names must be unique in the Cloudflare account.
# Leave empty to use <deployment_name>-tun-01, -02, and so on, or list two names
# per anycast IP in tunnel order, for example:
# cloudflare_tunnel_names = ["aws-tun-01", "aws-tun-02"]
cloudflare_tunnel_names = []
terraform.tfvars.two-anycast-ips.example
Create terraform.tfvars.two-anycast-ips.example as the input template for customers with two anycast IPs. Copy it to terraform.tfvars instead of terraform.tfvars.example. It configures two anycast IPs, four tunnel CIDRs, and four PSKs, which creates two customer gateways, two Site-to-Site VPN connections, and four Cloudflare WAN tunnels.
expected_account_id = "REPLACE_WITH_AWS_ACCOUNT_ID"
cloudflare_account_id = "REPLACE_WITH_CLOUDFLARE_ACCOUNT_ID"
deployment_name = "cloudflare-wan-reference"
aws_region = "us-east-2"
# These three subnets must be canonical, non-overlapping networks inside the VPC.
vpc_cidr = "192.168.0.0/16"
workload_subnet_cidr = "192.168.125.0/24"
tgw_subnet_a_cidr = "192.168.124.0/28"
tgw_subnet_b_cidr = "192.168.124.16/28"
# Only these destinations use the Site-to-Site VPNs. All other IPv4 traffic
# uses the Internet Gateway default route.
vpn_routed_prefixes = [
"172.16.0.0/16",
]
# Accounts using two anycast IPs have four tunnels. Entries 1-2 belong to
# anycast IP 1 and entries 3-4 belong to anycast IP 2.
tunnel_inside_cidrs = [
"169.254.240.44/30",
"169.254.240.48/30",
"169.254.240.52/30",
"169.254.240.56/30",
]
# PSKs are sensitive and persist in Terraform state; never commit terraform.tfvars.
vpn_preshared_keys = [
"REPLACE_WITH_UNIQUE_TUNNEL_01_PSK",
"REPLACE_WITH_UNIQUE_TUNNEL_02_PSK",
"REPLACE_WITH_UNIQUE_TUNNEL_03_PSK",
"REPLACE_WITH_UNIQUE_TUNNEL_04_PSK",
]
# List order is resource identity. Anycast IP 1 backs tunnels 01-02 and VPN
# connection 01; anycast IP 2 backs tunnels 03-04 and VPN connection 02.
cloudflare_anycast_ips = ["162.159.135.1", "172.64.135.1"]
aws_transit_gateway_asn = 65020
# Set to the CF Account ASN shown under Networking > Routes > WAN configuration.
cloudflare_asn = 13335
cloudflare_additional_advertised_prefixes = []
# Optional. Cloudflare tunnel names must be unique in the Cloudflare account.
# Leave empty to use <deployment_name>-tun-01, -02, and so on, or list two names
# per anycast IP in tunnel order, for example:
# cloudflare_tunnel_names = ["aws-tun-01", "aws-tun-02", "aws-tun-03", "aws-tun-04"]
cloudflare_tunnel_names = []
tests/reference.tftest.hcl
Create the tests/ directory first, then create tests/reference.tftest.hcl to run 24 tests: five positive cases and nineteen invalid-input cases, without contacting AWS or Cloudflare.
