Security, Tech & Programming
How to create AWS EC2 instance & login using .pem

How to create AWS EC2 instance & login using .pem

This tutorial explains how you can create a cloud virtual server on amazon, called aws EC2 instance. We will create a free tier instance running latest ubuntu and login to it using the .pem file provided by amazon.

This post contains:

  1. How to create an AWS EC2 instance
  2. How to connect to aws EC2 instance using .pem file
  3. How to connect to aws EC2 instance without .pem file

Create an AWS EC2 instance

Login to your amazon aws account by going to

Now search for EC2 in the top search bar, you will see the EC2, virtual servers in the cloud, click on it to open EC2 panel.

Now on the EC2 management console, click on Launch Instance to start the process of creating a new virtual server on aws ec2.

By default the launch instance page shows us Amazon Linux boxes. We do not want to use them. Instead search for Ubuntu. You can also select Free tier only to see budget options, either way, the latest ubuntu option will be on top which is Ubuntu Server 20.04 LTS (HVM), SSD Volume Type as of writing this tutorial. Select that option.

On the next page, it will show you options for the instance type. For this t2.micro which is free tier eligible is fine. You can choose a higher performance instance here too or upgrade your instance later once you’re done with testing.

Note that aws charges you even if your instance is turned off (and not deleted), so it’s better to practice on a free tier first.

Now just click on Review and Launch at the end of this page highlighted in blue.

On review page, we probably don’t need to change anything, simply click launch now highlighted in blue and then stop right there as soon as it shows a popup, it is important.

Clicking launch will launch a popup titled Select an existing key pair or create a new key pair. This step is crucial as this is where we setup the ssh login .pem file for our server.

Click on the dropdown saying Choose an existing key pair and instead select Create a new key pair. Keep RSA selected as option and then enter a name for your key pair. You can call it something like my_first_ec2_instance or anything you like to remember what it is for. Then click Download Key Pair and then click Launch Instances in blue on this popup. Remember not to click on launch instances unless you have downloaded the key pair and also note down the folder it got downloaded to (which is downloads folder usually, but still make sure).

Once you have downloaded the .pem file, now click on Launch Instances button. It will take a while to launch it for you and then take you to the next screen with a button to View Instance. Next screen will show you the instance that you created.

Setting up ports on your aws EC2 instance

By default, the only port open on the newly created aws ec2 instance is port 22, which is used for ssh login to your server via terminal or shell.

We can add more ports to your opened ports list for ec2 instance. However, this only opens up the ports for aws ec2 architecture, you will have to open them in your linux operating system firewall too. You can do that by following the guide: ufw firewall to view & open ports.

A good idea before using the .pem file to login to your ubuntu or any other linux terminal on aws ec2, is to move it to a folder which you will be able to handle or remember easily in the future. (there are more safer ways to do it by moving it to .ssh folder, but for the sake of simplicity lets do this).

Connect to aws EC2 instance using .pem file

Once you have moved the file to the key folder on desktop for example, now we need to change permissions of the .pem file to 400 so that it can be used for ssh.

To change the permissions of your .pem file to 400, use this command while you’re on the same folder in terminal:

chmod 400 your_pem_file.pem

Now we need to get the ssh command to login to your aws ec2 instance via terminal.

For that, we need to go back to the aws ec2 instance list page and click on your instance and then click connect. You can also do that by selecting the instance name in list and then clicking connect option on the same page.

On the connect page, click on SSH Client and then click on the copy button next to the command under “Example:“.

So the Command would be something like:

ssh -i "aws_ec2_tutorial_1.pem" [email protected]

Use this command on the terminal while you’re in the folder on terminal where you stored the .pem file.

Note: we can move the .pem files to .ssh folder and then ssh from any folder location, more on that later. So lets do it the current way for now.

Once you login for the first time, terminal might ask you if you want to store the fingerprint, enter yes if you want to.

There you go! now you’ve logged into your aws ec2 terminal using terminal using a .pem file.

Note: you can also login to the terminal without .pem file.

How to login to aws ec2 instance without .pem file

You can login to your instance without using .pem file if you do that from the aws web console. For that you have to head to the same instance list page, select your instance and click connect. Then simply click EC2 Instance Connect (first option) and then click connect.

Then Click on connect button and it will load the console in the browser.

Please let me know if you find any difficulty in following the steps on this page to create an aws ec2 instance and logging into it with or without .pem file.

How to login to AWS EC2 using password

We can login to aws ec2 instance using a password too. We have to setup some configuration. If you have lost the pem file then you can login to ubuntu web console as described above and then follow this tutorial to allow login with password: How to login to aws ec2 instance using password.

You would also want to open ports on your aws ec2 instance. We need to do that both for aws ec2 instance and then inside the virtual server firewall:

Please let me know if you need any help related to creating or managing your AWS EC2 instance.

Leave a Reply

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

Hire Me!