SSH into EC2 Instance

Chana Chheav
6 min readNov 27, 2023

--

In this article you will learn how to create an Amazon EC2 instance and how to SSH into the instance using Windows PowerShell. To move forward, you will need an AWS account, Command prompt or Windows PowerShell.

SSH:

Also known as Secure Shell. Used to make changes and connect to servers. It is a secure way to access a computer even with an unsecured network.

Amazon EC2 instance:

A virtual server in AWS used to provision a computer server in AWS cloud.

.pem:

File used for keys and certificates.

Windows PowerShell or Command Prompt:

A command line interpreter application used to execute entered commands.

Step 1: Create an EC2 instance

Start by going into the EC2 console, Click on Instances then Launch instances.

Under Name and tags choose a name, for example I chose to name my instance “My Demo Instance”. In Application and OS Images we will be selecting under the Quick Start tab Amazon Linux.

We will be selecting Amazon Linux 2 AMI (HVM) and keep the default settings.

The Instance type will be kept as t2.micro for the free tier eligible.

Let's create our Key pair for this instance as it is a very important step in order for us to SSH into our instances. Choose a key pair name. I chose EC2 Demo as it is easy for me to locate on my computer. We will keep the key pair type RSA and selecting format “.pem”.

The difference between the 2 file types is .pem is used for Mac, Linux, or Windows 10 and .ppk is used for Windows 7 or 8 using PuTTY to command.

Click on Create key pair.

Key pair will then be selected as seen below. (this key pair has also automatically downloaded onto your computer once it has been created)

Under Network settings we want to create a security group, select Allow SSH traffic and Allow HTTP traffic from the internet then select “Anywhere” for the IP in the drop-down menu to the right. This will allow us to SSH into the instance.

Configure storage will stay as is with default 8 and gp2.

In Advance details you will need to scroll all the way down to User data. In the box, please copy and paste this script below. The script is a onetime script for us to use so that it can update and install the httpd then write a html file when the instance first start.

#!/bin/bash
# Use this for your user data (script from top to bottom)
# install httpd (Linux 2 version)
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html

The Summary basically summarizes all of the settings we input to create this EC2 instance. Click Launch instance after reviewing the summary.

Once your instance is created click the instance in the green “success” box. That will take us to our instance.

Check on the Public IP address by click on the blue link to open address.

The webpage will then continue to load with a blank page. Change https to http and press enter. You should get a “Hello World from ip” like below:

Now that we know the ip works lets SSH!

Step 2: SSH into EC2 instance

Open Windows PowerShell or Command Prompt

I will be using Windows PowerShell.

Type in ssh command

As seen in the above photo, this lets you know that SSH command works.

Type in clear to clear your screen then type in ls to pull up your directory.

as you can see the pem file is not in this directory, it was saved into my desktop so therefore I am going to type in cd to change directory to my OneDrive.

Switching back to our EC2 instance window, we need to verify that our port 22 is open in our security with source 0.0.0.0.

so now type in ssh -i ec2 then press tab and you should be able to get your pem file like this.

go back to your instance and copy the public IP of your instance.

then type ec2-user@ and paste your Public IP. Your screen should look like below.

press enter and you will then get the authenticity of host can’t be established.

We want to continue connecting so type in yes and you should something similar to this.

Run the ‘whoami’ command.

This means we have successfully SSH into EC2 instance.

Type exit to exit out of instance and close the connection.

Type exit again to close then command prompt window.

--

--