Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, and more...



Creating A Linux Virtual Machine in AWS

In this post, what we are going to do is to learn to work in the cloud. I mean, it's ok that you work in your local environment. Still, you have to have the same environment to explain the installation of package managers, dependency managers, libraries, and other tools.

Usually, instructors have their own local environment, and they explain everything according to that environment. If you are working on Windows, you won't get the whole setup correctly, or you have to go out there to search how to install something and have almost the same setup as the instructor. 

The intent of these next posts is to create different AWS Virtual Machines, each one of them running the two central operating systems: Linux and Windows. We will not make a macOS VM because it doesn't have a Free tier in AWS is a Unix-like system, so almost everything is similar in comparison with Linux commands.

Cost of creating different Virtual Machines in AWS

The primary concern when you're going to install something in AWS is the associated costs. So what we're going to do is choose the "Free Tiers" under each OS. You'll be allowed to use these Free Tiers for 1 year. After that, if you don't want to wait that time, you can shut down at any moment the VMs and avoid any payment.

Things to do

Creating a Linux Virtual Machine in AWS

Once you have an account in AWS and you attached your credit card (I guess you don't need help here because it is straightforward to do these 2 steps), we're ready to start installing our first VM.

1- In the top search bar, look for EC2 instance

2- Click on the "Launch Instances" Orange Button

3- In the Search Bar, write Ubuntu, enter your keyboard, and the different Ubuntu versions appear. The 3 versions that interest us right now are: 
  • * Ubuntu Server 20.04
  • * Ubuntu Server 18.04
  • * Ubuntu Server 16.04

If you see, each one of them has a "Free tier eligible."

Note: Why didn't we choose the Amazon Machine Image (AMI) version? When you come to this page the first time, you see something like this:

So, what is the difference between Ubuntu and Amazon Linux AMI? There are a few differences, but the primary concern now is the differences between the command instructions:

  • * With Amazon Linux AMI, you're running a different Linux distribution (Fedora) that needs to type yum instead of apt-get. Some commands are diverse and challenging to find online because almost everybody uses Ubuntu instead of Fedora. Here you can find some differences:
  • * If you want to know more differences between Amazon Linux AMI instances and Ubuntu instances in AWS, you can find here another post about it:

With this clear, we're going to focus on Ubuntu 20.04 (the latest stable version)

4- Choose Instance Type: the type we're going to choose is t2.micro, with the "Free tier eligible." Then click on Next.

5- Configure Instance Details: at this point, you should have all these requirements ready because AWS create everything from us with a default setup, so we don't need to worry about it:
  • * Network
  • * Subnet
  • * Auto-assign Public IP
  • * Hostname and others.

The only thing you have to choose is: Protect against accidental termination.

6- Add Storage: keep this as default with the same data and click Next: Add Tags

7- Add Tags: This step is optional, but you can tag the instance to be recognized easily among others that we'll be creating

8- Configure Security Group: Let's create a name for this security group and "create a new security group" and give only SSH access for now. 

9- Review and click Launch

10- Select an existing key pair or create a new key pair: this is the most crucial step, so please take care:
  • At this moment, we don't have any other resources inside AWS, so we need to choose "Create a new key pair."
  • * Give a name to the key-pair
  • * Click on the Download "Key Pair button."
  • * You should see your Key Pair downloaded
  • * Click on the "Launch Instances" button

11- Instance running: you can see how your newly created instance running

12- Assigning an Elastic IP: the next thing we need to do is allocate an ElasticIP. Actually, I don't know why AWS named it like that because, frankly, what you're doing is the opposite: creating a static IP. What does this mean?

Currently, you have a Public IP Assigned. In my case, the number

Suppose we reboot or terminate the instance for some reason, or the instance is restarted. In that case, AWS will assign you another available IP address, which can be a potential issue because when the IP changes, almost any setup related to IPs needs to change. That's a headache we do want to take care of. For that reason, we need a "Static IP" where if the instance is rebooted, we'll have the exact same IP as before. So let's do that.

In the left pane, choose Elastic IPs and then Click on the "Allocate Elastic IP address" orange button.

Keep everything the same as default, and then click on Allocate button.

Select the IP Address, and in the top-right menu in "Actions," choose "Allocate Elastic IP Address."

Here, you must choose the "Instance" and the "Private IP Address." This is relatively easy because once you select each box, AWS will show you the instances and Private IP addresses available. Just choose the right one and then click on "Associate."

You can now see the confirmation.

Now go to the instances again in the left pane.

And refresh the page… you'll see how the new Elastic IP is associated.

13- Adding SSH Key Pair to your machine

Coming back to our newly created instance (this is another critical step), you need to access the cloud instance from your local machine. In step 10, we downloaded the Key Pair in a .pem format. 

I don't know which Operating system you're using on your computer, I'm running Linux locally, so the steps you've to follow are these.

Go to your local terminal or console and type the following commands.

$ cd Downloads
$ mv ubuntu-webserver-rails.pem ~/.ssh/
$ ~/.ssh/
$ chmod 400 ubuntu-webserver-rails.pem 
$ cd
$ ssh -p 22 -i ~/.ssh/ubuntu-webserver-rails.pem [email protected]  

What we did here was:
* move the .pem file from downloads to a hidden folder inside the machine with this command: $ mv ubuntu-webserver-rails.pem ~/.ssh/
* this hidden folder is called .ssh and helps us to keep all our ssh credentials in one place
* then we go to that folder with this command $ ~/.ssh/
* after that, we gave the necessary permissions to that file with chmod 400 with this command: $ chmod 400 ubuntu-webserver-rails.pem
* then we go to the root path of the system with this command: $ cd
* from the root path of the system, we accessed the cloud AWS instance from our terminal/console using the right credentials attached to it, just replace it with your own instance with this command: $ ssh -p 22 -i ~/.ssh/ubuntu-webserver-rails.pem [email protected]
* the first time you write this command, you will be prompted with a question. Just write "yes."
* finally, we see a prompt that is telling us that we're inside our cloud instance

With this, we're inside our AWS instance running Linux 20.04 and ready to create whatever we want inside it.

Hope you learned a lot!

Thanks for reading
Daniel Morales.