# AWS VPC Peering

### What is actually VPC peering?

A *virtual private cloud* (VPC) is a virtual network dedicated to your AWS account. It is logically isolated from other virtual networks in the AWS Cloud. You can launch AWS resources, such as Amazon EC2 instances, into your VPC.

A VPC peering connection is a networking connection between two VPCs that enables you to route traffic between them using private IPv4 addresses or IPv6 addresses. Instances in either VPC can communicate as if they are within the same network. You can create a VPC peering connection between your VPCs or a VPC in another AWS account. The VPCs can be in different Regions (also known as an inter-Region VPC peering connection).

### **Pricing for a VPC peering connection**

There is no charge to create a VPC peering connection. All data transfer over a VPC Peering connection within an Availability Zone (AZ) is free. Charges apply for data transfer over VPC Peering connections that cross Availability Zones and Regions.

## LAB: How to create VPC peering?

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686486608564/490cc68e-e79d-43c2-96ad-439fb8b9e4b4.png align="center")

Note: In this lab session we are going to create two different instances and try to connect the private IP of one instance by using another instance.

### Step 1: Create a First VPC

1. **Goto** `Your VPCs` **and Create vpc1:**
    
    * Resources to create `VPC only`
        
    * Name tag - optional eg. `vpc1`
        
    * IPv4 CIDR eg. `10.0.0.0/16`
        
        **<mark>Create VPC </mark>** with all other default
        
2. **Goto** `Subnets` **and create subnet for** `vpc1` **as:**
    
    * VPC ID select `vpc1`
        
    * Scroll down to Subnet settings
        
        * Subnet name `vpc1subnet`
            
        * Availability Zone anyone your choice eg. `us-east-1a`
            
        * IPv4 CIDR block: `10.0.0.0/24`
            
            and **<mark>Create subnet</mark>**
            
3. Goto `Internet gateways` and Create internet gateway as:
    
    * In Internet gateway setting
        
        * Name tag: `IGvpc1`
            
        * **<mark>Create internet gateway</mark>**
            
    * Click on the popup **Attach to VPC**
        
        * Available VPCs -&gt; select a VPC : `vpc1`
            
            **<mark>Attach internet gateway</mark>**
            
4. Goto `Route tables` and create a route table for `vpc1`
    
    * Route table settings
        
        * Name: `route_vpc1`
            
        * VPC: Select `vpc1`
            
            **<mark>Create route table</mark>**
            

### Step 2: Similarly, Create a Second VPC

1. **Goto** `Your VPCs` **and Create vpc2:**
    
    * Resources to create `VPC only`
        
    * Name tag - optional eg. `vpc2`
        
    * IPv4 CIDR eg. `194.0.0.0/16`
        
        **<mark>Create VPC </mark>** with all other default
        
2. **Goto** `Subnets` **and create subnet for** `vpc2` **as:**
    
    * VPC ID select `vpc2`
        
    * Scroll down to Subnet settings
        
        * Subnet name `vpc2subnet`
            
        * Availability Zone anyone your choice eg. `us-east-1b`
            
        * IPv4 CIDR block: `194.0.0.0/24`
            
            and **<mark>Create subnet</mark>**
            
3. Goto `Internet gateways` and Create internet gateway as:
    
    * In Internet gateway setting
        
        * Name tag: `IGvpc2`
            
        * **<mark>Create internet gateway</mark>**
            
    * Click on the popup **Attach to VPC**
        
        * Available VPCs -&gt; Select a VPC : `vpc2`
            
            **<mark>Attach internet gateway</mark>**
            
4. Goto `Route tables` and create a route table for `vpc2`
    
    * Route table settings
        
        * Name: `route_vpc2`
            
        * VPC: Select `vpc2`
            
            **<mark>Create route table</mark>**
            

### Step 3: Now, Create a peering connection:

* Goto `Peering connections` by scrolling down on the left side of the bar
    
* Click on <mark>Create peering connection</mark>
    
* Peering connection settings
    
    * Name: `peerVPC`
        
    * VPC ID (Requester): Select `vpc1`
        
    * VPC ID (Accepter): Select `vpc2`
        
        <mark>Create peering connection</mark>
        
* Select `peerVPC` -&gt; `Actions` -&gt; `Accept request`
    

### Step 4: Last thing to do Modify a route table:

* Goto `Route tables`
    
* Select `route_vpc1`
    
    * Click on `Subnet associations` -&gt; `Edit subnet associations`
        
        * Available subnets: Select `vpc1subnet` and <mark>Save associations</mark>
            
    * Click on `Routes` -&gt; `Edit routes` -&gt; `Add route`
        
        Destination: `0.0.0.0/0` Targe: `Internet Gateway` -&gt; `(IGvpc1)`
        
        Destination: `194.0.0.0/16` Targe: `Peering Connection` -&gt; `(peerVPC)`
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686490799813/829d396d-afb3-4164-9268-fdc0bdfb9f91.jpeg align="center")

* Similarly, Select `route_vpc2`
    
    * Click on `Subnet associations` -&gt; `Edit subnet associations`
        
        * Available subnets: Select `vpc2subnet` and <mark>Save associations</mark>
            
    * Click on `Routes` -&gt; `Edit routes` -&gt; `Add route`
        
        Destination: `0.0.0.0/0` Targe: `Internet Gateway` -&gt; `(IGvpc2)`
        
        Destination: `10.0.0.0/16` Targe: `Peering Connection` -&gt; `(peerVPC)`
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686491046077/dddf949b-7edb-40dc-99e6-83a9b0850518.jpeg align="center")

### Step 4: Now, Smile all VPC work was done. Next goto EC2 -&gt; instances

1. **For First Instance**
    
    * Launch instances
        
    * Name: `server1`
        
    * select the existing key or create new
        
    * Network settings -&gt; `Edit`
        
        * VPC: Select `vpc1`
            
        * Subnet: `vpc1subnet`
            
        * Auto-assign public IP: `enable`
            
        * Leave Create a security group checked
            
        * Leave all default and **<mark>Launch instance</mark>**
            
2. **For Second Instance**
    
    * Launch instances
        
    * Name: `server2`
        
    * select the existing key or create new
        
    * Network settings -&gt; `Edit`
        
        * VPC: Select `vpc2`
            
        * Subnet: `vpc2subnet`
            
        * Auto-assign public IP: `desable`
            
        * Leave Create a security group checked
            
        * `Add security group rule`
            
            * Type: `All traffic`
                
            * Source type: `Anywhere`
                
                \[ Note: It is not good practice to set the type `All traffic` just for basic understanding \]
                
        * Leave all default and **<mark>Launch instance</mark>**
            

### Step 4: Connect the EC2 instance

* After the Status check `2/2 checks passed`
    
* Select the EC2 instance with private IP i.e. `server2` and copy private IP
    
* Then, Select the EC2 instance with public IP i.e. `server1`
    
* And Click on `Connect` at the top, again <mark>Connect</mark>
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686492508054/ebb26b99-1891-4239-a869-b1028535a93c.jpeg align="center")

* Now, you reached this window:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686492594539/e96ec54d-f131-49ae-a708-c4a41ea5bfde.jpeg align="center")

* Here, perform the following commands:
    
    ```bash
    sudo su
    ```
    
    ```bash
    ping <private-ip-you-copied>
    ```
    
    * You get the following output `CTRL+C to exit`
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686492999197/1fc8baed-29c7-46fe-82fa-dedd502a3c4e.jpeg align="center")
    

### Don't forget to terminate all the services you started...

# ***Thanks for your time***
