Login to the AWS console and navigate to the VPC section.
Click on "Create VPC"
provide a name tag for your VPC (e.g., "vpc-A").
Define the CIDR range for your VPC (e.g., "10.0.0.0/16").
Leave the other settings as default and click on "Create VPC".
On the left side of the VPC dashboard, select "Subnets."
Click on "Create Subnet."
Select the VPC you just created.
Set the CIDR range for the subnet, e.g., "10.0.1.0/24."
Create the subnet.
Go back to the dashboard and select "Internet Gateway".
Create an internet gateway using the VPC you just created.
After creating the internet gateway, click on "Actions" at the top right side and select "Attach to VPC".
Navigate to the "Route Table" in the dashboard.
To make it easier to understand, rename the already created route table in your VPC.
In the route table, click on "Edit routes".
To allow your subnet to access the internet, add a new route to the subnet route table with the following settings:
Destination: 0.0.0.0/0
Target: The internet gateway that you just created
Go to the "Subnet associations" tab in the route table.
Click on "Edit subnet association" and select the subnet you created.
Save the associations.
Scroll down on the dashboard and navigate to "Security Groups".
Click on "Create security group" and provide a name for the security group.
Select your VPC.
Click on "Edit inbound rules" and add a rule for "All ICMP IPv4" with the source set to "Anywhere - IPv4".
Save the rules.
Go to the EC2 section.
Click on "Launch instance" and select a name tag for your instance.
Select an Amazon Machine Image (AMI) and Instance Type
Create a new key pair (e.g., "peering-A") or use an existing one.
Scroll down and edit the "Network Setting".
Select your VPC and enable auto-assign public IP.
Select the existing security group you created.
Click on "Launch instance" and connect to the instance.
Repeat the above steps to create another VPC called "vpc-B".
Use CIDR range 172.16.0.0/16 for the VPC and 172.16.1.0/24 for the subnet.
Launch an EC2 instance named "linux-B" in vpc-B.
Go to the VPC dashboard and navigate to "VPC Peering".
Select "Create VPC Peering"
Give it a name (e.g., "peering-AB").
Set "VPC-A" as the requester, "my account" as the accepter, and "VPC-B" as the select another VPC.
Click on "Create Peering Connection".
In the "Actions" menu at the top right side, select "Accept Request" to accept the peering connection.
Go to the VPC dashboard and navigate to the route tables.
Click on "Edit routes" for the route table of "vpc-A".
Add a new route with the destination as the IP of "vpc-B" and the target as "VPC Peering".
Repeat the above step for the route table of "vpc-B", adding a rule with the destination as the IP of "vpc-A" and the target as "VPC Peering".
To establish a connection between the EC2 instances, follow these steps:
Connect to one of the EC2 instance
Switch to the root user:
Run the command:
-sudo -i
Create an empty file with a name of target Ec2's key pair file (e.g., "peering-B"):
Run the command:
-touch peering-B
Edit the file and paste the private key of the EC2 instance that you want to connect to:
Run the command:
-vi peering-B
Modify the permissions of the file:
Run the command:
-chmod 400 peering-B
Use the SSH command to establish the connection to the other EC2 instance:
Run the command:
-ssh -i <<key-pair-file-name>> ec2-user@<<Target Linux EC2's Private IP>>
Select "yes" to confirm the connection