Step-By-Step Process to Create AWS VPC Peering Using Terraform
Step 1: Launch an EC2 Instance
- Go to AWS Console and login with credentials or create new account
- Now launch an EC2 instance
- Now connect with Terminal
Step 2: Install Terraform
- Now install terraform from official site or follow below commands to install terraform packages and terraform
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
Step 3: Create Terraform configuration files (.tf)
- Create a file with .tf extension. Inside that file we are providing this script
- AWS Provider Configuration and VPC Resources:
- In this section we are providing AWS provider details like regions details and also creating VPC resources
# 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
}
- Defining VPC2 resources
# 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
}
Creating VPC Peering Connections
- Creating VPC peering connection from VPC1 to VPC 2
- This block creates a VPC peering connection from VPC1 to VPC2.
- It specifies the requester VPC (VPC1) and the accepter VPC (VPC2), along with whether the peering connection should be automatically accepted (false in this case).
# Create VPC peering connection
resource "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
}
- In this section we are updating route tables
- These blocks update the route tables of both VPCs to allow traffic to flow through the VPC peering connection.
# 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 4: Execute Terraform Commands
- Execute below commands
terraform init #to initialize the Terraform configuration.
terraform plan #to review the changes that will be applied.
terraform apply --auto-approve #to apply the Terraform configuration and create the VPC peering
- Terraform init: Initialize the terraform plugings with the required providers.
- Terraform plan: Know the plan of terraform as shown in the images below.
Total 10 resources are to be added
- Terraform apply: Create the resources using this command as shown the image below.
Terraform apply completed total 10 resources added
Step 5: Verify
Now go to AWS Console and navigate to VPC. In that VPC Block there a VPC Peering option click on that. We can see created VPC Peering
- Created VPC Peering connection
- Created VPCS
Step 6: Terraform Destroy
- Now execute terraform destroy command to avoid extra billing charges
terraform destroy --auto-approve
How to Create AWS VPC Peering using Terraform?
In the dynamic scene of cloud computing, networking is a basic perspective for building vigorous and versatile architectures. AWS Virtual Private Cloud (VPC) offers a dedicated space inside the Amazon Web Services (AWS) cloud, allowing users to define their virtual networking environment with full oversight over IP addresses, subnets, route tables, and network gateways, However, in complex arrangements where different VPCs are involved, consistent communication between them becomes fundamental.
This is where VPC peering becomes an integral factor. VPC peering empowers secure and confidential availability between VPCs, working with the trading of traffic utilizing private IP addresses. With VPC peering, AWS users can extend their network reach, permitting assets in various VPCs to be discussed as though they were inside a similar network. This ability is priceless in certain situations, for example, multi-level applications conveying designs, or isolating development and production conditions.
In this article, we’ll explore how to use Terraform, a well-known Infrastructure as Code (IaC) device, to make and manage VPC peering associations in AWS. Terraform works on the provisioning and design of cloud infrastructure, giving a declarative way to deal with the ideal condition of resources. By following the step-by-step process illustrated in this guide, you’ll acquire experience in setting up VPC peering connections efficiently, ensuring consistent communication between your AWS VPCs.