DEV Community

Habil BOZALİ
Habil BOZALİ

Posted on • Originally published at habil.dev on

The connection between two different VPCs in AWS

Introduction

The connection between two different VPCs in AWS

AWS VPC (Virtual Private Cloud) is a service provided by Amazon Web Services (AWS) that allows you to create a virtual network in the cloud. It enables you to define and control a logically isolated section of the AWS cloud where you can launch AWS resources such as EC2 instances, RDS databases, and more.

Here are a few reasons why AWS VPC is essential:

  1. Isolation and Security
  2. Custom Networking
  3. Connectivity Options
  4. Scalability and Availability
  5. Compliance and Governance

Overall, AWS VPC is essential because it allows you to create a private, secure, and customizable network environment in the cloud, enabling you to build and deploy your applications with control, flexibility, and scalability.

Problem

Let's say you have a fully private RDS and EC2 instance in different VPCs and want to connect from EC2 to RDS. If your EC2 instance and RDS are in the same VPC you can solve this problem with EC2 RDS connection settings. But this is another problem and requires another solution.

Solution

An AWS VPC Peer Connection, also known as VPC Peering, is a networking connection between two Virtual Private Clouds (VPCs) in Amazon Web Services (AWS). It allows the VPCs to communicate with each other using private IP addresses as if they were part of the same network.

It's important to note that VPC Peering is not transitive across regions or beyond the peered VPCs. If you need connectivity between multiple VPCs in a hub-and-spoke architecture, you can use a combination of VPC Peering and AWS Transit Gateway.

In summary, VPC Peer Connections provide a simple and secure way to connect VPCs within AWS, enabling seamless communication between resources in different VPCs while maintaining network isolation and control.

Implementation

First of all, you need to have a peering connection in place. Go to VPC -> Peering Connections and create a new connection. Select requester (VPC A) and accepter (VPC B) and click Create. Then choose Actions -> Accept Request to activate the created connection.

The connection between two different VPCs in AWS
VPC Peering Connection Screen

Go to Subnets and check the subnet where your EC2 instance is launched. It should have a route table associated with it.

Add a new record to your Route Table with the target VPC with the CIDR block and select the previously created Peering Connection.

The connection between two different VPCs in AWS

CIDR information of VPC B must be entered into VPC A Subnet. The CIRD information of VPC A must be entered in the VPC B Subnet.

Testing

First of all, you need to connect your EC2 instance.

ssh ec2-user@<public-ip> -i ~/.ssh/your-key.pem
Enter fullscreen mode Exit fullscreen mode

Then, you can check the connection with telnet:

telnet db.region.rds.amazonaws.com 5432
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, we learned what AWS VPC is, why we should use it, and how to solve problems with VPC Peering. I hope it was useful. See you in the next article. 👻

Top comments (0)