- Amazon AWS Transit Gateway·续6
- Mock providers let plan assertions run without credentials or live API calls.
- terraform test also loads terraform.tfvars from the module directory. Set every
- input here so a deployment's terraform.tfvars cannot change the test baseline.
- Supply deterministic AZs so the two-AZ safety precondition can be tested offline.
- AWS normally computes public IPv4 addresses during apply. Fixed plan-time values make the
- default AWS VPN connection's two Cloudflare WAN-to-AWS tunnel mappings testable offline.
- Make route targets known during plan so assertions can verify exact wiring.
- This default positive case protects one anycast IP, one VPN, and two tunnels.
- Customers with two distinct anycast IPs use two VPNs and four tunnels.
- The remaining runs verify a private CF Account ASN and confirm that unsafe or ambiguous inputs fail at plan time.
Amazon AWS Transit Gateway·续6
```hcl
Mock providers let plan assertions run without credentials or live API calls.
mock_provider "aws" {} mock_provider "cloudflare" {}
terraform test also loads terraform.tfvars from the module directory. Set every
input here so a deployment's terraform.tfvars cannot change the test baseline.
variables { expected_account_id = "123456789012" cloudflare_account_id = "0123456789abcdef0123456789abcdef" aws_region = "us-east-2" deployment_name = "cloudflare-wan-reference" 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" # Exercise multiple selective routes instead of relying on the empty default. vpn_routed_prefixes = ["172.16.0.0/16", "172.17.0.0/16"] cloudflare_anycast_ips = ["162.159.135.1"] tunnel_inside_cidrs = [ "169.254.240.44/30", "169.254.240.48/30", ] vpn_preshared_keys = [ "A0123456789abcdef0123456789abcdef0123456789abcdef", "B0123456789abcdef0123456789abcdef0123456789abcdef", ] aws_transit_gateway_asn = 65020 cloudflare_asn = 13335 cloudflare_additional_advertised_prefixes = [] cloudflare_tunnel_names = [] }
Supply deterministic AZs so the two-AZ safety precondition can be tested offline.
override_data { target = data.aws_availability_zones.available values = { names = ["us-east-2a", "us-east-2b"] } }
AWS normally computes public IPv4 addresses during apply. Fixed plan-time values make the
default AWS VPN connection's two Cloudflare WAN-to-AWS tunnel mappings testable offline.
override_resource { target = aws_vpn_connection.cloudflare["01"] override_during = plan values = { tunnel1_address = "198.51.100.1" tunnel2_address = "198.51.100.2" } }
Make route targets known during plan so assertions can verify exact wiring.
override_resource { target = aws_route_table.workload override_during = plan values = { id = "rtb-0123456789abcdef0" } }
override_resource { target = aws_internet_gateway.workload override_during = plan values = { id = "igw-0123456789abcdef0" } }
override_resource { target = aws_ec2_transit_gateway.cloudflare override_during = plan values = { id = "tgw-0123456789abcdef0" } }
This default positive case protects one anycast IP, one VPN, and two tunnels.
run "valid_reference_configuration" { command = plan
assert { condition = ( length(var.cloudflare_anycast_ips) == 1 && length(var.tunnel_inside_cidrs) == 2 && length(nonsensitive(var.vpn_preshared_keys)) == 2 && length(aws_customer_gateway.cloudflare) == 1 && length(aws_vpn_connection.cloudflare) == 1 && length(cloudflare_magic_wan_ipsec_tunnel.aws) == 2 ) error_message = "The default configuration must create one customer gateway, one VPN, and two tunnels from two CIDRs and two PSKs." }
assert { condition = aws_ec2_transit_gateway.cloudflare.amazon_side_asn == var.aws_transit_gateway_asn error_message = "The Transit Gateway ASN does not match aws_transit_gateway_asn." }
assert { condition = cloudflare_magic_wan_ipsec_tunnel.aws["01"].name == "cloudflare-wan-reference-tun-01" && cloudflare_magic_wan_ipsec_tunnel.aws["02"].name == "cloudflare-wan-reference-tun-02" error_message = "Cloudflare tunnel names must be prefixed with deployment_name." }
assert { condition = cloudflare_magic_wan_ipsec_tunnel.aws["01"].interface_address == "${cidrhost(var.tunnel_inside_cidrs[0], 2)}/30" && cloudflare_magic_wan_ipsec_tunnel.aws["02"].interface_address == "${cidrhost(var.tunnel_inside_cidrs[1], 2)}/30" error_message = "A Cloudflare interface address does not match its tunnel CIDR." }
assert { condition = ( cloudflare_magic_wan_ipsec_tunnel.aws["01"].customer_endpoint == "198.51.100.1" && cloudflare_magic_wan_ipsec_tunnel.aws["02"].customer_endpoint == "198.51.100.2" ) error_message = "A customer_endpoint value is mapped to the wrong AWS VPN tunnel." }
assert { condition = ( cloudflare_magic_wan_ipsec_tunnel.aws["01"].cloudflare_endpoint == var.cloudflare_anycast_ips[0] && cloudflare_magic_wan_ipsec_tunnel.aws["02"].cloudflare_endpoint == var.cloudflare_anycast_ips[0] ) error_message = "A Cloudflare anycast IP is mapped to the wrong tunnel." }
assert { condition = cloudflare_magic_wan_ipsec_tunnel.aws["01"].replay_protection == true && cloudflare_magic_wan_ipsec_tunnel.aws["01"].automatic_return_routing == false && cloudflare_magic_wan_ipsec_tunnel.aws["01"].health_check.type == "request" && cloudflare_magic_wan_ipsec_tunnel.aws["01"].health_check.direction == "bidirectional" error_message = "The Cloudflare tunnel liveness or routing settings changed unexpectedly." }
assert { condition = alltrue([ for tunnel in cloudflare_magic_wan_ipsec_tunnel.aws : tunnel.health_check.enabled == true && tunnel.health_check.rate == "mid" && tunnel.health_check.type == "request" && tunnel.health_check.direction == "bidirectional" ]) error_message = "Every Cloudflare tunnel must use enabled, medium-rate, request-type, bidirectional health checks." }
assert { condition = cloudflare_magic_wan_ipsec_tunnel.aws["01"].bgp.customer_asn == var.aws_transit_gateway_asn && toset(cloudflare_magic_wan_ipsec_tunnel.aws["01"].bgp.extra_prefixes) == toset(var.cloudflare_additional_advertised_prefixes) error_message = "The Cloudflare BGP settings do not match the configured inputs." }
assert { condition = aws_ec2_transit_gateway.cloudflare.default_route_table_association == "disable" && aws_ec2_transit_gateway.cloudflare.default_route_table_propagation == "disable" && aws_ec2_transit_gateway.cloudflare.vpn_ecmp_support == "enable" error_message = "The Transit Gateway routing controls changed unexpectedly." }
assert { condition = aws_subnet.workload.map_public_ip_on_launch == true && aws_route.internet.destination_cidr_block == "0.0.0.0/0" && aws_route.internet.gateway_id == "igw-0123456789abcdef0" && aws_route.internet.route_table_id == "rtb-0123456789abcdef0" error_message = "The public workload subnet must use the Internet Gateway default route." }
assert { condition = toset([for route in aws_route.cloudflare : route.destination_cidr_block]) == toset(var.vpn_routed_prefixes) && alltrue([for route in aws_route.cloudflare : route.transit_gateway_id == "tgw-0123456789abcdef0" && route.route_table_id == "rtb-0123456789abcdef0"]) error_message = "The Transit Gateway routes do not match vpn_routed_prefixes." }
assert { condition = aws_customer_gateway.cloudflare["01"].ip_address == var.cloudflare_anycast_ips[0] && tostring(aws_customer_gateway.cloudflare["01"].bgp_asn) == tostring(var.cloudflare_asn) error_message = "The AWS customer gateway settings do not match the Cloudflare inputs." }
assert { condition = aws_vpn_connection.cloudflare["01"].tunnel1_inside_cidr == var.tunnel_inside_cidrs[0] && aws_vpn_connection.cloudflare["01"].tunnel2_inside_cidr == var.tunnel_inside_cidrs[1] error_message = "The default VPN has the wrong inside CIDRs." }
assert { condition = aws_vpn_connection.cloudflare["01"].static_routes_only == false && aws_vpn_connection.cloudflare["01"].tunnel1_startup_action == "start" && aws_vpn_connection.cloudflare["01"].tunnel1_dpd_timeout_action == "restart" && aws_vpn_connection.cloudflare["01"].tunnel1_replay_window_size == 2048 error_message = "The dynamic-routing or tunnel liveness settings changed unexpectedly." }
assert { condition = aws_vpn_connection.cloudflare["01"].tunnel2_phase1_encryption_algorithms == toset(["AES256"]) && aws_vpn_connection.cloudflare["01"].tunnel2_phase2_integrity_algorithms == toset(["SHA2-256"]) && aws_vpn_connection.cloudflare["01"].tunnel2_phase1_dh_group_numbers == toset([20]) && aws_vpn_connection.cloudflare["01"].tunnel2_ike_versions == toset(["ikev2"]) error_message = "The tunnel cryptographic settings changed unexpectedly." }
assert { condition = aws_vpn_connection.cloudflare["01"].tunnel1_phase1_lifetime_seconds == 28800 && aws_vpn_connection.cloudflare["01"].tunnel1_phase2_lifetime_seconds == 3600 error_message = "The tunnel lifetimes changed unexpectedly." } }
Customers with two distinct anycast IPs use two VPNs and four tunnels.
run "valid_two_anycast_ip_configuration" { command = plan
override_resource { target = aws_vpn_connection.cloudflare["02"] override_during = plan values = { tunnel1_address = "198.51.100.3" tunnel2_address = "198.51.100.4" } }
variables { cloudflare_anycast_ips = ["162.159.135.1", "162.159.135.2"] tunnel_inside_cidrs = [ "169.254.240.44/30", "169.254.240.48/30", "169.254.240.52/30", "169.254.240.56/30", ] vpn_preshared_keys = [ "A0123456789abcdef0123456789abcdef0123456789abcdef", "B0123456789abcdef0123456789abcdef0123456789abcdef", "C0123456789abcdef0123456789abcdef0123456789abcdef", "D0123456789abcdef0123456789abcdef0123456789abcdef", ] }
assert { condition = length(aws_customer_gateway.cloudflare) == 2 && length(aws_vpn_connection.cloudflare) == 2 && length(cloudflare_magic_wan_ipsec_tunnel.aws) == 4 error_message = "The two-anycast-IP configuration must create two customer gateways, two AWS Site-to-Site VPN connections, and four Cloudflare WAN tunnels." }
assert { condition = aws_customer_gateway.cloudflare["01"].ip_address == var.cloudflare_anycast_ips[0] && aws_customer_gateway.cloudflare["02"].ip_address == var.cloudflare_anycast_ips[1] error_message = "A customer gateway is mapped to the wrong Cloudflare anycast IP." }
assert { condition = aws_customer_gateway.cloudflare["01"].device_name == "${var.deployment_name}-cgw-01" && aws_customer_gateway.cloudflare["02"].device_name == "${var.deployment_name}-cgw-02" error_message = "Each customer gateway must use a unique device name derived from deployment_name." }
assert { condition = cloudflare_magic_wan_ipsec_tunnel.aws["03"].cloudflare_endpoint == var.cloudflare_anycast_ips[1] && cloudflare_magic_wan_ipsec_tunnel.aws["04"].cloudflare_endpoint == var.cloudflare_anycast_ips[1] error_message = "Anycast IP 2 must map to Cloudflare tunnels 03 and 04." }
assert { condition = cloudflare_magic_wan_ipsec_tunnel.aws["03"].customer_endpoint == "198.51.100.3" && cloudflare_magic_wan_ipsec_tunnel.aws["04"].customer_endpoint == "198.51.100.4" error_message = "Cloudflare WAN tunnels 03 and 04 must map to AWS VPN 02 tunnels 1 and 2." }
assert { condition = [for key in sort(keys(cloudflare_magic_wan_ipsec_tunnel.aws)) : cloudflare_magic_wan_ipsec_tunnel.aws[key].name] == ["cloudflare-wan-reference-tun-01", "cloudflare-wan-reference-tun-02", "cloudflare-wan-reference-tun-03", "cloudflare-wan-reference-tun-04"] error_message = "All four Cloudflare tunnel names must be unique and prefixed with deployment_name." }
assert { condition = nonsensitive(cloudflare_magic_wan_ipsec_tunnel.aws["03"].psk) == nonsensitive(var.vpn_preshared_keys[2]) && nonsensitive(cloudflare_magic_wan_ipsec_tunnel.aws["04"].psk) == nonsensitive(var.vpn_preshared_keys[3]) error_message = "Cloudflare tunnels 03 and 04 must use PSKs 3 and 4." }
assert { condition = aws_vpn_connection.cloudflare["02"].tunnel1_inside_cidr == var.tunnel_inside_cidrs[2] && aws_vpn_connection.cloudflare["02"].tunnel2_inside_cidr == var.tunnel_inside_cidrs[3] error_message = "VPN 02 must consume tunnel CIDRs at indexes 2 and 3." } }
The remaining runs verify a private CF Account ASN and confirm that unsafe or ambiguous inputs fail at plan time.
run "reject_non_network_tunnel_cidr" { command = plan
variables { tunnel_inside_cidrs = [ "169.254.240.45/30", "169.254.240.48/30", ] }
expect_failures = [var.tunnel_inside_cidrs] }
run "reject_unsupported_vpc_prefix" { command = plan
variables { vpc_cidr = "192.168.0.0/15" }
expect_failures = [var.vpc_cidr] }
run "reject_unsupported_subnet_prefix" { command = plan
variables { workload_subnet_cidr = "192.168.125.0/29" }
expect_failures = [var.workload_subnet_cidr] }
run "reject_overlapping_subnets" { command = plan
variables { tgw_subnet_a_cidr = "192.168.125.0/24" }
expect_failures = [aws_vpc.workload] }
run "reject_duplicate_cloudflare_anycast_ips" { command = plan
variables { cloudflare_anycast_ips = ["162.159.135.1", "162.159.135.1"] tunnel_inside_cidrs = [ "169.254.240.44/30", "169.254.240.48/30", "169.254.240.52/30", "169.254.240.56/30", ] vpn_preshared_keys = [ "A0123456789abcdef0123456789abcdef0123456789abcdef", "B0123456789abcdef0123456789abcdef0123456789abcdef", "C0123456789abcdef0123456789abcdef0123456789abcdef", "D0123456789abcdef0123456789abcdef0123456789abcdef", ] }
expect_failures = [var.cloudflare_anycast_ips] }
run "accept_private_cloudflare_asn" { command = plan
variables { cloudflare_asn = 65000 }
assert { condition = tostring(aws_customer_gateway.cloudflare["01"].bgp_asn) == "65000" error_message = "The AWS customer gateway must use a configured private CF Account ASN." } }
run "reject_unsupported_cloudflare_asn" { command = plan
variables { cloudflare_asn = 64511 }
expect_failures = [var.cloudflare_asn] }
run "reject_cloudflare_asn_matching_tgw_asn" { command = plan
variables { cloudflare_asn = 65020 }
expect_failures = [var.cloudflare_asn] }
run "accept_aws_psk_characters" { command = plan
variables { vpn_preshared_keys = [ "Key.01_a", "_Key.02_b", ] }
assert { condition = nonsensitive(aws_vpn_connection.cloudflare["01"].tunnel1_preshared_key) == "Key.01_a" error_message = "An 8-character PSK with a period and underscore must be accepted." } }
run "reject_psk_with_unsupported_character" { command = plan
variables { vpn_preshared_keys = [ "Invalid-psk-1", "B0123456789abcdef0123456789abcdef0123456789abcdef", ] }
expect_failures = [var.vpn_preshared_keys] }
run "reject_short_psk" { command = plan
variables { vpn_preshared_keys = [ "Short_7", "B0123456789abcdef0123456789abcdef0123456789abcdef", ] }
expect_failures = [var.vpn_preshared_keys] }
run "reject_psk_with_leading_zero" { command = plan
variables { vpn_preshared_keys = [ "0LeadingZeroKey", "B0123456789abcdef0123456789abcdef0123456789abcdef", ] }
expect_failures = [var.vpn_preshared_keys] }
run "reject_default_route_as_vpn_prefix" { command = plan
variables { vpn_routed_prefixes = ["0.0.0.0/0"] }
expect_failures = [var.vpn_routed_prefixes] }
run "reject_mismatched_tunnel_input_cardinality" { command = plan
variables { tunnel_inside_cidrs = [ "169.254.240.44/30", "169.254.240.48/30", "169.254.240.52/30", ] }
expect_failures = [var.tunnel_inside_cidrs] }
run "reject_mismatched_psk_cardinality" { command = plan
variables { vpn_preshared_keys = [ "A0123456789abcdef0123456789abcdef0123456789abcdef", ] }
expect_failures = [var.vpn_preshared_keys] }
run "reject_duplicate_psks" { command = plan
variables { vpn_preshared_keys = [ "A0123456789abcdef0123456789abcdef0123456789abcdef", "A0123456789abcdef0123456789abcdef0123456789abcdef", ] }
expect_failures = [var.vpn_preshared_keys] }
run "reject_zero_cloudflare_anycast_ips" { command = plan
variables { cloudflare_anycast_ips = [] tunnel_inside_cidrs = [] vpn_preshared_keys = [] }
expect_failures = [var.cloudflare_anycast_ips] }
run "reject_more_than_two_cloudflare_anycast_ips" { command = plan
variables { cloudflare_anycast_ips = ["162.159.135.1", "162.159.135.2", "162.159.135.3"] tunnel_inside_cidrs = [ "169.254.240.44/30", "169.254.240.48/30", "169.254.240.52/30", "169.254.240.56/30", "169.254.240.60/30", "169.254.240.64/30", ] vpn_preshared_keys = [ "A0123456789abcdef0123456789abcdef0123456789abcdef", "B0123456789abcdef0123456789abcdef0123456789abcdef", "C0123456789abcdef0123456789abcdef0123456789abcdef", "D0123456789abcdef0123456789abcdef0123456789abcdef", "E0123456789abcdef0123456789abcdef0123456789abcdef", "F0123456789abcdef0123456789abcdef0123456789abcdef", ] }
