Sub topic 14
Connecting to On-Premises with Site-to-Site VPN and Direct Connect
This chapter assumes you know how route tables and CIDR planning work and what a gateway attachment is. It also assumes you have read the chapters on peering and Transit Gateway. The commands reuse VPC_ID and PRIVATE_RT_ID from earlier chapters.
Two ways to reach a data center
A VPC has no path to your office or data center until you build one. AWS offers two managed options:
- AWS Site-to-Site VPN builds encrypted IPsec tunnels over the public internet.
- AWS Direct Connect gives you a dedicated private circuit from your network to AWS, with no internet in the path.
Both exchange routes with your network and both end in a gateway on the AWS side. They solve different problems, and many production designs use both.
Site-to-Site VPN
Components
A VPN connection has three parts:
- A gateway on the AWS side. This is either a virtual private gateway (VGW), attached to one VPC, or a Transit Gateway attachment, which serves many VPCs.
- A customer gateway. This is an AWS object that records the public IP address and BGP ASN of your on-premises router or firewall. The device itself is yours to configure.
- The VPN connection. It links the first two and always creates two tunnels, with endpoints in different Availability Zones.
Each tunnel carries up to about 1.25 Gbps. The second tunnel is for redundancy. AWS performs maintenance on tunnel endpoints, so a connection with only one tunnel configured will drop traffic at some point.
Static versus dynamic routing
With static routing, you list your on-premises prefixes on the connection by hand. With dynamic routing, your device and AWS exchange routes over BGP. Prefer BGP. It withdraws routes from a dead tunnel automatically, and it lets you add on-premises networks without touching AWS.
Building one
VGW_ID=$(aws ec2 create-vpn-gateway --type ipsec.1 --query VpnGateway.VpnGatewayId --output text)
aws ec2 attach-vpn-gateway --vpn-gateway-id "$VGW_ID" --vpc-id "$VPC_ID"
CGW_ID=$(aws ec2 create-customer-gateway --type ipsec.1 --bgp-asn 65000 --ip-address 203.0.113.12 --query CustomerGateway.CustomerGatewayId --output text)
VPN_ID=$(aws ec2 create-vpn-connection --type ipsec.1 --customer-gateway-id "$CGW_ID" --vpn-gateway-id "$VGW_ID" --options StaticRoutesOnly=false --query VpnConnection.VpnConnectionId --output text)
aws ec2 wait vpn-connection-available --vpn-connection-ids "$VPN_ID"
aws ec2 enable-vgw-route-propagation --route-table-id "$PRIVATE_RT_ID" --gateway-id "$VGW_ID"The address 203.0.113.12 is a documentation address. Replace it with the public IP of your device.
The last command turns on route propagation. Routes learned from your network then appear in the route table automatically, so you do not add them with create-route. You still need the device-side configuration. Download it from the console, or read the CustomerGatewayConfiguration field from describe-vpn-connections. It contains the tunnel IPs, pre-shared keys, and IKE and IPsec parameters.
Direct Connect
How it is built
Direct Connect links your network to AWS at a Direct Connect location, which is a colocation facility. There are two ways to get a connection:
- Dedicated connection: a physical port you own, at 1, 10, or 100 Gbps.
- Hosted connection: a slice of a partner's capacity, from 50 Mbps upward.
A connection alone carries no traffic. You create virtual interfaces (VIFs) on it. Each VIF is a VLAN with its own BGP session:
| VIF type | Reaches | Typical use |
|---|---|---|
| Private | One VPC through a VGW, or many VPCs through a Direct Connect gateway | Reaching private IPs in VPCs |
| Transit | A Transit Gateway through a Direct Connect gateway | Many VPCs and regions through one hub |
| Public | AWS public service endpoints, such as S3 | Reaching AWS public IPs without the internet |
A Direct Connect gateway is a global object. It lets one private VIF reach VPCs in several Regions, as long as their CIDR blocks do not overlap.
Security and lead time
Direct Connect traffic is private but not encrypted by default. If you need encryption, you have two choices. You can enable MACsec on supported dedicated ports, or you can run an IPsec VPN over a public or transit VIF. Provisioning takes days to weeks because it depends on cross-connects and carrier work. A VPN is ready in minutes.
How the pieces fit
Comparing the options
| Property | Site-to-Site VPN | Direct Connect |
|---|---|---|
| Setup time | Minutes | Days to weeks |
| Path | Public internet | Private circuit |
| Bandwidth | About 1.25 Gbps per tunnel | 50 Mbps to 100 Gbps |
| Latency and jitter | Variable | Consistent |
| Encryption | IPsec always | Optional (MACsec or VPN over it) |
| Cost model | Hourly per connection plus data transfer out | Port hours plus lower data transfer out |
Route selection and failover
When several routes match a destination, AWS picks the longest prefix first. If prefix lengths tie, it prefers routes in this order:
- Local VPC routes.
- Static routes in the table.
- Propagated Direct Connect BGP routes.
- Propagated VPN static routes.
- Propagated VPN BGP routes.
Direct Connect therefore wins over a VPN that advertises the same prefix. This is the basis of the common pattern of Direct Connect as the primary path and a VPN as the backup. If the circuit fails, BGP withdraws its routes and traffic shifts to the VPN.
Common errors and gotchas
- Overlapping CIDR blocks. If your VPC and on-premises ranges overlap, routing cannot tell them apart. Fix this in the address plan, because changing it later means renumbering.
- Only one tunnel configured. The connection shows "up" while one tunnel is down, and you lose redundancy without noticing. Configure both tunnels and alarm on the
TunnelStateCloudWatch metric. - Tunnels that idle out. A tunnel can show as down until traffic or DPD (dead peer detection) restarts negotiation. Your on-premises side should initiate traffic or use a keepalive.
- Firewalls still apply. A working tunnel does not open anything. Security groups must allow on-premises CIDRs, and network ACLs must allow traffic in both directions.
- No transitive routing through a VGW. A VGW cannot forward traffic between your VPN and a peered VPC, nor between a VPN and Direct Connect. Use Transit Gateway when you need that.
- Single Direct Connect circuit. One connection is one location and one device. For high availability, use two connections at separate locations.
- Name resolution. On-premises hosts cannot resolve private VPC DNS names by default. Add Route 53 Resolver inbound endpoints, and outbound endpoints for the reverse direction.
- Throughput ceilings. One IPsec flow is limited by a single tunnel. With a VGW you cannot aggregate tunnels. With Transit Gateway, ECMP across several VPN connections raises the total.
Choosing between them
Pick Site-to-Site VPN when you need connectivity quickly, your traffic is modest, or you want a low-cost backup. Pick Direct Connect when you move large volumes, need predictable latency, or have compliance rules against the public internet. Use both when the link is business critical. Attach either to a Transit Gateway once you connect more than a couple of VPCs.
Exercise
Your company has a Direct Connect private VIF as its primary link and a VPN as backup. Both advertise 10.20.0.0/16 for the on-premises network. During a drill you shut down the Direct Connect circuit. Describe what happens to traffic bound for 10.20.0.0/16, and name one configuration mistake that would stop the circuit from being preferred during normal operation.
Show answer
BGP withdraws the 10.20.0.0/16 route learned over Direct Connect, so only the propagated VPN route remains, and traffic fails over to the VPN. When the circuit returns, the Direct Connect route is preferred again because both prefixes have the same length. The mistake would be advertising a more specific prefix over the VPN, such as 10.20.1.0/24. Longest-prefix match would then send that traffic over the VPN even while Direct Connect is healthy.