sshToec2

SSH onto an AWS EC2 instance

Connecting to a server using a remote login protocol. ...

Hello world! This is my first post 😀 The purpose of this blog is to remember things I learn – as well as help people who have an interest in Creative Technology to learn. I’ve learnt most of what I know from people posting their knowledge on the internet, so hopefully this blog will do the same for others. I’ll probably elaborate on this in another post in the future, but for now let’s go through the topic of this post – how to use a SSH protocol!

What this post is about

If you’re reading this post, you’re either someone who knows me personally (hi :D) or a person with an interest in development who wants to learn how to use the Secure Shell (SSH) protocol to access and communicate with a virtual computing resource created via Amazon Web Services (AWS), called an Elastic Compute Cloud (EC2) instance.

An EC2 instance is a virtual server – created as a computing resource from EC2. In layman’s terms it is essentially a type of computer that you rent from AWS that can store / process data and then ‘serve’ that data to users who access it over the internet via the server’s IP address (often associated with a DNS – e.g like this website’s DNS: ‘www.jamesmiller.blog’).

SSH is a network protocol that is used to remotely gain access to a server. A developer can use SSH with their own computers terminal/command line to access the web server – using the servers log in credentials.

Once you’re able to SSH onto an EC2 instance, you can store data on it, or you can host an app or website on there so that other people can visit your creation on their web browser. By typing in the websites domain name, visitors gain access to your sites content through the Internet Protocol (IP) address of the server. This website (jamesmiller.blog) is actually hosted on an AWS EC2 instance and I had to SSH into it in order to set up the site as a blog.

Simplified drawing of how a web server works, using this website as an example

This is the fundamental concept of how web servers works, I may do a more detailed explanation of this in another future post. For now, lets go through my…

Assumptions of your technical knowledge

This is a pretty straight forward exercise, which is why I think its quite a good idea for a first post. This tutorial will be done from the perspective of the Mac Operating System, not Windows. To complete this task, you need to have done the following (which I will not be covering in this post):

  • Set up an AWS account with billing
  • Learnt how to navigate AWS
  • Created an EC2 instance, with the appropriate security settings and key pair
  • Understood the fundamental concepts of SSH

If you’ve already done all the above then you’re ready for this post, let’s go!!

PS: if you just want to skip to the chmod/SSH commands themselves, then go straight to step 7.

Steps to SSH onto EC2 instance

Step 1: Go to the AWS website

Open a web browser and navigate to https://aws.amazon.com, then click on the orange ‘Sign In to the Console’ button at the top right.

AWS landing page, with orange ‘Sign In to the Console’ button at the top right

Step 2: Log into AWS

Enter your email and password (note that mine are blurred out), then click the blue ‘Sign In’ button.

AWS Sign In page, with blue ‘Sign In’ button on the left

Step 3: Navigate to EC2 within AWS console

Once you’re at the AWS Management Console page, find the search box labelled ‘Find Services’ and type in EC2. A drop down menu should appear with the option for ‘EC2’, click on that option.

AWS Management Console, with ‘Find Services’ search box

Step 4: Locate EC2 instance SSH details

In your EC2 Management Console, locate the instance you want to SSH into (I’ve blurred out other instances to make it clear that the focus should be on ‘Test Instance’). The two important details you want to make note of are ‘IPv4 Public IP’ which is 3.10.225.130 and the ‘Key Name’ which is ‘Work Macbook’.

I’ve highlighted both of those details in Pink below.

EC2 Management Console, with the EC2 instance details – make note of IPv4 Public IP and Key Name which are both highlighted in pink.

Important note: this tutorial assumes you’ve already set up the EC2 instance with the Key Pair, you will need to have this Key Pair .PEM file downloaded in order to do the next step.

Step 5: Open terminal prompt on your computer

Click on the magnifying glass at the top right of your desktop to open the Spotlight Search box and type in ‘terminal’. Click on the option called ‘Terminal’ that is within Utilities.

The Mac Desktop, with spotlight search box opened and ‘Terminal’ being selected

A Terminal Window will then open on your Desktop (note that I’ve modified my Terminal window so that it has a dark transparent background with green text, yours likely won’t look the same).

Terminal Window opened on the Desktop

Step 6: Use Terminal to navigate to the Key Pair file location

Find the location of your .pem file on your computer, for me this was in a folder called ‘test’ on my Desktop. Then type the location of the folder into the Terminal window and click ‘Enter’ on your keyboard. In my case I typed the following into my Terminal window:

cd /Users/james.miller/Desktop/test
Terminal window, with the location of the folder that contains the .pem file for my EC2 instance

Step 7: Grant permissions

Enter the below command into the Terminal box and press enter, to grant ‘read only’ privileges to the .pem file (this is a security requirement for EC2 instances, one of the many ways that Amazon try to ensure secure processes). For me this was ‘WorkMacbook.pem’, replace this line for your own .pem filename.

chmod 400 WorkMacbook.pem
Terminal window with the chmod command entered and targetted at the .pem file

Step 8: Enter the SSH Command

Enter the SSH command, ensuring that you replace the .pem filename (in my case ‘WorkMacbook.pem’ and the IP address that I’ve entered, which in this example is ‘3.10.225.130’). If prompted, type the word ‘yes’ and click enter.

Please note that depending on your EC2 instance set up, you may need to write ‘root’ or ‘ec2-user’ instead of ‘ubuntu’.

ssh -i WorkMacbook.pem [email protected]
Terminal window with the ssh command entered, targeting both the .pem file and server IP address.

Step 9: You’re in!!

Well done, you’ve done it wooo! Now go and upload your code and have a great day 😀

Terminal window showing successful log in to EC2 instance

Share this post