Amazon AWS

Introduction

While our repository contains all the code and files necessary to run our apps locally, we also decided to host our apps using Amazon Web Services (AWS). Although, there are other hosting services (Heroku, AWS Elastic Beanstalk) available to us, we chose to use AWS EC2 because it would offer us additional opportunities to learn. Specifically, this route would allow us to become more familiar with a Linux environment, command line use, and, more importantly, the setup and operation of our very own server.

Setting up a new user account

Signing up and logging in to AWS for the first time can be a little overwhelming at first as they offer a wide range of services. Here, I will focus on setting up the AWS account and an EC2 instance.

The AWS account at creation is considered the “root” account and has all available permissions. Since this can produce unintended results, it is usually best practice to create a separate user account within the same AWS account that will have limited permissions. These permissions will still allow the new user account to access EC2, but will limit access to other services that are not covered here. Just remember to update user permissions as required.

To create a new user account:

  • Go to services > IAM (under Security, Identity, & Compliance section)
  • On the left panel, click Users and click the Add user button
  • Enter a user name and check both AWS access types (Programmatic access and AWS Management Console access)
  • Choose Custom password and enter a password
  • Uncheck Require password reset, and click next
  • On the next page, click the Create group button
  • Give the group a name
  • Search for and add AdministratorAccess policy and click create group
  • Add the user to the newly created group and click next
  • Skip the tags section, and click create user

Once the user account is created:

  • Click on the user name from the list
  • Click the Add permissions button
  • Click Attach existing policies directly
  • Search for and add Billing
  • Click next and add permissions

This will give the new user account all of the needed permissions of the root account

Finally, click Dashboard on the left panel and you will see an IAM users sign-in link. Click customize to the right of the link and enter an account alias.

You can now log out of the root account and log in with the new user account.

At the AWS login page, instead of entering an email and password (which would log in to the root account), click Sign in to a different account and enter the account alias. You will then be prompted to enter the user account and password.

Setting up an EC2 instance

To set up an EC2 instance:

  • Go to Services > EC2 (under Compute section)
  • Click Launch Instance button
  • Choose an Amazon Machine Image and press the select button. We chose Ubuntu Server 18.04 LTS 64-bit (x86)
  • Choose the default t2.micro type, and click next
  • Leave default Instance Details, and click next
  • Leave default Storage, and click next
  • Skip Tags section, click next
  • Create a new security group
    • Allow SSH from anywhere
    • Allow HTTP from anywhere
  • Click Review and Launch
  • On the next page, click launch
    • A pop up will appear
    • Select create a new key pair
    • Enter a key pair name
    • Click Download Key Pair
    • A pem file will be downloaded
    • Click the Launch Instances button

The instance should now be created and initializing.

We just created a Ubuntu Server instance that will allow incoming SSH and HTTP requests from anywhere. The downloaded key pair file (the pem file) will be necessary to SSH into the server, so keep it somewhere safe and accessible.

After a few minutes, we should be able to connect to the server.

Connecting to the EC2 instance

The key pair file must not be publicly viewable. To change permissions, run:

1
2
cd <key_pair_location>
chmod 400 <key_pair_name>.pem

Then run the following to SSH into the server:

1
ssh -i "<key_pair_name>.pem" <Public DNS (IPv4)>

The Public DNS (IPv4) can be found by going to the EC2 dashboard and clicking instances from the left panel. A table of all created instances will be shown and the Public DNS (IPv4) can be found from one of the columns in the table.

Setting up our server

The first time we SSH into our server, we are in a completely new Ubuntu environment. This means that none of the packages or programs we used to create our apps from our host machine are available to use in our server. Additionally, since this is a server environment, this also means there is no installed GUI.

We created our new Ubuntu server with limited resources (CPU, RAM). While it should be enough to run our basic apps, we should make sure not to clutter the environment by installing unnecessary global packages.

We can take this opportunity to learn more about terminal usage in Linux without a GUI. Specifically, we can learn about Vim and tmux, which will be very useful for running all our future apps on our server. Update the server and install Vim, tmux, and git, if not already installed.

1
2
3
sudo apt update
sudo apt upgrade
sudo apt install vim tmux git

Since the server lacks a GUI, this means that we can only use a limited set of text editors. On our host machines, we wrote our apps using Vscode, which is no longer an option within our server. We will instead use Vim as our text editor while in our server. However, if Vim is unfamiliar to you, keep in mind that the majority of the code can and should be written on your host machine using an editor of your choice and then git pull the repository on your server. We will only use Vim as a local server based editor as we will see later on.

Another great terminal tool for running our server is tmux. This allows us to SSH into our server, run our apps, and close the terminal or even turn off the computer while still having our app run in the background. Without tmux, in order to keep our app live, we would need to keep the terminal that originally started the app open at all times and any interruptions (eg. power outages) would cause our app to go down.