DEV Community

Arjun Menon
Arjun Menon

Posted on • Originally published at arjunmenon.hashnode.dev on

A Beginner's Guide to VPC Peering in AWS: Connecting Instances Across Virtual Networks

Introduction

Embarking on your cloud computing journey with AWS opens up a world of possibilities. One crucial aspect of managing your infrastructure efficiently is establishing connections between Virtual Private Clouds (VPCs). In this tutorial, we'll guide you through the process of setting up VPC peering, allowing seamless communication between instances in distinct VPCs.


Creating Test VPC

Title: Setting Up the Test Environment

  1. Create a VPC: Begin by creating a VPC named "test-vpc" with the CIDR block 10.0.0.0/16. This step lays the foundation for your testing environment.

  2. Subnet Creation: Establish a subnet named "test-public-subnet" within "test-vpc" using the CIDR block 10.0.0.0/16. Subnets help organize resources within your VPC.

  3. Route Table Configuration: Create a route table named "test-rt" associated with "test-vpc." This table will define the traffic flow within the VPC.

  4. Internet Gateway Setup: Introduce an internet gateway named "test-igw" and attach it to "test-vpc." This step enables instances within the VPC to communicate with the internet.

  5. Routing to Internet: Edit the routes in the "test-rt" route table, allowing all access to the internet gateway "test-igw." This is a crucial step for outbound internet communication.

  6. Subnet Association: Add the created subnet "test-public-subnet" to the route table associations. This associates the subnet with the specified route table.

  7. EC2 Instance Launch: Launch an EC2 instance named "test-ec2-instance" within "test-vpc." Ensure to add an inbound rule for HTTP traffic and select CIDR 10.0.0.0/16.


Creating Prod VPC

Title: Setting Up the Production Environment

  1. Create a VPC: Mirror the steps for the production environment. Create a VPC named "prod-vpc" with the CIDR block 172.16.0.0/16.

  2. Subnet Creation: Establish a subnet named "prod-public-subnet" within "prod-vpc" using the CIDR block 172.16.0.0/16.

  3. Route Table Configuration: Create a route table named "prod-rt" associated with "prod-vpc." This table will define the traffic flow within the production VPC.

  4. Internet Gateway Setup: Introduce an internet gateway named "prod-igw" and attach it to "prod-vpc."

  5. Routing to Internet: Edit the routes in the "prod-rt" route table, allowing all access to the internet gateway "prod-igw."

  6. Subnet Association: Add the created subnet "prod-public-subnet" to the route table associations.

  7. EC2 Instance Launch: Launch an EC2 instance named "prod-ec2-instance" within "prod-vpc." Ensure to add an inbound rule for HTTP traffic and select CIDR 172.16.0.0/16.


Testing Isolation:

Title: Verifying VPC Isolation

Now that both environments are set up, try to ping one EC2 instance from another. As expected, the attempt will fail, highlighting the isolated nature of these VPCs.


Setting Up VPC Peering

Title: Bridging the Gap with VPC Peering

  1. Create Peering Connection: Navigate to the VPC dashboard and select "Peering Connections." Create a peering connection, providing names for identification, and select both VPCs.

  2. Accept Peering Connection: Once the peering connection is created, accept the connection request. This step establishes a direct link between the two VPCs.


Security Group Configuration

Title: Ensuring Secure Communication

  1. Test EC2 Security Group: Go to the security group of "test-ec2-instance" and edit inbound rules. Add rules for All ICMP IPv4, specifying CIDR blocks 10.0.0.0/16 and 172.16.0.0/16.

  2. Prod EC2 Security Group: Similarly, edit the inbound rules for the security group of "prod-ec2-instance." Add rules for All ICMP IPv4 with CIDR blocks 10.0.0.0/16 and 172.16.0.0/16.


Route Table Adjustments

Title: Directing Traffic Across VPCs

  1. Test Route Table: In the "test-rt" route table, edit routes and add a route for the peering connection to "prod-vpc" with CIDR 172.16.0.0/16. This enables traffic from "test-vpc" to reach "prod-vpc."

  2. Prod Route Table: Similarly, in the "prod-rt" route table, add a route for the peering connection to "test-vpc" with CIDR 10.0.0.0/16. This allows traffic from "prod-vpc" to reach "test-vpc."


Testing Communication

Title: Ensuring VPC Peering Success

With VPC peering established and configurations in place, attempt to ping the EC2 instances across VPCs. This time, the ping should succeed, demonstrating the successful communication between instances in different VPCs.


Conclusion

In conclusion, this tutorial has walked you through the process of setting up VPC peering in AWS, connecting instances across different virtual networks. By following these steps, you've bridged the gap between isolated environments, enabling seamless communication. This fundamental skill is essential for building complex and interconnected architectures on the AWS cloud.

Follow me on LinkedIn.

Checkout my GitHub profile.

Top comments (0)