# VPC Peering

Hello everyone,

Today, we'll be exploring VPC peering in AWS. In Amazon Web Services (AWS), a Virtual Private Cloud (VPC) is a logically isolated network segment you create within the cloud. You can launch resources like EC2 instances into your VPC.

VPC peering is a networking connection established between two VPCs that enables you to route traffic between them using private IP addresses.

**Why do we need VPC peering?**

VPC peering is essential for enabling private communication between VPCs without relying on public IPs or the internet. It facilitates secure and efficient sharing of resources across different VPCs, whether they reside within the same account, different accounts, or even different regions.

Alright, time to put our knowledge to work!

---

### Overview

We will set up two VPCs, each containing instances. We will SSH into our instances and initiate communication with instances in the other VPC to test if our VPC peering connection is working.

---

### Task 1: Creating VPCs

Search for **VPC** in the search box.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717414735194/ed0402c3-65c6-4838-add1-eedd76c495a7.png align="left")

On **VPC dashboard**, click on `Create VPC`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717414921324/1d6079c7-f626-49ea-bf9a-4d3514adba41.png align="left")

Provide a name for your VPC and input a CIDR range value for your VPC.

Click on `Create VPC`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717414969048/fcc99705-3c65-420c-935a-0d4b3a013307.png align="center")

Let's create another VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415128290/8d2fc224-6aaa-4cae-adbc-e82d91e0a6a3.png align="left")

Provide a suitable name and input a CIDR range value for your second VPC.

Click on `Create VPC`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415190194/c51bc16a-8813-43c0-b8f1-abe449e0ec0d.png align="center")

We have created two VPCs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717546208267/f6b8b86c-e076-4c25-a902-d0c4f85de3f4.png align="center")

---

### Task 2: Creating Internet gateway

On **Internet gateways**, click on `Create internet gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717420054888/9ad3e5ca-a76c-41f3-82d0-47f018d7e3b6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717420131229/fd63d27c-cca8-435c-826c-7eace325c57f.png align="left")

Provide a name for your Internet gateway and click on `Create internet gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717420187006/c6299c96-0ce3-48cd-8eea-d1ad363ae1fd.png align="left")

After creating internet gateway, attach to your first VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717420257481/adfae176-1325-437c-b1fe-45f8c51d195b.png align="left")

Similarly, create another internet gateway and attach to the second VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717546320555/64ae8e82-d683-4fa9-9248-7e74ecd07e68.png align="center")

---

### Task 3: Creating Subnets

On **Subnets**, click on `Create subnet`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415259081/6c8f9e54-db62-4a69-88d3-65003f88993c.png align="center")

Select your first VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415310404/fc606926-bb58-4bdd-9cb3-fbdea5ced159.png align="left")

Provide a suitable name for your VPC, choose AZ and input a valid CIDR range for your subnet.

Click on `Create subnet`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415377903/2714c600-7a5f-4126-8eb1-55a041f379a5.png align="left")

Let's create another subnet in our second VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415442808/0cfec5ef-3965-46e5-9e6a-871c06233a40.png align="left")

Provide the name for your subnet, select AZ, and input an eligible CIDR range.

Click on `Create subnet`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415596385/3dc4c9e6-9b79-4a68-a0fe-1340091eb623.png align="center")

We have created two subnets in different VPCs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717546635266/ac011215-450d-4005-bf14-df634219fdb5.png align="center")

---

### Task 4: Creating Route tables

On **Route tables**, click on `Create route table`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415635263/e4e1003e-cd82-44b2-99d8-2116cb454ec1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415714941/cc2a05e5-5482-47d5-ade3-dbde678e6241.png align="left")

Provide the name for your route table and select your first VPC.

Click on `Create route table`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415778458/28797bee-3ed6-48bf-8594-4050220da708.png align="center")

On **Subnet associations**, click on `Edit subnet associations`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415835891/5ccc4d15-079f-43e3-a1a5-fb16a121618f.png align="left")

Select your subnet and click on `Save associations`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717420341559/48252201-0efd-4989-9b4f-8f828e240ac5.png align="left")

On **Routes**, click on `Edit routes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717420380294/ac2d78b8-3a24-492c-ba13-6d0edae452c4.png align="center")

Click on `Add route` to add the above configurations.

Click on `Save changes`.

Now, let's create another route table, associate the corresponding subnet to it & add the necessary route.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415927199/34c0f658-f966-4708-ad6a-5a25c3fa85f6.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717415961944/5c8c1a26-e3c1-4917-9a1e-6a805ba09e99.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717420529073/3d1a8b0b-208b-45e5-aa28-91f60d58bf23.png align="center")

And now we have set up our second route table.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717416002151/2912a722-7ee8-43bc-91ce-c0e4fcd8344f.png align="left")

We have created two route tables for two VPCs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717546782769/66836a21-7d63-4bbd-a71a-26095c1f361d.png align="center")

---

### Task 5: Creating EC2 instances

Go to **EC2 dashboard**, on Instances, click on `Launch instances`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717416107676/1653b4b8-3f6d-44a1-ad05-2e4fb40bc36b.png align="left")

Provide name for your EC2, select an AMI. Create or use an existing key pair.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717420822887/d9acb0fd-4e70-4695-a227-4e1f81d6806d.png align="left")

`Edit` Network settings. Select your first VPC, subnet and **Enable** the Auto-assign public IP.

We are provided with default SSH configuration.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717470974788/0e86a098-8458-4c67-95d8-b58796773ba4.png align="left")

Click on `Add security group rule`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717471017237/f6e4e786-79cc-4c27-81f0-26439d44c1e3.png align="left")

Configure the above configurations in your Security group rule.

On **Advanced Details**, scroll to **User data** and paste the following command.

```bash
#!/bin/bash

# Update and install apache2
apt-get update
apt-get install -y apache2

# Create a simple index.html file with "Hello" text
echo "Hello FIRST" > /var/www/html/index.html

# Restart apache2 service
systemctl restart apache2
```

Leave all other configurations as default and click on `Launch instance`.

Now, let's create another instance.

Provide name for your instance, choose an AMI, either create or use an existing key pair.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717420979354/0cf9c421-bba6-4095-92d3-52ee4ff1d830.png align="left")

`Edit` Network settings, select your second VPC, subnet and **Enable** the Auto-assign public IP.

Repeat the necessary instructions (security group rule) as earlier.

Here is the user data for the second instance.

```bash
#!/bin/bash

# Update and install apache2
apt-get update
apt-get install -y apache2

# Create a simple index.html file with "Hello" text
echo "Hello SECOND" > /var/www/html/index.html

# Restart apache2 service
systemctl restart apache2
```

Leaving all other configurations as default, click on `Launch instance`.

We have created two EC2 instances in different subnets residing in different VPCs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717471636548/07d7fd32-157b-4eed-a172-9752b3fc7a40.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717546920974/dcf653be-035f-4d22-9444-0b8048c6ec54.png align="center")

---

### Task 6: Establishing a VPC Peering Connection

A peering connection is a networking connection between two Virtual Private Clouds that allows them to communicate as if they are on the same network.

Go to **VPC dashboard**, scroll down to **Peering connections**, click on `Create peering connection`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717424812073/cdd204c0-ef03-4c4f-9b65-46e73322bada.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717424876496/dac8323a-2dfe-455d-9c86-5a9c0a4985f5.png align="left")

Provide a suitable name and select your first VPC as Requester.

One VPC initiates a peering connection request to another VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717425056053/2b1034f0-2186-4954-bd25-df1f7a06b966.png align="left")

We can establish a peering connection to a VPC in our own account as well as to a VPC in another account.

The owner of the target VPC accepts the peering request.

Click on `Create peering connection`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717425246247/dd7106e8-dc47-40be-a3cb-7ce9be4c45ca.png align="left")

Since both VPCs reside within the same account, this acceptance request message appears. In the Actions panel, click `Accept request` to proceed.

Now, both VPCs must update their route tables to direct traffic destined for the other VPC through the peering connection.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717425384614/670ceecf-4cb7-4fba-9cb2-95a535a338d4.png align="left")

On **Route tables**, click on one of your Route table.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717425433415/6af3b16f-bed1-4af1-8de4-f1fbc1c2fb19.png align="left")

Click on `Edit routes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717425478295/d9adc8bf-607f-43b5-8d5a-5635b0d3f00f.png align="left")

Click on `Add route`.

Duplicate your current tab and copy the IPv4 CIDR of your second VPC.

**Note** that for the first router, IPv4 CIDR of second VPC must be configured while for second router, IPv4 CIDR of the first VPC is considered.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717462408145/b3f52e95-ee88-4fa0-8c14-6d5f72886ccc.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717462653828/37c40dc8-401b-42ac-9c13-3c703126ab93.png align="center")

Paste into the **Destination** field and select your Peering connection in **Target** field.

Click on `Save changes`.

Now, let's set up the necessary route for the second VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717463007394/db8ef4c2-5cea-438a-b330-36aada78468f.png align="left")

Copy the IPv4 CIDR of your first VPC and paste it into the destination field in your second route table.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717463040245/abef465a-674f-4026-b077-c4caadf01575.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717463154409/193b0f86-5f05-494d-b6fb-70c9b45fd9dd.png align="center")

Click on `Save changes`.

Now, we have set up the necessary peering routes in the respective route tables.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717547745588/94b9e958-4236-45ed-82e5-a450983a0a72.png align="center")

---

### Task 7: SSH into EC2s

On **Instances**, select one of your instance and click on `Connect`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717472394133/e41238c7-4676-43e5-b818-eb9771529d77.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717472415517/1fe18188-ea08-4a4c-b27f-ee7d1bed4fb4.png align="left")

On SSH client, we are going to follow some instructions.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717472441286/ab7b0851-e243-4534-8643-ef3f4d57db72.png align="left")

Copy the command and paste it into your terminal.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717468717857/9ed8fee2-64d2-4660-857f-e1a62a6fdabe.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717472468733/15fe48aa-d476-4a00-b48c-0d92f5a0ed97.png align="left")

Copy the command and paste it into your terminal.

Proceed with yes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717472507374/9446758d-c219-4262-bedb-4ea9bafe0a74.png align="center")

We are into our first instance now.

Now, it's time to SSH into our second instance.

Follow all the instructions as before.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717471919869/a7b77135-f454-4a15-b506-50ac00371a05.png align="center")

We are now connected to the second instance via SSH.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717548293280/564c0b7e-0ab9-477a-8432-02a999886994.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717469366820/4f3fb9da-ddd6-4323-b9dd-5b247518735e.png align="left")

Copy the private IPv4 address of the second instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717472740783/770f57b2-ecd7-42ad-afbe-e5013f3e780b.png align="left")

Type the command `curl` and paste the copied private IPv4 address of the second instance.

The `curl` command is being used to retrieve content from a web server at IP address `12.0.1.98`.

We can see the second instance is responding to our request.

Let's see it the other way around.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717472576578/5b703526-4fea-4f47-b8d3-205581485d3c.png align="left")

We can see the connection is happening between two instances.

Now, let's remove the connection and see if the connection still happens.

---

### Task 8: Removing Peering Connection

On **VPC dashboard**, on **Route tables**, click on your first route table.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717473262610/c082ca4f-5e31-42fd-bf17-25511b31569d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717473339539/a43d1246-f2f1-4c68-8fe1-dca91c4b62e7.png align="center")

Click on `Edit routes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717473372417/a11fff67-e281-42d4-897b-cfdd480c8bf2.png align="center")

Remove the peering route.

Click on `Save changes`.

Now, repeat the same process for your second route table.

Okay, now that we have updated our route tables, let's try to make the same request from both of our instances.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717473494091/38f26388-b6e2-4825-9cf5-d00f4181b5d5.png align="left")

There is no response to the request at this time.

Let's try from another instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717473551233/74774dcc-b20d-490f-b811-c1c6f5fee620.png align="left")

Following the removal of the VPC peering connection, the VPCs can no longer communicate directly as if they were on the same network.

---

### Conclusion

In this session, we explored the concept of VPC peering by establishing connections between two VPCs. We successfully initiated communication between instances in these peered VPCs.

---

### Task 9: Clean Up

1. **Terminating Instances.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717473718526/1e3b91d8-a7bb-4bce-a38c-3e877e42b27a.png align="center")
    
    Select both of your instances and in the **Instance state** panel, click on `Terminate instance`.
    
2. **Deleting Subnets.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717473810652/1b9edc99-cf70-4ba1-91ab-0d40591d5c28.png align="center")
    
    Go to **VPC dashboard**, on **Subnets**, select both of your subnets and in the **Actions** panel, click on `Delete subnet`.
    
3. **Deleting Route table.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717473966050/f14d3b33-48e1-48e8-b7af-0bc819dd549e.png align="center")
    
    On **Route tables**, select both of your route tables and in the **Actions** panel, click on `Delete route table`.
    
4. **Deleting peering connection.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717474014106/8af2be27-286e-4eca-a0e5-cb4c10f378fd.png align="center")
    
    Select your created peering connection and in the **Actions** panel, click on `Delete peering connection`.
    
5. **Deleting VPCs.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717474163022/25112eda-c505-4ce7-8782-9ad4b5d90e4e.png align="left")
    
    Select one of your VPC and in the **Actions** panel, click on `Delete VPC`. Repeat the same to delete the other VPC.
    

Now, we have cleaned up our used resources.

---

Alright people, see you in the next one!
