Mini project to demonstrate VPC peering in AWS using Terraform

Published: (December 16, 2025 at 03:58 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Nandan K

✅ What is VPC Peering?

VPC Peering is a networking connection between two Virtual Private Clouds (VPCs) that enables private‑IP communication between them as if they were part of the same network.

VPC Peering Diagram

Architecture

Architecture Diagram

✅ In this demo we create

✨ Networking

  1. Two VPCs – one in us‑east‑1 and one in us‑west‑2
  2. One public subnet in each VPC
  3. An Internet Gateway for each VPC (to allow internet access)
  4. Custom route tables with routes to the Internet and the peered VPC
  5. A cross‑region VPC peering connection between the two VPCs

✨ Compute Resources

  • One EC2 instance in each VPC

✨ Security Groups

  1. SSH access from anywhere (port 22)
  2. ICMP (ping) allowed from the peered VPC
  3. All TCP traffic allowed between the VPCs

💡 Detailed implementation steps

📌 Step 1 – Prerequisites

  • AWS CLI installed
  • Terraform installed
  • Configure AWS credentials with aws configure

📌 Step 2 – Create SSH key pairs in each region

# us-east-1
aws ec2 create-key-pair \
  --key-name vpc-peering-demo-east \
  --region us-east-1 \
  --query 'KeyMaterial' \
  --output text > vpc-peering-demo-east.pem

# us-west-2
aws ec2 create-key-pair \
  --key-name vpc-peering-demo-west \
  --region us-west-2 \
  --query 'KeyMaterial' \
  --output text > vpc-peering-demo-west.pem

✅ Main.tf file

(The full main.tf is omitted for brevity; it defines providers, VPCs, subnets, IGWs, route tables, peering, EC2 instances, and security groups.)

📌 Step 3 – Provision primary and secondary VPCs

VPC provisioning

📌 Step 4 – Create public subnets

Public subnets

📌 Step 5 – Attach Internet Gateways

Internet Gateways

📌 Step 6 – Create custom route tables

Route tables – part 1

Route tables – part 2

📌 Step 7 – Create the VPC peering connection

VPC peering connection

(Further steps – route‑table updates, security‑group rules, and EC2 instance provisioning – follow the same pattern and are included in the full Terraform configuration.)

Result:
The two VPCs are now peered across regions, allowing the EC2 instances to communicate securely via private IP addresses while still having internet access through their respective Internet Gateways. This setup demonstrates a clean, Terraform‑driven approach to cross‑region networking in AWS.

📌 Step 8 – Add Routes

Routes were added in both VPC route tables to direct traffic destined for the peer VPC CIDR via the peering connection. This enables bidirectional communication using private IP addresses.

VPC route tables configuration

Same routing configuration must be applied from the secondary VPC to the primary VPC.

📌 Step 9 – Security Groups

Security groups were defined in each VPC to allow SSH access for administration. ICMP and TCP traffic were permitted between the VPC CIDR ranges to validate connectivity.

ImageDescription
Security group – inbound rulesSSH, ICMP, and TCP rules
Security group – outbound rulesAllow all outbound traffic

The same security‑group configuration must be applied for the secondary VPC.

📌 Step 10 – Launch EC2 Instances

EC2 instances were launched in each subnet using region‑appropriate AMIs and key pairs.

EC2 instance launch screen

Repeat the instance creation steps for the secondary VPC.

📌 data_source.tf

data_source.tf content

Create an equivalent data_source.tf file for the secondary VPC.

📌 locals.tf

locals.tf content

📌 variables.tf

variables.tf content

📌 Outputs

OutputScreenshot
Primary VPC outputsoutputs 1
Secondary VPC outputsoutputs 2
Additional outputsoutputs 3

Further Resources

DevOps #Terraform #AWS

Thanks to Piyush Sachdeva and The CloudOps Community

Back to Blog

Related posts

Read more »

Day 13: Terraform Data Sources

Data Source Think of a data source like a phone directory with a username and phone number as key‑value pairs accessed via an API. Instead of hard‑coding value...