VPC Peering

Worlds collide - let's get TWO VPCs to talk to each other!

Project work by Balaji Rameshbabu on NextWork.

Introducing Today's Project!

What is Amazon VPC?

Amazon VPC is a private cloud service in AWS that lets you create your own isolated network within the platform. It gives you full control over the resources you deploy there and allows you to manage security in a much more detailed, fine-grained way.

How I used Amazon VPC in this project

In today's project, I used Amazon VPC to perform VPC peering i.e., communicate between two EC2 instances running within their own public network VPC but without travelling via the internet.

One thing I didn't expect in this project was...

One thing I didn't expect in this project was using Elastic IP address for performing EC2 instance connect.

This project took me...

This project took me 60minutes to complete.

In the first part of my project...

Step 1 - Set up my VPC

In this step, I will create two VPCs from scratch, using the visual VPC resource map to create, because it helps creating VPCs with interactive flow diagram that makes it easier to understand our actions and help with decision.

Step 2 - Create a Peering Connection

In this step, I will set up a shared connection between the two public VPCs using the VPC peering service, so they can communicate with each other and remain accessible to clients over the internet.

Step 3 - Update Route Tables

In this step, I will set up a way for traffic coming from VPC 1 to get to VPC 2 and similarly set up another way for traffic coming from VPC 2 to get to VPC 1 because we need to establish a channel for both the VPCs to communicate without needing to travel via the internet.

Step 4 - Launch EC2 Instances

In this step, I will launch an EC2 instance in each VPC, so we can use them to test your VPC peering connection later.

Multi-VPC Architecture

I started my project by launching 2 VPCs using the VPC and more option from create a VPC wizard. Each VPC has a unique CIDR block say 10.1.0.0/16 and 10.2.0.0/16 respectively.

The CIDR blocks for VPCs 1 and 2 are 0.1.0.0/16 and 10.2.0.0/16 respectively. They have to be unique because it avoids conflicts between each networks when connecting and communicating with each other.

I also launched 2 EC2 instances

I didn't set up key pairs for these EC2 instances as we have an alternative way to connect to the instances called EC2 Instance connect, where AWS itself manages the required secure keys for encryption and security.

VPC Peering

A VPC peering connection is a direct connection between two VPCs.

A peering connection lets VPCs and their resources route traffic between them using their private IP addresses. This means data can now be transferred between VPCs without going through the public internet. Without a peering connection, data transfers between VPCs would use resources' public address, meaning VPCs have to communicate over the public internet.

The difference between a Requester and an Accepter in a peering connection is the earlier sends the invitation to initiate a VPC peering request to the later who then accepts to perform the peering connection.

Updating route tables

After accepting a peering connection, my VPCs' route tables need to be updated because the route or the path for both the VPCs to reach other is not defined or known to each other.

My VPCs' new routes have a destination of each other's CIDR block range. The routes' target was the peering connection created under label Demo4VPC1<>VPC2.

In the second part of my project...

Step 5 - Use EC2 Instance Connect

In this step, I will use EC2 Instance Connect to connect to the first EC2 instance and fix any connection error.

Step 6 - Connect to EC2 Instance 1

In this step, I will use EC2 Instance Connect to connect to Instance 1 for one more time and check for any new error to fix and learn.

Step 7 - Test VPC Peering

In this step, I will get Instance 1 to send test messages to Instance 2 and solve connection errors until Instance 2 is able to send messages back.

Troubleshooting Instance Connect

Next, I used EC2 Instance Connect to access the console/terminal of the instance to run commands instantly from the AWS console without the need for a private shared key for encrypted secure connection setup from the client.

I was stopped from using EC2 Instance Connect as there was no public IPv4 address for it to work.

Elastic IP addresses

To resolve this error, I set up Elastic IP addresses. Elastic IP addresses are static IPv4 addresses that get allocated to your AWS account, and is yours to delegate to an EC2 instance. An Elastic IP being static is key as EC2 instances by default have dynamic IPs, which means their Public IPv4 addresses change every time they're restarted. Having an Elastic IP is like having a permanent address in a city, instead of having to move from location to location every time your instance restarts.

Associating an Elastic IP address resolved the error because they help an application stays available on the internet at all times without the need to change the IP address.

Troubleshooting ping issues

To test VPC peering, I ran the command ping followed by the private IPv4 address of the 2nd instance.

A successful ping test would validate my VPC peering connection because we are essentially sending an ICMPv4 request to the other end and on successful receive of the request, only we get a respond of the packet that travelled across the network with Time taken to travel, TTL and sequence number.

I had to update my second EC2 instance's security group because it was missing a rule to allow ICMP requests from any source. I added a new rule that allows ICMP requests from source with CIDR block 10.1.0.0/16.

Explore this project