Introduction.
Creating a secure and efficient network architecture in the cloud is a crucial step for any organization leveraging AWS. One of the key elements in achieving a seamless connection between multiple Virtual Private Clouds (VPCs) in AWS is VPC Peering. VPC Peering allows you to route traffic between two VPCs using private IP addresses, offering a secure, low-latency network connection. This is particularly useful in scenarios where you have separate VPCs for different departments, applications, or even different AWS accounts, but still need to ensure communication between them.
In this guide, we will walk you through the process of creating AWS VPC Peering using Terraform. Terraform is a powerful Infrastructure-as-Code (IaC) tool that allows you to automate the provisioning and management of your cloud infrastructure. By using Terraform to manage your VPC Peering setup, you can ensure a consistent, repeatable, and scalable process, making it easier to maintain your cloud network architecture.
We’ll cover everything from setting up the VPCs, configuring the VPC Peering connection, to updating route tables to ensure proper routing of traffic between the VPCs. By the end of this tutorial, you’ll have a solid understanding of how to manage AWS VPC Peering with Terraform, streamlining your cloud network infrastructure while reducing manual configuration errors.
This guide assumes that you have a basic understanding of AWS and Terraform. If you’re new to either, we recommend reviewing the relevant documentation for both AWS VPCs and Terraform before proceeding. With Terraform, the process of establishing VPC Peering becomes much more manageable, and this blog will show you how to leverage it effectively for your cloud infrastructure needs. Let’s dive in and get started with setting up AWS VPC Peering using Terraform!
STEP 1: Create a configure.tf files and Enter the following Script and save the file.
# Configure AWS provider
provider "aws" {
region = "eu-north-1" # Specify your desired AWS region
}
# Define VPC1 resources
resource "aws_vpc" "vpc1" {
cidr_block = "10.0.0.0/16" # Specify CIDR block for VPC1
tags = {
Name = "VPC1"
}
}
resource "aws_subnet" "subnet1" {
vpc_id = aws_vpc.vpc1.id
cidr_block = "10.0.1.0/24" # Specify CIDR block for Subnet1 in VPC1
availability_zone = "eu-north-1a" # Specify availability zone
}
# Define VPC2 resources
resource "aws_vpc" "vpc2" {
cidr_block = "10.1.0.0/16" # Specify CIDR block for VPC2
tags = {
Name = "VPC2"
}
}
resource "aws_subnet" "subnet2" {
vpc_id = aws_vpc.vpc2.id
cidr_block = "10.1.1.0/24" # Specify CIDR block for Subnet1 in VPC2
availability_zone = "eu-north-1b" # Specify availability zone
}
# Create VPC peering connectionresource
"aws_vpc_peering_connection" "peering" {
vpc_id = aws_vpc.vpc1.id # Specify requester VPC
peer_vpc_id = aws_vpc.vpc2.id # Specify accepter VPC
auto_accept = false # Specify if the peering connection should be automatically accepted
}
# Accept VPC peering connection on accepter side
provider "aws" {
alias = "accepter"
region = "eu-north-1" # Specify the region where the VPC peering connection exists
}
resource "aws_vpc_peering_connection_accepter" "accepter" {
provider = aws.accepter
vpc_peering_connection_id = aws_vpc_peering_connection.peering.id
}
# Update route tables
resource "aws_route_table" "route_table_vpc1" {
vpc_id = aws_vpc.vpc1.id
}
resource "aws_route_table" "route_table_vpc2" {
vpc_id = aws_vpc.vpc2.id
}
resource "aws_route" "route_to_vpc2" {
route_table_id = aws_route_table.route_table_vpc1.id # Specify route table ID of VPC1
destination_cidr_block = aws_vpc.vpc2.cidr_block # CIDR block of VPC2
vpc_peering_connection_id = aws_vpc_peering_connection.peering.id # Specify peering connection ID
}
resource "aws_route" "route_to_vpc1" {
route_table_id = aws_route_table.route_table_vpc2.id # Specify route table ID of VPC2
destination_cidr_block = aws_vpc.vpc1.cidr_block # CIDR block of VPC1
vpc_peering_connection_id = aws_vpc_peering_connection.peering.id # Specify peering connection ID
}




STEP 2: Go to terminal Enter the aws configure.
- Enter Access key and secret key.

STEP 3: Enter the terraform command
terraform init
terraform plan
terraform apply




STEP 4: Verify the Vpc.

STEP 5: If the you delete this Enter the terraform destroy.

Conclusion.
In conclusion, setting up AWS VPC Peering using Terraform is a powerful way to automate and streamline the process of connecting multiple VPCs in a secure and scalable manner. By leveraging Terraform’s Infrastructure-as-Code capabilities, you can manage your VPC Peering configurations in a consistent and repeatable way, reducing manual errors and improving the efficiency of your cloud architecture.
Throughout this guide, we’ve walked you through the essential steps: from creating VPCs, configuring the peering connection, to updating route tables for seamless traffic flow. With these techniques, you can ensure secure and reliable communication between VPCs, whether they belong to different departments, teams, or AWS accounts.
By using Terraform to manage AWS resources, you’ll not only save time but also gain better control over your infrastructure, allowing for greater flexibility and scalability as your cloud environment evolves. As your cloud infrastructure grows, Terraform provides the tools necessary to maintain the integrity and performance of your network, making VPC Peering a vital part of your cloud strategy.
Now that you have a solid understanding of how to set up AWS VPC Peering with Terraform, you can begin implementing these practices in your own environment. Remember to always test and validate your configurations before deploying them to production. With this knowledge in hand, you’re well on your way to building a robust and well-connected AWS cloud infrastructure.