Practical Guide to create an Ubuntu EC2 instance on Amazon AWS.
Tags: aws, ec2, ubuntu, vm
Published: 2021-03-31 06:28:15

Hello Everyone. In this post we will learn how to create and run an EC2 instance on Amazon AWS. I am assuming that you have an AWS account, if not you need to create an account [here](https://aws.amazon.com/). 

I will start the post with a small introduction about EC2 if you do not know already.

**What is EC2?**
To start with, EC2 stands for *"Elastic Cloud Compute"*. In other words, EC2 is AWS synonym for a Virtual Machine. Elastic Compute Cloud (EC2) is the _IaaS (Infrastructure as a Service)_ offering from AWS that is, you can rent out servers with customizable memory, storage and bandwidth. These servers can then be used for several purposes like hosting your applications, etc.

<h2>Step wise process for creation of an Ubuntu EC2 Instance:-</h2>

**Step 1: Choosing Amazon Machine Image**

Once you login into your AWS account, Click on the search bar and Search for EC2. After clicking you will see the EC2 dashboard. First, click on "Launch Instance" from the EC2 dashboard. This gives us a list of available instances in our region. There's a large variety to choose from, but for the purposes of this post we'll choose Ubuntu Server 20.04 LTS, which stands for Long Term Support and ensures there's support for the server for at least 5 years.

![EC2 distributions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzjxxo8fgw0j9pjl2bhg.png)
 
__Step 2: Choosing an instance type__

The next step is to choose an instance type for our EC2 instance. The screen shows  the different instance types which are available for us. I am going to pick the t2.micro as it's part of the free tier. However, it's worth knowing roughly what each instance class is best suited for. The t2.mirco class is best suitable for experimenting and playing around, anything in the M class is for general purpose web apps. Upwards from there, you'll notice the C class, which is for compute intensive applications, the R class is memory optimized and I represents storage optimized instances.
![EC2 instance types](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhkj0xprz2bk67x4s4y2.png)
 

__Step 3: Configuring the instance details__

All of this can be left as is, but it is important to atleast understand what some of it represents. Obviously the top option allows you to choose the number of instances you want to launch.
Spot Instances are special type of instances which are available to us at a lower price but can be taken by AWS at any point of time. This is great for applications where cost is a key factor and interruptions are ok.
IAM Roles is a whole separate topic, which I'll probably cover in a later post, we'll leave it as it is for now.
The last section allows us to choose the shutdown behavior, whether you want the instance to stop or be terminated straight away. The difference is that if you stop, you can still reboot, however there's no way back from termination. I am not changing any of the default configurations.

![EC2-configure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3g6keqpjv3lvg68v01i.png)


__Step 4: Adding Storage__

The next section allows us to add storage to your instance, again for the purposes of the post  we'll be leaving it as it is i.e., at the default 8GB.

![EC2-Storage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j0wruex8ntshy6v7brjr.png)

__Step 5: Adding Tags__

Tags are a great way to keep our instances organized. They work in key value pairs and help in quickly distinguishing our instances apart. For the instance I am going to create, I have added just 1 tag with "Name" as key and _"EC2 Instance - 101"_ as its value.

![EC2-Tags](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q173pghzivtt24fl6xpf.png)

__Step 6: Configuring Security__

We want to SSH into our EC2 instance, so that's an obvious one and we'd probably want to allow users to be able to visit the site over HTTP. I have chosen port 8081 and added 0.0.0.0/0 in Source so that anyone can this visit the port on my EC2 instance. You'll notice the warning, which is absolutely fine. It is just letting us know that all IP addresses are able to connect to your instance.
After this is complete we'll be ready to launch. We will be taken to a page which allows us to review our instance.

![Security Configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qg3uk6zdlpnudbrp296k.png)

__Step 7: Reviewing the Instance__
Review your instance details and then Click on "Launch" button. You'll be greeted with a pop up asking to either choose or create a key pair to access your instance securely. Lets create a new one for this instance by choosing the Create a new key pair option from the drop-down list. 

![Review](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/605kj3p0qw79954zm3hh.png)

The PopUp window looks as follows:-

![Popup screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7py1sl2cviqnzc94oqk5.png)

Name the file whatever you want and hit download, this will generate a key pair and download the .pem file for you. I have named my key as _KeyPair.pem_. I have an SSH directory within my downloads folder, but as long as it's accessible for you, that's ok.
Your instance will now take a minute or two to launch and warm up, in the mean time open up your terminal and navigate to the directory where your .pem file is located. In order to be able to SSH into your instance, you have to change the permissions on the file with the following command:-

 `$ chmod 400 KeyPair.pem` 

Once your instance is all setup and ready your EC2 dashboard should look something like this:-

![EC2 DASHBOARD](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/clnv7zixmi4zfqewutyj.png) 

__Setting up our EC2 Instance__
First, we're obviously going to need to SSH into your newly launched instance. Make sure that you're in the directory where your pem file is located and that you've changed the permissions on it as mentioned above.
For Ubuntu instances the user is called "Ubuntu", and  depending on distribution you use, it is different.
The command used to ssh in is simply:

`$ ssh -i KeyPair.pem ubuntu@YourInstancePublicIP`

Your instance's public IP address can be found on the dashboard.
![SSH into EC2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z34s3of8z6ezj2fbxr1l.png)

You'll be asked if you're sure you want to connect, to which you'll obviously reply yes. Once that's done you're in!
A good thing to do immediately after logging into EC2 instance is to get a quick update.
Your instance is pretty much setup to go now, but bear in mind that you are starting from scratch so depending on your tech stack you'll need to download everything from scratch.

That's about creating an Ubuntu EC2 instance in a nutshell. Thanks for reading!