Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

What Is VPC Peering in AWS and How to Set It Up

A VPC is a private network that nothing else can enter unless you open a door (Why a custom VPC). Sometimes two private networks must talk: your app and a partner's database, or your production VPC and a shared-services VPC. Opening the database to the internet is the wrong answer. This chapter explains VPC peering: a private link between two VPCs, the rules it follows (no overlapping CIDRs, not transitive), why you must never accept a peering request you didn't expect, and a full console walk-through: create the peering, accept it, add routes on both sides and open the security groups.

In this class

  • Why two VPCs can't talk by default
  • What VPC peering is
  • Requester, accepter, and the 7-day expiry
  • Routes on both sides, and security group rules
  • Rule 1: CIDRs must not overlap
  • Rule 2: peering is not transitive
  • Why you accept only requests you expect
  • VPC quotas, and the console walk-through

Suppose we are Hotstar, streaming a cricket league. The live scores sit in the league's private database, in the league's own VPC.

  1. Two VPCs are fully separate networks. A server in one can't reach a private IP in the other.
  2. The wrong fix: give the database a public IP and open it to the internet. Now anyone can try to break in.
  3. The right fix: the league peers its VPC with ours. Our app servers reach the database by its private IP, and nothing is exposed to the internet.

Classroom line

Talking between two VPCs privately: that is VPC peering.

Another common case: your own production VPC and a shared-services VPC (monitoring, a build server, a shared database).

Suppose we are Hotstar: reach the league'sdatabase privately Wrong: open the database to the internet Our VPC: App server Internet anyone can try · attackers League VPC: Database, public IP Right: VPC peering, private IP to private IP Our VPC · 10.0.0.0/16 App server 10.0.2.15 pcx-... League VPC · 172.16.0.0/16 Database 172.16.1.20 Nothing is exposed to the internet.

Figure 1. Suppose we are Hotstar and need the league's live scores. Opening the league database to the internet exposes it to everyone. With VPC peering, our app server reaches it by private IP and nothing is public.

2. What VPC peering is

  1. A peering connection is a private link between exactly two VPCs. Its ID looks like pcx-0a1b2c3d4e5f6a7b8.
  2. The VPCs can be in the same account or different accounts, and in the same region or different regions.
  3. Servers talk private IP to private IP. The traffic stays on the AWS network; no internet gateway, NAT or VPN is needed for it.
  4. There is no hourly charge for the peering itself. Data that crosses AZs or regions is charged per GB, like other data transfer.

3. How a peering is set up

Use two route tables to complete the private path

Draw VPC A with 10.10.0.0/16 and VPC B with 10.20.0.0/16. After their peering connection becomes active, give A’s participating subnet a route to B’s range through the peering connection. Give B’s participating subnet a return route to A’s range. The destination security rules still need to allow the approved connection.

Use a tiny web test on a permitted private instance. Test from A to B using its private address and port. If you see a timeout while the peering state looks active, check the route table actually associated with each source and return subnet. The peer status only says the connection object exists.

Suppose VPC A is 10.0.0.0/16 (ours) and VPC B is 172.16.0.0/16 (the partner's).

  1. Request: the owner of VPC A creates a peering connection to VPC B. A is the requester.
  2. Accept: the owner of VPC B sees it as Pending acceptance and accepts it. B is the accepter. A request that isn't accepted expires after 7 days.
  3. Routes on both sides: A's route table gets 172.16.0.0/16 → pcx-..., and B's route table gets 10.0.0.0/16 → pcx-.... Without the route on either side, the replies have no way back.
  4. Security groups: B's database SG allows 3306 from A's app subnet, for example 10.0.2.0/24.
  5. Now 10.0.2.15 can connect to 172.16.1.20:3306.
Setting up a peering: request, accept, routes,security group VPC A (requester) · 10.0.0.0/16 app 10.0.2.15 172.16.0.0/16 → pcx-... pcx-... Pending 1. A creates a peering request to B. Status: Pending acceptance(expires after 7 days). Active 2. B checks the account ID and CIDR, then accepts. Status: Active.No traffic yet: no routes. VPC B (accepter) · 172.16.0.0/16 db 172.16.1.20 10.0.0.0/16 → pcx-... SG: 3306 from 10.0.2.0/24 3. Routes on both sides and the SG rule: 10.0.2.15 connects to172.16.1.20:3306.

Figure 2. Animation: VPC A (10.0.0.0/16) requests a peering to VPC B (172.16.0.0/16); B accepts within 7 days; each side adds a route to the other's CIDR through pcx-..., and B's security group allows 3306 from A's app subnet.

Routes are needed on both sides VPC A route table Destination Target 10.0.0.0/16 local 172.16.0.0/16 pcx-... 0.0.0.0/0 igw-... VPC B route table Destination Target 172.16.0.0/16 local 10.0.0.0/16 pcx-... Missing on A: requests never leave A. Missing on B: requests arrive, butreplies have no way back. Add the route to the route table the subnet actually uses.

Figure 3. Route table of VPC A: 10.0.0.0/16 local, 172.16.0.0/16 to pcx-.... Route table of VPC B: 172.16.0.0/16 local, 10.0.0.0/16 to pcx-.... Both are needed.

Route tables themselves are explained in How to Create Public and Private Subnets in AWS VPC.

4. Rule 1: the CIDRs must not overlap

If both VPCs contain 10.0.1.5, a route can't tell which one you mean. So AWS refuses to peer overlapping ranges.

  1. 10.0.0.0/16 and 172.16.0.0/16: no overlap. Peering works.
  2. 10.0.0.0/16 and 10.1.0.0/16: no overlap (10.0.x.x vs 10.1.x.x). Peering works.
  3. 10.0.0.0/16 and 10.0.128.0/17: the second is inside the first (10.0.128.0 to 10.0.255.255). Overlap; peering fails.
  4. Two default-style VPCs both on 10.0.0.0/16: overlap; peering fails.

So plan the CIDRs of all your VPCs on day one, different for each. How to calculate the ranges: What Is AWS VPC and How to Calculate CIDR IP Ranges.

Can these two VPCs be peered? The CIDRs mustnot overlap 10.0.0.0/16 + 172.16.0.0/16 peering works different ranges 10.0.0.0/16 + 10.1.0.0/16 peering works 10.0.x.x vs 10.1.x.x 10.0.0.0/16 + 10.0.128.0/17 overlap: fails 10.0.128.0-10.0.255.255 is inside the first 10.0.0.0/16 + 10.0.0.0/16 overlap: fails same range

Figure 4. 10.0.0.0/16 with 172.16.0.0/16 or with 10.1.0.0/16: no overlap, peering works. 10.0.0.0/16 with 10.0.128.0/17 or with another 10.0.0.0/16: overlap, peering fails.

5. Rule 2: peering is not transitive

Explain why a middle VPC cannot pass the traffic on

On paper, connect VPC A to B and B to C using two peering lines. Now draw an app in A trying to reach a database in C. C is not directly peered with A. B does not become a transit router merely because its two peering links exist. The two successful tests A-to-B and B-to-C do not prove the third path.

Choose a direct peering connection when only those two VPCs need the path and CIDRs allow it, or a supported transit design when a group of networks needs managed connectivity. Plan and verify intended route and security rules at both ends before adding another connection.

  1. Suppose A is peered with B, and B is peered with C.
  2. A still can't reach C through B. B doesn't pass traffic on.
  3. If A needs C, create a separate A-C peering, with its own routes and SG rules.
  4. With many VPCs this becomes many links (3 VPCs need 3 peerings, 4 need 6). At that size, teams use AWS Transit Gateway as a central hub instead.
Peering is not transitive VPC A 10.0.0.0/16 A-B VPC B 10.1.0.0/16 B-C VPC C 10.2.0.0/16 ✗ A → B → C is dropped: B does not pass traffic on. A-C Fix: a separate A-C peering, with its own routes and SG rules.

Figure 5. Animation: A is peered with B and B with C, but traffic from A to C through B is dropped; a separate A-C peering is needed.

6. Accept only the requests you expect

Anyone with an AWS account who knows your VPC ID can send you a peering request. Accepting it, and then adding routes, connects their network to yours.

Classroom line

Suppose two roommates work for different companies.

They share a flat, but neither would connect his office laptop to the other company's network. VPCs are the same.

Suppose a software company is building a government ID system and needs private access to the government's database VPC for that project. Before accepting:

  1. Check the requester account ID: is it the partner you agreed with?
  2. Check the requester VPC CIDR: is it the range they told you?
  3. Confirm by a separate channel (email or call) that they sent it.
  4. After accepting, add routes only for the subnets that need it, and open SG rules only for the ports that need it, from the narrowest source.
  5. Anything unexpected: Reject.
Before you accept a peering request 1. Requester account ID = the partner you agreed with? 2. Requester VPC CIDR = the range they told you? 3. Confirmed by a separate channel (email or call)? 4. Accept, then add routes only for the needed subnets 5. Open only the needed ports, from the narrowest source Anything unexpected? Reject a wrong accept + routes opens your network

Figure 6. Before accepting a peering request: check the requester account ID, check the requester CIDR, confirm by a separate channel, then open only the routes and ports needed. Anything unexpected: Reject.

7. Quotas and running out of IPs

  1. VPCs per region: 5 by default. You can ask for more in Service Quotas.
  2. Running out of IPs in a VPC? Don't create a second VPC and peer it just for space. First add a secondary CIDR to the same VPC (VPC > Actions > Edit CIDRs), then create new subnets in it.

8. Build it in the console

Part A: two VPCs to practise with (one account, ap-south-1)

  1. VPC > Create VPC > VPC only: vpc-a, CIDR 10.0.0.0/16. Create a subnet 10.0.1.0/24.
  2. Create vpc-b, CIDR 172.16.0.0/16, with a subnet 172.16.1.0/24.
  3. Launch one small instance in each subnet. Give the one in vpc-a a way in for SSH (public subnet with an internet gateway, or Session Manager).
  4. On the vpc-b instance, install a web server: sudo yum install nginx -y, then sudo service nginx start.

Part B: create the peering (requester)

  1. VPC > Peering connections > Create peering connection.
  2. Name a-to-b. VPC ID (Requester): vpc-a.
  3. Select another VPC to peer with: My account (or Another account with its account ID), This Region (or Another Region), VPC ID (Accepter): vpc-b.
  4. Create peering connection. Status: Pending acceptance.

Part C: accept it (accepter)

  1. In the accepter account and region, open Peering connections and select the request.
  2. Check the requester account ID and CIDR (section 6).
  3. Actions > Accept request > Accept request. Status: Active.

Part D: routes on both sides

  1. Route tables, open the one used by vpc-a's subnet, Routes > Edit routes > Add route.
  2. Destination 172.16.0.0/16, target Peering Connection a-to-b. Save changes.
  3. Open the route table of vpc-b's subnet and add destination 10.0.0.0/16, target a-to-b. Save changes.

Part E: security group rules

  1. Open the SG of the vpc-b instance, Edit inbound rules.
  2. Add HTTP 80, source 10.0.1.0/24 (only vpc-a's subnet).
  3. For a ping test, add All ICMP - IPv4 from the same source.
  4. In the same region you can use the peer VPC's security group ID as the source instead of a CIDR.

Part F: test

  1. SSH to the vpc-a instance.
  2. Run ping -c 3 172.16.1.20 (use your vpc-b instance's private IP).
  3. Run curl http://172.16.1.20. The Nginx welcome page comes back over the private link.

9. When something goes wrong

  1. Create fails with overlapping CIDRs: the two ranges overlap. Peering needs different ranges; plan them again.
  2. Status stays Pending acceptance, then Expired: nobody accepted within 7 days. Create a new request.
  3. Active, but ping and curl time out: a route is missing on one side, or it was added to a route table the subnet doesn't use.
  4. Routes fine, still time out: the target SG doesn't allow the port (or ICMP) from the other VPC's range, or a NACL blocks it.
  5. A can reach B and B can reach C, but A can't reach C: peering isn't transitive. Peer A with C directly.

Ravindra Bagale's Tip

Give every VPC its own CIDR from day one, written down in one place, so any two can be peered later. Treat a peering request like a request to plug a stranger's cable into your office network: check the account ID and the CIDR, confirm with the person, and open only the routes and ports that are needed.

10. The whole class on one page

  1. Two VPCs can't talk by default. Opening a database to the internet is the wrong fix; peering is the private one.
  2. Peering: a private link between two VPCs, same or different accounts and regions, private IP to private IP.
  3. Requester creates, accepter accepts (within 7 days).
  4. Routes on both sides to the other CIDR through pcx-..., plus SG rules for the needed ports.
  5. CIDRs must not overlap. Peering is not transitive.
  6. Accept only requests you expect: check the account ID and CIDR.
  7. 5 VPCs per region by default (raisable). Out of IPs: add a secondary CIDR first.

11. Try at home

Try at home

Task 1: on paper

  1. Which pairs can be peered? 10.0.0.0/16 and 10.0.64.0/18; 10.0.0.0/16 and 192.168.0.0/16; 172.16.0.0/16 and 172.17.0.0/16.
  2. Four VPCs must all reach each other with peering. How many peering connections are needed?

Task 2: in the console

  1. Build vpc-a and vpc-b as in section 8, peer them, add both routes and the SG rule.
  2. From vpc-a, curl the private IP in vpc-b. Then delete one of the two routes and try again: it should time out.

Task 3: clean up

  1. Terminate both instances, delete the peering connection, then the two VPCs.

Common errors and fixes

Error / symptomFix
Peering says Active but connection times outCheck routes in both directions, subnet associations, SGs/NACLs and whether the application listens.
CIDR overlapsVPC peering cannot connect overlapping CIDRs. Re-plan addressing or use another suitable application/network design.
A reaches B, B reaches C, A cannot reach CPeering is non-transitive. Add direct A–C peering or design with Transit Gateway.
Trying to borrow the peer’s NAT/IGWPeering does not provide edge-to-edge access through the peer’s NAT, IGW, VPN or S3 gateway endpoint.

FAQ

Q. Is Active proof the application works?

No. It is connection lifecycle state. Routes, permissions and app health still determine reachability.

Q. Same account only?

No. Peering can be between supported VPCs across accounts and Regions, with the proper request/accept flow.

Q. Can I use SG references across Regions?

Do not assume it. Peer SG references are supported for same-Region peering; inter-Region rules need an appropriate supported source such as CIDR.

Q. Why not connect every VPC to one hub with peering?

Because the hub cannot forward transit traffic through peering. Larger networks need a deliberate transit design.

Recap

In short

  1. VPC peering privately connects two VPCs, without the internet.
  2. Request, accept, routes on both sides, security groups: all four are needed.
  3. No overlapping CIDRs, and no transit through a third VPC.
  4. Accept only what you expect, and open only what you need.

Practise this chapter

Interview questions and answers

How do you prove a new VPC peering connection is usable?

Check that it is active, then inspect the route tables actually attached to both participating subnets and the network permissions. Make a permitted test request to the destination service’s private address.

If two VPCs peer successfully, when might private DNS still surprise users?

DNS resolution has its own settings and referenced names must be usable in the client network. Test the approved private address and the hostname separately so a naming failure is not confused with a routing one.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. A↔B and B↔C peering links are active. Can A use B to reach C through those links alone?

Answer and explanation

D. No, VPC peering is not transitive

A separate connection or a supported transit design is needed. Two working individual peer links do not create transit through the middle VPC.

2. A peering link is Active, but A-to-B times out. Which check remains necessary?

Answer and explanation

A. Relevant subnet routes in both directions and the intended security rules

An active peering object does not install all routes or security permissions. Confirm the actual route associations and both directions.

3. Two VPCs both use 10.0.0.0/16. What does this mean for a VPC peering plan?

Answer and explanation

A. The overlapping CIDRs prevent peering

Peering requires non-overlapping address ranges so routes can identify which side owns the destination.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Ahmednagar Sugar Co-op is a sugar co-operative business in Ahilyanagar. Balasaheb Pawar looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Easy How many addresses in a /16?

Picking a VPC size needs this count.

Ahmednagar Sugar Co-op plans the VPC 10.26.0.0/16. How many IP addresses does it hold in total?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Create a VPC

A VPC is your own private network in AWS. Everything else lives inside it.

Create a VPC with the CIDR block 10.5.0.0/16.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy Create an internet gateway

Without an internet gateway, nothing in the VPC can reach the internet.

Create a new internet gateway.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Create a subnet in one AZ

Subnets split the VPC range and each one lives in exactly one AZ.

In vpc-0e5ec252f5deadbee, create subnet 10.5.1.0/24 in ap-south-1a.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Find the VPC router address

AWS reserves base+1 for the router.

Which address does AWS reserve for the VPC router in subnet 10.26.4.0/24?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Usable IPs in an AWS /24 subnet

AWS keeps 5 addresses in every subnet.

How many addresses can Ahmednagar Sugar Co-op use for servers in subnet 10.26.1.0/24 on AWS?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Add the internet route

A subnet is "public" only when its route table sends 0.0.0.0/0 to an internet gateway.

In route table rtb-031d5da252c5bb22, send all internet traffic (0.0.0.0/0) to igw-01d5da252c5aa11.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Do these VPCs overlap?

Peering fails when ranges overlap.

Can VPC 10.26.0.0/16 peer with VPC 10.26.128.0/17? Answer yes or no.

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Link a subnet to a route table

Until you associate it, a subnet uses the main route table.

Associate subnet subnet-031d5da252c5bb1 with route table rtb-031d5da252c5bb22.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Attach the internet gateway

A gateway does nothing until it is attached to a VPC.

Attach igw-01d5da252c5aa11 to vpc-0e5ec252f5deadbee.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Pick the smallest prefix

Right-sizing saves address space.

A subnet must hold 100 servers on AWS. What is the largest prefix number (smallest subnet) that fits? Answer like /25.

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Split a /24 into two

Public and private subnets often split one block.

Split 10.26.8.0/24 into two equal subnets. Write both CIDRs separated by a comma.

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: What is VPC peering?. Two VPCs cannot talk by default, even in the same account. A peering connection links two VPCs privately over AWS's network. Request, accept (within 7 days), add routes on both sides, allow in SGs. Route in VPC A:
Revision card: What is VPC peering? · Download PNG
Revision card: Peering rule: no overlapping CIDRs. If both VPCs use 10.0.0.0/16, a packet to 10.0.1.5 has two possible homes. Peering is refused when the CIDRs overlap. Plan unique CIDRs per VPC from day one. OK pair:
Revision card: Peering rule: no overlapping CIDRs · Download PNG
Revision card: Peering is not transitive. People assume A–B and B–C means A can reach C. Each pair needs its own peering. Traffic never passes through a middle VPC. Create A–C too, or use a Transit Gateway for many VPCs. 3 VPCs fully connected = 3 peerings.
Revision card: Peering is not transitive · Download PNG
Comparison: VPC peering vs Transit Gateway. VPC peering: A direct private link between two VPCs. Transit Gateway: A central hub that many VPCs attach to.
Comparison: VPC peering vs Transit Gateway · Download PNG
Interview card: A is peered with B, B with C. Can A reach C?. No. VPC peering is not transitive. Traffic cannot pass through B. Add A–C peering or use Transit Gateway.
Interview card: A is peered with B, B with C. Can A reach C? · Download PNG
Interview card: Peering is active but ping fails. What do you check?. Routes on BOTH sides and SG rules allowing the other CIDR. Peering only connects; routes and SGs decide traffic. A: 10.1.0.0/16 › pcx; B: 10.0.0.0/16 › pcx; SG allow ICMP.
Interview card: Peering is active but ping fails. What do you check? · Download PNG
Interview card: Why accept only peering requests you expect?. Accepting connects a stranger's network to yours. With routes added, they could reach your servers. Check requester account ID and CIDR first.
Interview card: Why accept only peering requests you expect? · Download PNG

More cards: Ravi Sir's Cards.