AWS Builder Center
Day 41: Connecting Networks – VPC Peering, Transit Gateway, VPN, Direct Connect, and PrivateLink

Day 41: Connecting Networks – VPC Peering, Transit Gateway, VPN, Direct Connect, and PrivateLink

Day 41 of my 90-day AWS challenge: how to connect VPCs to each other, to on-premises networks, and to AWS services privately, and how to choose between peering, Transit Gateway, VPN, Direct Connect, and VPC endpoints.

Recap of Day 40

On Day 5 I built one VPC, and yesterday I learned why organizations use many accounts. Many accounts usually means many VPCs, and those networks often need to talk to each other, to an office or data center, and to AWS services without sending traffic over the public internet. Here are the tools for that.

The options at a glance

Option
Connects
In one line
VPC peering
One VPC to one other VPC
A simple, direct private link
Transit Gateway
Many VPCs and networks
A central hub, so each network connects once
Site-to-Site VPN
A VPC to an on-premises network
Encrypted tunnels over the internet
Direct Connect
A VPC to an on-premises network
A dedicated private line
VPC endpoints and PrivateLink
A VPC to AWS services or other services
Private access without an internet path

VPC peering

A peering connection links two VPCs so that instances can talk using private IP addresses. The VPCs can be in different accounts and different Regions.
Rules to remember:
  • The two VPCs' CIDR ranges must not overlap, for example  10.0.0.0/16  and  10.1.0.0/16 
  • Peering is not transitive: if A is peered with B and B with C, A cannot reach C through B
  • I must add routes in each VPC's route table and allow the traffic in security groups (Day 5)
  • Connecting many VPCs needs a link for every pair, which gets messy quickly

Transit Gateway

A Transit Gateway is a regional hub. Each VPC, VPN connection, or Direct Connect link attaches to the hub once, and the hub routes traffic between them.
  • It supports transitive routing, so attached networks can reach each other, controlled with route tables
  • It can connect to Transit Gateways in other Regions
  • It is billed per attachment per hour and per GB of data processed, so it costs more than peering for a small setup
Use peering for two or three VPCs, and a Transit Gateway when the number of networks grows.

Site-to-Site VPN

A VPN creates encrypted IPsec tunnels between a VPC and an on-premises network over the public internet.
  • Each connection has two tunnels for redundancy
  • It ends at a virtual private gateway or a Transit Gateway on the AWS side, and a customer gateway on the customer side
  • It is quick to set up, but its performance depends on the internet

AWS Direct Connect

Direct Connect is a dedicated network link from a data center to AWS.
  • It gives consistent bandwidth and latency
  • It takes time to arrange, because a physical connection is needed
  • It is not encrypted by default, so sensitive traffic can use MACsec or a VPN over the link

VPC endpoints and PrivateLink

By default, a private instance reaches S3 or other AWS services through a NAT gateway (Day 5), which costs money and sends traffic out to the internet path. VPC endpoints keep that traffic inside AWS.
Type
How it works
Services
Gateway endpoint
A route table entry that points to the service
S3 and DynamoDB, at no extra charge
Interface endpoint (PrivateLink)
A network interface with a private IP inside my subnet
Most other AWS services, and services shared by other accounts
  • Endpoint policies can restrict what can be done through an endpoint, such as which buckets are reachable
  • Interface endpoints are billed per hour per Availability Zone and per GB, so remember to delete practice ones
An S3 gateway endpoint from the CLI, with placeholder IDs:
aws ec2 create-vpc-endpoint \
--vpc-id vpc-0123456789abcdef0 \
--service-name com.amazonaws.<region>.s3 \
--route-table-ids rtb-0123456789abcdef0
This is also how Session Manager (Day 39) works in a private subnet: interface endpoints for the Systems Manager services provide the network path.

Planning and visibility

  • Plan CIDR ranges early across all accounts so they never overlap, because changing them later is painful
  • VPC Flow Logs record the traffic accepted and rejected on network interfaces, and can be sent to CloudWatch (Day 9) or S3, where Athena (Day 29) can query them
  • Keep security groups strict, because a network path does not mean the traffic should be allowed

Choosing a connection

Need
Use
Two VPCs, simple
VPC peering
Many VPCs and accounts
Transit Gateway
Office or data center, fast to set up and encrypted
Site-to-Site VPN
Office or data center, steady high bandwidth
Direct Connect
Private access to S3 or DynamoDB
Gateway endpoint
Private access to other services
Interface endpoint (PrivateLink)

How it connects to earlier days

  • Day 5: route tables, security groups, and NAT gateways are the building blocks
  • Day 40: multiple accounts mean multiple VPCs to connect
  • Day 39: Session Manager in private subnets needs endpoints
  • Day 26: NAT gateways, Transit Gateway, and interface endpoints all show up on the bill

What I practiced today

  1. Created two VPCs with non-overlapping CIDR ranges and a small instance in each
  1. Created a peering connection, accepted it, and added routes on both sides
  1. Allowed the traffic in the security groups and tested connectivity with private IPs
  1. Created an S3 gateway endpoint and checked the new route table entry
  1. Looked at the Transit Gateway and VPN creation screens without creating them, because they bill by the hour
  1. Deleted the peering connection, endpoint, instances, and VPCs afterwards

Key takeaways

  • Peering is simple but not transitive, and Transit Gateway is the hub for larger setups
  • Gateway endpoints for S3 and DynamoDB are free and remove NAT traffic for those services
  • Overlapping CIDR ranges make networks impossible to connect, so plan them from the start
  • Transit Gateway attachments, VPN connections, and interface endpoints bill by the hour, so clean up

Up next: Day 42

Week 6 recap: Beanstalk, ECS Express Mode, Amplify, Systems Manager, Organizations, and networking.
Learning AWS too? Follow along and share what you're building. #90DaysOfAWS
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article