DEV Community

Cover image for AWS VPC Peering: How to Connect Two VPCs Privately
Rajesh
Rajesh

Posted on

AWS VPC Peering: How to Connect Two VPCs Privately

Connecting Two AWS VPCs Using VPC Peering 🚀

Imagine you have two different VPCs in AWS.

One VPC has your application, and the other has your database.

Now your application needs to get data from the database. How can these two VPCs talk to each other?

AWS provides a feature called VPC Peering to solve this problem.

What is VPC Peering?

VPC Peering is simply a way to connect two VPCs so that they can communicate privately.

For example, let's say we have:

  • VPC A – 10.0.0.0/16
  • VPC B – 10.1.0.0/16

We can connect these two VPCs and allow resources inside them to communicate using their private IP addresses.

No public IP address is needed for communication between the VPCs.

How can we connect two VPCs?

Let's do it step by step.

  1. First, create two VPCs in your AWS account.
  2. Make sure both VPCs have different IP address ranges.
  3. Open the VPC service in the AWS Console.
  4. Go to Peering Connections and create a new connection.
  5. Select the VPCs you want to connect.
  6. Accept the connection request.
  7. Add the required routes in both VPC route tables.
  8. Update the security groups to allow communication.

Now your VPCs are ready to communicate!

One important thing to remember

Creating a peering connection alone is not enough.

You must also update the route tables on both sides.

For example:

  • In VPC A, add a route for 10.1.0.0/16.
  • In VPC B, add a route for 10.0.0.0/16.

Both VPCs need to know where to send the traffic.

Also, check the security groups and network ACLs if communication is not working.

Can three VPCs communicate through Peering?

Let's say:

  • VPC A is connected to VPC B.
  • VPC B is connected to VPC C.

Can VPC A directly communicate with VPC C through VPC B?

No!

This is called non-transitive peering.

If A needs to communicate with C, you must create another direct peering connection between them.

If you have many VPCs, AWS Transit Gateway can make network management easier.

Important points to know

Before using VPC Peering, remember:

  • Two VPCs cannot have overlapping IP ranges.
  • Peering connections are not transitive.
  • Both sides need proper route table entries.
  • Security groups must allow the required traffic.
  • You cannot use a peer VPC's NAT Gateway or Internet Gateway through peering.
  • Data transfer charges apply.

When should we use VPC Peering?

Here are a few examples:

  • An application in one VPC needs to access a database in another VPC.
  • Two different AWS accounts need private communication.
  • Development and testing environments need to share resources.
  • Applications running in different AWS Regions need private connectivity.

Top comments (0)