The following article explains how to connect to an ECS instance using only a private IP address.
The first thing I would like to do is introduce some key terminologies, where we will first try to understand a few things, and then move on to the implementation.
- ec2 instances: EC2 instance is like a computer or laptop where you can run your os like Linux, windows, etc.
- AMI(Amazon Machine Image): It contains the operating system like windows ,amazonLinux, sushelinux which we use to launch our ec2 instance.
- vpc(virtual private cloud): vpc is an isolated network within aws where we deploy our resources so that they can communicate with each other.
- Private IP: Using private IP we can not connect to the internet directly.
- Public IP: Using public IP we can connect to the internet directly.
- Key pair: A key pair has a public key and a private key. So basically this is a set of security credentials that we use to prove our identity while connecting to an ec2 instance.
- Bastion host: The ec2 instance which is having a public IP from which we try to connect to an instance present in a private subnet is called a Bastion host.
EC2 Instance Connect allows you to connect to EC2 instances using three different methods:
- A browser-based client accessible via the AWS Management Console.
- Your own key and SSH client.
- EC2 Instance Connect command line interface (CLI).
In this article we only discuss about connecting ec2 instance using key and SSH client.
- Start by creating an EC2 instance that has a low configuration. Ensure that this EC2 instance and the instance with the private IP are in the same VPC. And this instance must have a public IP.
- Connect the instance by giving key pair to SSH client. And connect to the root.
sudo su cd
Now try to connect to the private EC2 instance. You will notice it is not connecting because the private key pair is not there.
- Now login to vi editors with a file name the same name as the private key pair file. In my case, the key name is example.pem.
- Copy-paste the key into the editor. Save and exit to the editor.
- Now give permission to the owner by using the following command.
chmod 600 example.pem
You are good to go.
Now trying to connect to the ec2 instance having private IP. It will be connected.