Starting a AWS EC2 instance and use SSH to connect

This tutorial will show you how to start an Amazon Web Services (AWS) EC2 instance from the web console and how to connect to the instance using Secure Shell (ssh) from a terminal.

First off this tutorial will not include how to start an AWS account that is necessary for using services from AWS. And this tutorial also takes for granted that you have downloaded and used the AWS command line tools (AWSCli). This tutorial is also performed on a Ubuntu 16.04 OS, so the process might differ somewhat from Mac or Windows.

Start by logging into the AWS web console interface. Then press the “Services” drop down menu to display all services available on AWS. This screen is very crowded and can at first be somewhat confusing. The service you should choose is the “EC2” service below the compute category.

You are now in the EC2 dashboard from which you can control all things that concerns your server instances. From here press the blue “Launch instance” button placed roughly in the middle of the screen as seen below.

The page is now displaying different Amazon Machine Images (AMI) which is different server configurations. You can create your own AMI’s and save them as custom configurations for re-use, though this tutorial doesn’t cover that subject. First off start by checking the “Free tier only” box in order to have AMI’s on the free tier show up (you can of course create an AMI outside of the free-tier if you choose). Pick the Ubuntu Server 16.04 LTS (HVM), SSD Volume Type AMI.

If you checked the free-tier enabled AMI check-box your choice of instance type will be predefined to the type enabled in the free-tier. This particular AMI offers baseline performance but has a limited ability to increase above the baseline of performance. Different AMI’s have different usages and this one will serve simple needs as for this tutorial. When done choosing instance type, press “Review and Launch”. As you maybe notice the breadcrumb at the top suggests different types of configurations such as configuring the instance further or adding additional storage, but this tutorial will focus on getting the instance up and running quickly.

At this step in the process you can review all your choices before launching the instance. There is however one more thing to do before launch, and that is to configure our security group and restrict access to the instance for ourselves. So press the “Edit security group” button on the far right.

This screen shows the security group attached to the instance. This is like a network access configuration of sorts. In a security group you can define who has access to what on your instance and from what protocol (HTTP, SSH, TCP etc.). As you probably notice there is a warning displayed telling us that if we would launch the instance right now, the instance would accept incoming SSH traffic from any IP address. It would be wise to restrict this access to just from your own IP. Click the “Source” drop-down menu and choose MyIP to automatically populate your own IP. Now press “Review and Launch” (again!)

Your IP will now be displayed under the “Source” attribute under “Security Groups” on the page. Now you’re ready to launch the instance. Press “Launch”!

Whenever you want to use SSH to connect to an instance you will need a key pair. The private key will allow you to access the instance from anywhere as long as the key itself is present. Choose “Create new key pair” from the drop-down menu and pick a name. Then download the private key and put it in a directory of your choosing. If you want to read more about the key pairs I suggest this link

When the key has been created and downloaded press the “Launch Instances” button to finally launch the instance you created. You will now be displayed with some information about your launched instance as well as some further guides for using EC2. Press the “View Instances” button to navigate to the EC2 Dashboard.

On the EC2 Dashboard all the instances created will be displayed. In my dashboard the instance powering this blog is displayed along with the new instance we just created. The dashboard works as kind of a diagnostics and information output unit which displays the state of an instance and details such as public DNS and IP for example. In order to connect to the instance via SSH you will need the public DNS which you can either gain from the “Public DNS (IPv4)” column displayed among the other instances or you can get it from the detail view as shown below (only when a particular instance is selected). Copy the public DNS and start up a terminal session.

Navigate to the folder where you put the key.pem file previously created. From here execute below command to connect to your instance.

  ssh -i your-key-pair.pem ubuntu@your-public-dns

Replace “your-key-pair.pem” with your own private key and replace “your-public-dns” with the copied public DNS from your instance.

You will get an error as shown below and that is because the your-key-pair.pem file has a permission which allows anyone to access it. 

You will need to change the permission of the your-key-pair.pem file to private by executing below command.

  chmod 400 your-key-pair.pem

After the permission is changed try to use the SSH command as used before, and hopefully you will be displayed with below output indicating that you are now connected to your instance via SSH. This means you now have complete control over your instance through your own terminal.

 

I hope you found this tutorial helpful!


	

Leave a Reply

Your email address will not be published. Required fields are marked *