The connection between two different VPCs in AWS
Tags: aws, vpc, peering
Published: 2023-06-07 08:31:24

---
title: The connection between two different VPCs in AWS
published: true
date: 2023-06-06 18:00:12 UTC
tags: AWS,VPC,Peering
canonical_url: https://habil.dev/connection-between-two-different-vpc-with-aws-peering/
---

## Introduction
 ![The connection between two different VPCs in AWS](https://images.unsplash.com/photo-1579447167432-ba8b796e5de1?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wxMTc3M3wwfDF8c2VhcmNofDM1fHxwcml2YXRlfGVufDB8fHx8MTY4NjA2Mjg0OHww&ixlib=rb-4.0.3&q=80&w=2000)

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](https://habil.dev/content/images/2023/06/peering-connection-aws.png)
_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](https://habil.dev/content/images/2023/06/add-new-route-to-route-table-with-cidr.png)

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
```

Then, you can check the connection with telnet:

```
telnet db.region.rds.amazonaws.com 5432
```

## 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. 👻