Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 70 ยท Networking with VPC

Peering, Transit Gateway, VPN and Direct Connect

Connect VPCs and on-premises networks: VPC peering and why it is non-transitive, Transit Gateway hub-and-spoke, Site-to-Site VPN, Direct Connect and Route 53 Resolver.

One VPC is rarely the whole story. A company has a VPC per environment, separate accounts for teams, and an office or data center that still hosts something important. Sooner or later these networks must talk to each other privately. AWS offers several ways to connect them, and each fits a different size and shape of problem: VPC peering, Transit Gateway, Site-to-Site VPN and Direct Connect. A fifth piece, Route 53 Resolver, makes DNS names work across the links.

This lesson is mostly conceptual, because connecting networks needs real accounts and hardware. Every command below is read-only. The runnable parts are small stand-ins: a checker for overlapping address ranges, which is the first thing that breaks every connection plan, and a reachability model that shows why peering does not chain and a transit gateway does. They model the routing rules, not the actual network devices.

The first rule: no overlapping CIDR blocks

All of these options route by IP address. If two networks use the same range, a router cannot tell which one 10.0.5.9 belongs to. For that reason peering requires non-overlapping CIDR blocks, and Transit Gateway cannot route to overlapping attachments either. The cheapest time to avoid the problem is at the start: give every VPC and office a unique slice of a planned address space, as suggested in VPC Fundamentals. Renumbering a live network later is painful.

The checker below takes a plan and lists every conflicting pair, using the real ipaddress.overlaps method. It is a good exercise to run against your own plan before you build anything.

Python Overlapping CIDR checker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

VPC peering

A VPC peering connection is a one-to-one private link between two VPCs. The VPCs can be in different accounts and different Regions. Traffic travels on the AWS backbone, with no gateway, no appliance and no single bottleneck, and there is no hourly charge for the peering connection itself (data transfer between zones or Regions is billed).

To use it you must do three things: one side requests and the other accepts, both VPCs add routes that point the other's CIDR at the peering connection, and security groups must allow the traffic.

Terminal
# read-only: peer two VPCs and add the routes
aws ec2 create-vpc-peering-connection \
  --vpc-id vpc-0prod --peer-vpc-id vpc-0shared
aws ec2 accept-vpc-peering-connection \
  --vpc-peering-connection-id pcx-0123abcd
aws ec2 create-route --route-table-id rtb-0prod \
  --destination-cidr-block 10.2.0.0/20 \
  --vpc-peering-connection-id pcx-0123abcd

The defining limit is that peering is non-transitive. If A is peered with B, and B is peered with C, A cannot reach C through B. AWS does not forward traffic between two peering connections, and it also will not let A use B's internet gateway, NAT gateway or VPN. To connect A and C, you create a third peering. For n VPCs that all need to talk to each other you need n(n-1)/2 connections and as many routes to keep in sync: ten VPCs need 45.

Peering limits, topologies and jumbo frames

Collect the rules in one place before you design with peering:

  • No overlapping CIDR blocks between the two VPCs.
  • No transitive routing, and no edge-to-edge routing: a peer cannot use your NAT gateway, internet gateway, VPN or Direct Connect connection.
  • One peering connection between any pair of VPCs. Routes are per route table, so every subnet's route table that needs the path needs the route.
  • Cross-Region peering works, with traffic encrypted on the AWS backbone, but you cannot reference the other VPC's security groups there (within one Region you can).
  • Quotas apply per VPC (an adjustable default number of active peerings).
  • MTU. A peering connection inside one Region supports jumbo frames (MTU 9001 bytes). Across Regions it is limited to 1500. Larger frames mean fewer packets for bulk transfers, because each packet carries less header overhead.

The usual topologies are hub and spoke (a shared-services VPC peered with each application VPC, which is fine for a handful of spokes because the spokes still cannot reach each other) and a full mesh (every pair peered, which grows as n(n-1)/2 and becomes unmanageable). Peering is also handy after a merger or acquisition, to join two companies' networks quickly across accounts before a proper redesign.

Python Peering checks and the cost of jumbo frames

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Transit Gateway: hub and spoke

A Transit Gateway (TGW) is a regional router that you place in the middle. Every VPC, VPN connection and Direct Connect gateway attaches to it once. The gateway holds route tables, and each attachment is associated with one table and can propagate its CIDR into others. Ten VPCs need ten attachments, not 45 peerings, and adding the eleventh is one new attachment.

flowchart TB
    A["VPC A<br/>10.0.0.0/16"] --- TGW(("Transit<br/>Gateway"))
    B["VPC B<br/>10.1.0.0/16"] --- TGW
    C["VPC C<br/>10.2.0.0/16"] --- TGW
    VPN["Office VPN<br/>192.168.0.0/16"] --- TGW

Each network attaches once to the hub; routing between them is decided by the gateway's route tables.

Because you control the route tables, you can build segmented networks: for instance production and development attached to the same gateway but associated with separate tables so they cannot reach each other, while both can reach a shared services VPC. Transit Gateways can also peer with one another across Regions.

The trade-off is cost and simplicity: you pay an hourly fee per attachment plus a per-gigabyte data processing fee, which peering does not charge. For two or three VPCs peering is cheaper and simpler. As the number of networks grows, the TGW pays for itself in operations.

The model below tests reachability with and without a transit gateway. A peering graph allows only direct links; the gateway lets any attached networks reach each other. It is a graph model of the rule, not of the devices.

Python Peering chain versus transit gateway

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Site-to-Site VPN

A Site-to-Site VPN connects your VPC to an on-premises network over the public internet with encrypted IPsec tunnels. You set up three pieces: a customer gateway (a record describing your router or firewall), a virtual private gateway or a transit gateway on the AWS side, and the VPN connection itself. Each connection provides two tunnels in different Availability Zones for redundancy, and you should configure both on your device. Each tunnel carries up to about 1.25 Gbps, and you pay an hourly fee per connection plus data transfer out.

A VPN is quick to create, in minutes, and needs no special circuit, so it is the usual first link to a data center and a good backup for a faster one. Routing can be static or dynamic with BGP, and BGP is the better choice because routes adjust automatically when a tunnel fails. Because traffic crosses the internet, throughput and latency can vary.

Direct Connect

AWS Direct Connect is a dedicated physical network connection from your location, or a partner's, to AWS at a Direct Connect location. You can order a dedicated port (1, 10 or 100 Gbps) or a smaller hosted connection from a partner. It does not use the public internet, so latency and bandwidth are consistent and data transfer out is cheaper than over the internet.

You run one or more virtual interfaces (VIFs) over the connection: a private VIF to reach VPCs, a public VIF to reach public AWS services such as S3 without crossing the internet, and a transit VIF to reach a Transit Gateway through a Direct Connect gateway. Two caveats matter. Provisioning takes weeks, because it involves physical work, and Direct Connect traffic is not encrypted by default. If you need encryption, use MACsec on supported ports or run a VPN over the connection. For resilience, order two connections at different locations, or use a VPN as a backup.

Option Setup time Bandwidth Encrypted by default Good for
VPC peering Minutes Up to the instance limits Traffic stays on AWS network A few VPCs, simple links
Transit Gateway Minutes High, per-attachment limits Traffic stays on AWS network Many VPCs, segmentation, hybrid hub
Site-to-Site VPN Minutes About 1.25 Gbps per tunnel Yes, IPsec Quick or backup hybrid link
Direct Connect Weeks 1 to 100 Gbps No (MACsec or VPN optional) Steady, high-volume hybrid traffic

Route 53 Resolver: names across networks

Connecting networks does not make names resolve. Inside a VPC, the Amazon-provided DNS resolver (reachable at the VPC's base address plus two) answers for AWS resources and private hosted zones. Route 53 Resolver endpoints extend it across links:

  • An inbound endpoint gives your on-premises DNS servers an address in the VPC to which they can forward queries for AWS names.
  • An outbound endpoint with forwarding rules lets resources in the VPC resolve names that belong to your on-premises domain, such as corp.example.com, by forwarding them to your own DNS servers.

Rules can be shared across accounts, so one forwarding setup serves a whole organization. The service is explained alongside hosted zones in Route 53 and DNS.

Choosing

Start with the number and shape of networks. Two or three VPCs that rarely change: peering. Many VPCs, several accounts, or a hybrid hub: Transit Gateway. Connect on-premises quickly or as a backup: VPN. Large steady volumes or strict latency needs: Direct Connect, with a VPN in reserve. Whichever you choose, plan non-overlapping CIDR ranges first, and revisit NACLs, Flow Logs and Network Security to remember that routes alone do not grant access: security groups and NACLs still decide.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. VPC A is peered with VPC B, and VPC B is peered with VPC C. Can A reach C through B?

    AWS does not forward traffic between two peering connections. You would create a third peering or use a Transit Gateway.
  2. 2. Why can two VPCs not be peered if their CIDR blocks overlap?

    A router needs each destination address to belong to exactly one network. With overlapping ranges it cannot decide where to send the traffic.
  3. 3. You need a hybrid link to a data center by next week. Which option can be created in minutes?

    A VPN rides on the internet and needs no physical circuit. Direct Connect involves physical provisioning that can take weeks.
  4. 4. Which statement about Direct Connect is correct?

    Direct Connect is a private circuit, not an encrypted one. Encryption is added with MACsec on supported ports or a VPN.
  5. 5. What does a Route 53 Resolver outbound endpoint with a forwarding rule do?

    Outbound endpoints forward queries from the VPC to external DNS servers according to rules. Inbound endpoints do the reverse.

Summary

  • Every connection option routes by IP, so plan unique, non-overlapping CIDR blocks first.
  • VPC peering is a simple one-to-one link, non-transitive, and needs routes on both sides.
  • A Transit Gateway is a regional hub with route tables; each network attaches once and you can segment traffic.
  • Site-to-Site VPN is quick and encrypted over the internet; Direct Connect is a dedicated, unencrypted-by-default circuit that takes weeks.
  • Route 53 Resolver endpoints and forwarding rules make DNS names work across connected networks.

Next: Route 53 and DNS.