Creating a VPC Peering Connection
Tags: vpc, aws, peering, community
Published: 2023-05-24 12:13:23

Hi Friends,

Let us learn about the basics of VPC peering, limitations of vpc peering and how can we create a vpc peering connection between two VPCs.

## What is VPC peering
A VPC peering connection is a networking connection between two VPCs that enables you to route traffic between the VPCs using private IPv4 addresses or IPv6 addresses.The instances in these VPCs can communicate each other. VPCs can be created within the same AWS Account or with another AWS Account. AWS doesn't charge you for creating a Peering connection however you need to pay for the traffic flow between the VPCs. 
![VPC Peering Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0qaa88ywmze30wcwgce.png)

Let us assume that you have created the VPC peering for your VPCs residing in the same region and in the same account. You are trying to connect between instance A in VPC 1 and instance B in VPC 2. If both the instances are in the same AZ, then the connectivity charges is zero as your traffic is not flowing outside of the AZ.
Leverage Transit gateway if you need multiple VPCs to interconnect rather than creating peering connections.

## Limitations of VPC Peering
1. **VPC peering doesn't support transitive peering.** :- Let us assume that you have three VPCs. You had created a peering between "VPC A and VPC B" and "VPC B and VPC C". Don't expect that you can connect an instance in VPC A to an instance running in VPC C.

1. **Edge to Edge Routing is not supported** :- Let us assume that you have created a peering connection between VPC A and VPC B. VPC A has connected to an internet gateway or a VPN gateway. The instances in VPC B, cannot use an Internet gateway or VPN gateway connected to VPC A for the traffic Flow.

1. **Overlapping CIDR blocks are not supported** :- Your VPCs should not have the same or overlapping IP Address ranges.

1. You **cannot create multiple VPC peering** connections between 2 VPCs at the same time.

1. **Mind your VPC Peering connection quotas** 
{% embed https://docs.aws.amazon.com/vpc/latest/peering/vpc-peering-connection-quotas.html %}

## Creating a VPC Peering Connection

In this blog, I am going to create a VPC peering connection between my two VPCs located in the same region and same account.

- Navigate to the AWS portal--> VPC

- Search for Peering connections under the virtual private cloud section.

![VPC peering connection page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x65rlg98theegi0oivth.png)

- The peering connection page will appear. Click on "create peering connection" from the top right corner. 

![peered connections page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ni39s4i593y9o8h9xgzt.png)

- In the create peering connection page, provide the details as below

![new peering connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jultizajtbxme703o2sd.png)

1. Name of your peering connection

1. The VPC ID of the requester

1. Specify the AWS Account of the 2nd VPC to which you need to create the peering connection

1.Specify the AWS Region of the 2nd VPC to which you need to create the peering connection

1. The VPC ID of the Accepter

1. Provide tags if any and _click on Create peering connection_

Once you create the peering connection, you need to accept the peering connection. If you are connecting to a VPC in a different account, navigate to that account and accept the peering connection request
![Acceptance pending](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iiyo07tru2zex1jofsxq.png)
Verify the settings and click _accept request_

![Accept VPC request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n0bvtsvjt2v10ahdu4u.png)
You will get a confirmation once the request has been accepted.

![VPC Peering request confirmed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhobrqzpracl67eknugx.png)

##s Create Route Table
An important activity to perform as part of the VPC peering activity is to edit your route tables and add the cross VPC routing.


![Routing table structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hjfldihfjadg9m4jp83h.png)
The below diagrams shows the routing tables created for my VPCs.

![VPC A routing table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sas9495ysntu4lriei9z.png)
![VPC B routing table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkvkv689vd84uvmmrwke.png)

##s Testing the connection
Our VPC peering is done, now it time to test it. I had connected to VM1 in VPC A and tried an ICMP ping to my VM2 located in VPC B. _Note:_ I have enabled security group inbound rules for the ICMP traffic flow._ 

![Ping test across VPCs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2ltlkfk1gcwr2h03ax1.png)

Hope this short blog helps you to understand what is VPC peering and how to create a VPC peering connection.