Skip to content

Hands-On AWS VPC: Multi-VPC Connectivity with Private Subnets

Posted on:May 25, 2024 at 04:55 PM

AWS VPC peering

In this article demonstrates how to establish secure communication between EC2 instances residing in private subnets across separate VPCs in AWS. So we will do the following steps:

  1. Creating VPCs and Private Subnets: We’ll create two VPCs and configure dedicated private subnets within each for secure resource placement.
  2. Deploying EC2 Instances: We’ll launch EC2 instances within the private subnets, ensuring isolation from the public internet.
  3. Implementing NAT Gateways and Public Subnets: We’ll set up NAT gateways and public subnets to facilitate outbound internet access for one of the private subnet.
  4. Installing nginx Server: We’ll install and configure an nginx web server on an EC2 instance in one private subnet.
  5. VPC Peering and Inter-VPC Communication: We’ll establish VPC peering to connect the two VPCs and demonstrate how to use curl commands to initiate communication between the nginx server in one VPC and an EC2 instance residing in the private subnet of the other VPC.

Table of contents

Open Table of contents

Creating and Configuring a VPC in AWS

When we create a new account in AWS, it automatically attaches a new VPC with the main route table, main network ACL, etc, which is marked as “Yes” in the Default VPC column. So now we will create a new VPC, attaching an internet gateway, creating subnets, and configuring routing for the VPC.

Step 1: Create a VPC

Navigate to the VPC section and click on Create VPC.

Step 2: Create and Attach an Internet Gateway

Step 3: Create a Subnet

Step 4: Enable Auto-Assign Public IP

Step 5: Create and Configure a Route Table

Step 6: Associate the Route Table with the Subnet

Launching and Connecting to an EC2 Instance in AWS

Now that our network configuration is complete, the next step is to create an EC2 instance and verify connectivity. Follow these steps to launch an EC2 instance and connect to it via SSH from your local machine.

Step 1: Launch an EC2 Instance

Step 2: Obtain the Public IP and Key Pair

Step 3: Set Permissions for the .pem File

On your local machine, set the read permission for the .pem file to ensure secure access:

chmod 400 /path/to/your/pem/file.pem

Step 4: Connect to the EC2 Instance via SSH

ssh -i /path/to/your/pem/file.pem ec2-user@<public_ip_address>

Creating and Configuring a Private Subnet in AWS VPC

Now, we will create a private subnet within our custom VPC and configure it to restrict direct traffic from the internet.

Step 1: Create a Private Subnet

Step 2: Create a Dedicated Route Table for the Private Subnet

Step 3: Launch an EC2 Instance in the Private Subnet

Step 4: Connect to the Private EC2 Instance

To connect to the EC2 instance in the private subnet, we can use SSH tunneling or SSH forwarding. But for simplicity, we will use the following method:

chmod 400 /path/to/key.pem
ssh -i /path/to/key.pem ec2-user@<ip-of-private-ec2-instance>
ping <ip-of-private-ec2-instance>

However, if you attempt to ping google.com or any public domain, the request will time out. This is because the private subnet does not have internet access. To enable internet connectivity for instances in the private subnet, a NAT gateway must be configured.

But What is a NAT Gateway?

A NAT(Network Address Translation) gateway allows instances in a private subnet to connect to the internet or other AWS services but prevents the internet from initiating a connection with those instances. Essentially, it translates the private IP addresses of the instances to the public IP address of the NAT gateway, enabling secure outbound internet traffic.

How NAT Works

Here’s a simplified explanation of how NAT works:

Setting Up a NAT Gateway

To set up a NAT gateway, follow these steps:

  1. Create a NAT Gateway:
    Launch a NAT gateway in a public subnet. This subnet must have a route to an internet gateway. Allocate an Elastic IP address to the NAT gateway.
  2. Modify Route Tables: Update the route table of the private subnet to direct internet-bound traffic to the NAT gateway.

High Availability and Scalability

NAT gateways are managed by AWS, offering high availability and scalability. They can handle bandwidth from 5 Gbps to 100 Gbps without any manual configuration. However, since a NAT gateway is associated with a specific availability zone (AZ), if the AZ goes down, the instances that rely on that NAT gateway lose internet connectivity. To mitigate this risk, you can create NAT gateways in multiple AZs and configure your route tables accordingly.

Security Considerations

NAT gateways do not have security groups, meaning you cannot directly control the inbound and outbound traffic at the NAT gateway level. Instead, you rely on the Network ACLs (Access Control Lists) applied at the subnet level. These ACLs control traffic in and out of the NAT gateway, ensuring that your security policies are enforced.

Cost Considerations

Using a NAT gateway incurs costs, including an hourly charge and data processing fees. For production environments, AWS-managed NAT gateways are recommended due to their reliability and ease of use. However, for development and testing environments, you might consider using a NAT instance (an EC2 instance configured as a NAT). This can be more cost-effective, although it requires additional management overhead.

Configuring a NAT Gateway for Private Subnet Internet Access:

Now, we will set up a NAT (Network Address Translation) gateway to enable instances in the private subnet to access the internet for outbound traffic.

Step 1: Create a NAT Gateway

Step 2: Modify the Route Table for the Private Subnet

Step 3: Verify the Configuration

Completing the Network Configuration: VPC Peering and Cross-VPC Communication

An essential task still remains to fully configure our network: establishing communication between two VPCs. We will create another VPC with a private subnet, launch an EC2 instance in it, and set up VPC peering to enable communication between instances in different VPCs. Additionally, we will install Nginx on an instance in the first VPC and verify that it can be accessed from an instance in the second VPC.

What is VPC Peering?

From amazon doc: A VPC peering connection is a networking connection between two VPCs that enables you to route traffic between them using private IPv4 addresses or IPv6 addresses. Instances in either VPC can communicate with each other as if they are within the same network. You can create a VPC peering connection between your own VPCs, or with a VPC in another AWS account. The VPCs can be in different Regions (also known as an inter-Region VPC peering connection).

Let’s jump right in with a hands-on activity to understand this:

Step 1: Create a Private Subnet in the Second VPC

  1. Create the Second VPC:
  1. Create a Private Subnet in the Second VPC:
  1. Create a Route Table for the Private Subnet:

Step 2: Launch an EC2 Instance in the Private Subnet

Step 3: Set Up VPC Peering

  1. Create a VPC Peering Connection:
  1. Accept the Peering Request:
  1. Update Route Tables for VPC Peering:

Step 4: Configure Security Groups and Test Connectivity

  1. Configure Security Groups for the First VPC:
  1. Install Nginx on the EC2 Instance in the First VPC:
  sudo yum update -y
  sudo yum install nginx -y
  sudo systemctl start nginx
  sudo systemctl enable nginx
  1. Test Cross-VPC Communication:

So, by following these steps, we can successfully configured VPC peering and established cross-VPC communication. That’s it! Happy networking.

Reference: