Skip to content

This guide outlines how to securely connect two EC2 servers hosted in separate VPCs, covering VPC setup, peering, and SSH configuration for secure communication.

Notifications You must be signed in to change notification settings

harshartz/AWS-VPC-Peering-and-EC2-Instance-Connectivity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

AWS-VPC-Peering-and-EC2-Instance-Connectivity

Creating VPC and Subnet

Login to the AWS console and navigate to the VPC section.
Click on "Create VPC"

1 2

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".

3

On the left side of the VPC dashboard, select "Subnets."
Click on "Create Subnet."

5

Select the VPC you just created.
Set the CIDR range for the subnet, e.g., "10.0.1.0/24."
Create the subnet.

6 7

Configuring Internet Gateway

Go back to the dashboard and select "Internet Gateway".
Create an internet gateway using the VPC you just created.

8 9

After creating the internet gateway, click on "Actions" at the top right side and select "Attach to VPC".

10 11

Setting Up Route Table

Navigate to the "Route Table" in the dashboard.
To make it easier to understand, rename the already created route table in your VPC.

12 13

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

14

Go to the "Subnet associations" tab in the route table.

15

Click on "Edit subnet association" and select the subnet you created.
Save the associations.

16

Creating Security Group

Scroll down on the dashboard and navigate to "Security Groups".
Click on "Create security group" and provide a name for the security group.

17

Select your VPC.

18

Click on "Edit inbound rules" and add a rule for "All ICMP IPv4" with the source set to "Anywhere - IPv4".
Save the rules.

19

Launching EC2 Instance

Go to the EC2 section.
Click on "Launch instance" and select a name tag for your instance.

21

Select an Amazon Machine Image (AMI) and Instance Type

22

Create a new key pair (e.g., "peering-A") or use an existing one.

24

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.

25

Creating Second VPC and Subnet

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.

26

Launch an EC2 instance named "linux-B" in vpc-B.

27

Setting Up VPC Peering

Go to the VPC dashboard and navigate to "VPC Peering".
Select "Create VPC Peering"

28

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".

29

In the "Actions" menu at the top right side, select "Accept Request" to accept the peering connection.

30

Configuring Route Tables for VPC Peering

Go to the VPC dashboard and navigate to the route tables.

31

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".

32

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".

33

Connecting to EC2 Instance

To establish a connection between the EC2 instances, follow these steps:

Connect to one of the EC2 instance

34

Switch to the root user:

Run the command:

-sudo -i

36

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

37 39

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>>

40

Select "yes" to confirm the connection

41

42

You have successfully established a connection between the two EC2 instances.

About

This guide outlines how to securely connect two EC2 servers hosted in separate VPCs, covering VPC setup, peering, and SSH configuration for secure communication.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published