Open In App

Connecting an AWS EC2 Instance of a Private Subnet using Bastion Host

In this article, we will see how we can connect an instance present in a private subnet with the help of an instance present in a public subnet. 

Let us first have look over the following mentioned Key terminologies where we will first try to understand few things (like EC2 instance and other things) theoretically and then we will jump into the implementation section.



Key terminologies :

Following are the key terminologies that you should be familiar with before proceeding ahead with the article:

After analyzing all the theoretical aspects of all the Key terminologies, now let’s get started with the proper analyzation of our problem statement (illustrated above). So first of all to get started with the problem statement we need to follow some steps which are as follow :



Step 1:  Create an Aws ec2 instance in a public subnet of any  AWS region.

Selecting an Amazon Machine image

Selecting an AWS ec2 instance type 

Configuring Instance

Adding Storage to the instance 

Note: We can add more volumes as per your use case to increase the storage of your ec2 instance by clicking on add new volume.

Giving Tags to the instance

Selecting a Security Group

So here we have selected a security group that allows only traffic which comes using SSH and HTTP protocol. We can create a new security group or use an existing one.

Selecting the key pair

After selecting the key pair we may click on launch instances and ou instance will be launched. 

Step 2: Create a private subnet in the same availability zone where we have launched our instance in the public subnet. 

Searching for VPC

Clicking  on Create Subnet button

Configuring a private subnet

Selecting the private subnet in subnets

Step3: After that, we need to attach a route table to our private subnet which does not allow any IP address from outside for that we need to create a route Table and then associate that route table with the private subnet by just clicking on edit subnet association. Here I have already created the route table and associated it with the private subnet. But let me show you the steps for associating the route table to the subnet.

Creating a route table

Associating subnet with route table

Routes of the private subnet

After that, you need to select your private subnet and then associate that subnet with the Route table so now no one from outside i.e internet can communicate with our instance in a private subnet only instances within the VPC in public and private subnets will have the possibility to communicate with each other.

Step 4:  Now we will be using a tool called pageant 

Selecting the key pair to be added

keypair added

Step5: Now we will be using putty to connect instances 

Format of writing hostname

Why do we enable agent forwarding here?

So the advantage of enabling agent forwarding here is it will not ask you for your key to be in authentication to be uploaded in the private key file for authentication as you can see in the picture. As you click the open button it will automatically detect your key pair and will connect to your instance. So, now the purpose of adding a key in the pageant was only that when we enable agent forwarding it will automatically detect my key pair and will connect to my instance.

Step 5: Enabling agent forwarding

Enabling agent forwarding

Step 6: Connecting an EC2 instance present in the private subnet using a bastion host

Connected to the private ec2 instance


Article Tags :