VPC: Connecting Networks
How to connect VPCs with peering and Transit Gateway, reach AWS services privately through endpoints, link your office with VPN or Direct Connect, and control network costs, with a hands-on for the two most useful connections.
- Key idea
- Definition
- When to use
- Caution
In this article
In part thirteen we built an isolated network. But real applications rarely stay alone: you have a second VPC for another team, servers that need S3 and an office that needs to reach the database.
Here's this part's map, to come back to whenever you're unsure:
| You want | Use |
|---|---|
| Two VPCs talking to each other | VPC peering |
| A private subnet reaching S3 or DynamoDB without NAT | Gateway endpoint |
| Reaching other AWS services or a partner's service privately | Interface endpoint |
| Your office reaching AWS quickly over the internet | Site-to-Site VPN |
| Employees' devices reaching the network | Client VPN |
| A steady, large line between your office and AWS | Direct Connect |
| Many networks and offices | Transit Gateway |
VPC peering
Peering connects two VPCs directly, as if they were one network. It works even between different accounts or different Regions. Its rules:
- Ranges can't overlap. This is what we warned you about in the previous part.
- It isn't transitive. If A connects to B and B connects to C, A can't reach C.
- You need a route on both sides. You add one in each network's route table.
There's no hourly charge; you pay only for data transfer.
VPC endpoints: AWS services without the internet
By default, your private server's request to S3 goes out through NAT, and you pay for every gigabyte, even though the destination is already inside AWS. Endpoints give you a private, direct path:
| Gateway endpoint | Interface endpoint | |
|---|---|---|
| Services | S3 and DynamoDB only | Most AWS services, and partners' services |
| How it works | A route in the route table | A network interface with a private address in your subnet |
| Cost | Free | Per hour per zone, and per gigabyte |
| From your office | Not reachable | Reachable through VPN or Direct Connect |
An interface endpoint is built on PrivateLink, which also lets you offer your own service to other networks through an NLB, with no peering and even if their ranges overlap.
Connecting your office: Site-to-Site VPN
Site-to-Site VPN connects your office network to the VPC over the internet through an encrypted tunnel, and you can set it up quickly. It has three pieces:
- Customer gateway: represents your office device and its public address.
- Virtual private gateway: the VPN gateway on AWS's side, attached to the VPC.
- VPN connection: the connection itself, which comes with two tunnels for availability.
Then turn on route propagation in the route table so the VPC learns the office's routes. Each tunnel gives you up to 1.25 Gbps, or up to 5 with Transit Gateway.
- VPN CloudHub: connects several offices to each other through the same gateway.
- Client VPN: for employees' devices rather than offices.
If you want a VPN fully under your control on your own server, see the WireGuard article.
Direct Connect: a private line
Direct Connect is a real private line between your site and AWS through a Direct Connect location, so it doesn't cross the public internet. Its speed is steady, and outgoing data transfer is cheaper.
- Dedicated: a port just for you, from 1 to 400 Gbps.
- Hosted: through a partner, with speeds starting far lower.
- Direct Connect Gateway: connects you to VPCs in several Regions.
Watch out for three things: setup takes weeks and sometimes months, the line isn't encrypted automatically, so use MACsec or a VPN on top, and one line is a single point of failure, so for your most important systems have two lines in two locations, or a backup VPN.
| Site-to-Site VPN | Direct Connect | |
|---|---|---|
| Path | The internet, encrypted | A private line |
| Setup | Minutes or hours | Weeks or months |
| Speed | 1.25 Gbps per tunnel, or 5 with Transit Gateway | 1 to 400 Gbps |
| Performance | Varies with the internet | Steady |
| Cost | Low | Higher at first, cheaper for large transfers |
Transit Gateway: one hub
As networks multiply, peering becomes tiring: since it isn't transitive, you need a link for every pair:
Transit Gateway is a central hub you connect networks to, and each one reaches the others through it:
- It connects everything: VPCs, VPN connections, Direct Connect and Transit Gateways in other Regions.
- Its own route tables: you decide who talks to whom, for example separating production from development.
- Across accounts: you share it with other accounts through the RAM service.
- Higher VPN speed: it spreads traffic across several tunnels.
You pay by the hour for each attachment, and for data. For two or three networks, peering is simpler and cheaper.
Network costs
Data transfer is the item that most often surprises people. These are approximate rates for US Regions, so check yours:
| Traffic | Cost per gigabyte |
|---|---|
| Into AWS | Free |
| Within one Availability Zone, by private address | Free |
| Between Availability Zones | About $0.01 in each direction |
| Between Regions | About $0.02 |
| Out to the internet | About $0.09, falling with volume |
| Through a NAT Gateway | About $0.045 extra |
| S3 through a gateway endpoint | Free |
How to save:
- Use private addresses between servers, not public ones.
- Keep services that talk a lot in the same Availability Zone, if your availability design allows it.
- Turn on a gateway endpoint for S3.
- Serve content through CloudFront, since transfer from your origin to it is free.
Layers of protection
| Layer | Service | Protects against |
|---|---|---|
| Subnet | NACL | Addresses you want to block |
| Server | Security group | Any port and source you didn't allow |
| Web application | WAF | Web attacks such as SQL injection |
| Large attacks | Shield | DDoS |
| The whole VPC | Network Firewall | Inspecting traffic and domains at the boundary |
As for IPv6: you can add it to a VPC, but all its addresses are public. If you want servers to go out with nobody coming in, use an egress-only internet gateway, which is like NAT for IPv6.
Hands-on: an endpoint and peering
We'll continue with the network from part thirteen, after deleting the NAT Gateway, so private-server has no route to the internet. The commands use the variables VPC and RT_PRIVATE; if your CloudShell session ended, set them by hand from the VPC page.
Gateway endpoint
Give private-server an IAM role with AmazonS3ReadOnlyAccess, as we explained in part two. On the server, try:
aws s3 ls --cli-connect-timeout 5
It times out after several attempts, because there's no route to S3. Now add an endpoint from CloudShell:
aws ec2 create-vpc-endpoint --vpc-id $VPC \
--service-name com.amazonaws.$AWS_REGION.s3 \
--route-table-ids $RT_PRIVATE
aws ec2 describe-route-tables --route-table-ids $RT_PRIVATE \
--query "RouteTables[0].Routes[].[DestinationCidrBlock,DestinationPrefixListId,GatewayId]" \
--output table
You'll see a new route starting with pl- and pointing to vpce-. Repeat aws s3 ls on the server, and it returns at once with your buckets, or empty if you have none, with no internet at all.
VPC peering
Now we'll connect this network to the default one, which holds web-1 from part three. Get its private address:
aws ec2 describe-instances \
--filters "Name=tag:Name,Values=web-1" "Name=instance-state-name,Values=running" \
--query "Reservations[0].Instances[0].PrivateIpAddress" --output text
From private-server, try curl -m 5 http://WEB-PRIVATE-IP, and it times out. Now create the connection and accept it:
DEFAULT_VPC=$(aws ec2 describe-vpcs --filters Name=isDefault,Values=true \
--query "Vpcs[0].VpcId" --output text)
PEER=$(aws ec2 create-vpc-peering-connection \
--vpc-id $VPC --peer-vpc-id $DEFAULT_VPC \
--query VpcPeeringConnection.VpcPeeringConnectionId --output text)
aws ec2 accept-vpc-peering-connection --vpc-peering-connection-id $PEER
The connection alone isn't enough, so add a route on both sides:
aws ec2 create-route --route-table-id $RT_PRIVATE \
--destination-cidr-block 172.31.0.0/16 --vpc-peering-connection-id $PEER
DEFAULT_RT=$(aws ec2 describe-route-tables \
--filters Name=vpc-id,Values=$DEFAULT_VPC Name=association.main,Values=true \
--query "RouteTables[0].RouteTableId" --output text)
aws ec2 create-route --route-table-id $DEFAULT_RT \
--destination-cidr-block 10.0.0.0/16 --vpc-peering-connection-id $PEER
Repeat curl from private-server, and web-1's page comes back over the private address.
aws ec2 delete-route --route-table-id $DEFAULT_RT \
--destination-cidr-block 10.0.0.0/16
aws ec2 delete-vpc-peering-connection --vpc-peering-connection-id $PEER
Summary
- Peering is for two networks and isn't transitive; Transit Gateway is for when networks multiply.
- A gateway endpoint is free for S3 and DynamoDB; an interface endpoint covers the other services.
- VPN is quick to set up over the internet; Direct Connect is a private line that takes time.
- Data transfer is among the biggest surprises on a bill, so plan for it from the start.
Test yourself
Before you move on, try these questions to check you've got this part. Each one is a practical scenario, and the explanation appears once you answer.
Question 1 of 5
An order-processing application in a private subnet writes heavily to DynamoDB, and the team finds most of its NAT Gateway bill comes from this traffic. What cuts this cost with the least effort?
Show the answer
Correct answer: C
A gateway endpoint adds a route to DynamoDB that skips NAT, with no extra charge and no code changes.
- A: This exposes servers that should stay private, and public addresses cost money too.
- B: DynamoDB isn't a VPC you can peer with.
- D: This links your own site to AWS, and has nothing to do with traffic inside the VPC.
Review: VPC endpoints: AWS services without the internet in this part, and Gateway endpoints for Amazon DynamoDB in the AWS docs.
Question 2 of 5
A payments team runs an internal API behind a Network Load Balancer. Forty other VPCs need to call it, some of their ranges overlap with its own, and they mustn't reach anything else on its network. What fits?
Show the answer
Correct answer: B
With PrivateLink, each consumer gets a private interface in its own subnet that leads only to that service, so overlapping ranges cause no conflict.
- A: Peering doesn't work with overlapping ranges, and it connects whole networks.
- C: Gateway endpoints are only for S3 and DynamoDB.
- D: This puts an internal API on the internet.
Review: VPC endpoints: AWS services without the internet in this part, and AWS PrivateLink in the AWS docs.
Question 3 of 5
A small company must connect its office network to a VPC by next week. Traffic is light, the budget is tight, and the data has to be encrypted. Which option fits?
Show the answer
Correct answer: A
Site-to-Site VPN crosses the internet through an encrypted tunnel, and can be set up quickly at low cost.
- B: Setting it up takes weeks or months, and it costs more at first.
- C: This is for individual employees' devices, rather than a whole office network.
- D: This connects VPCs to each other, and can't reach an office.
Review: Connecting your office: Site-to-Site VPN in this part, and What is AWS Site-to-Site VPN? in the AWS docs.
Question 4 of 5
A company is growing to 30 VPCs across several accounts, plus three offices connected by VPN. It wants every network to connect in one place, with production kept apart from development. What fits with the least operational overhead?
Show the answer
Correct answer: D
Transit Gateway is a central hub for VPCs and VPNs, its route tables decide who talks to whom, and it can be shared across accounts through RAM.
- A: Thirty networks would need 435 connections, each with its own routes.
- B: This connects offices to each other through one gateway, not a set of VPCs.
- C: Peering isn't transitive, so the others couldn't reach each other through it.
Review: Transit Gateway: one hub in this part, and What is AWS Transit Gateway for Amazon VPC? in the AWS docs.
Question 5 of 5
A bank reaches AWS over a single 10 Gbps Direct Connect line. An audit finds two problems: the line is a single point of failure, and its traffic isn't encrypted. Which two actions address these? (Choose two.)
Show the answer
Correct answer: A + C
A second line in a second location keeps the bank connected if a line or a whole location fails. MACsec or a VPN on top adds the encryption the line doesn't have by default.
- B: A faster line is still one line, and still unencrypted.
- D: This reaches S3 and DynamoDB from inside a VPC, and has nothing to do with the line.
- E: This is still one line, and a partner doesn't encrypt it for you.
Review: Direct Connect: a private line in this part, and Encryption in AWS Direct Connect and AWS Direct Connect Resiliency Toolkit in the AWS docs.
That completes the networking section. You now know the visitor's path from your site's name to its data, and what's left is a place to store anything at any size. That's the next section's subject, starting in part fifteen with the basics of S3.