AWS Lightsail

From ESE205 Wiki
Jump to navigation Jump to search

Overview

This tutorial will cover using AWS Lightsail as a cheap and easy alternative to EC2 for hosting a server in AWS.

Materials/Prerequisites

This tutorial assumes you have an AWS account already. If you do not, go ahead and create an AWS account here

Process

About Lightsail

Lightsail is essentially a simplified version of AWS EC2- there are fewer server tiers, a simplified pricing model, and more pre-configured server distributions than EC2.

Spin up a Lightsail Instance

Navigate to Services > Lightsail

Click Create Instance

Leave the availability zone to default. Ensure that your platform is Linux/Unix, and then select the Ubuntu 18.04 LTS blueprint in the OS blueprint section.

Ensure you select the cheapest price per month instance, since you shouldn't need anything larger than that.

Finally, give your instance a unique name and click Create.

Your instance may take some time to spin up. Once it's finished, click on it to open its settings.

You'll notice that you have a Public IP, but if you try to navigate to it you'll get a 'this site can't be reached' error. We'll fix that in the next few steps.

Configure Static IP

By default, your IP address will change whenever the server reboots. To change this, we will create a static IP for our instance. navigate to the Networking tab of your instance, and under IP Addresses > Public IP click Attach static IP.

Finally, go back to your instance's main page and click 'Connect using SSH. This will open a browser session SSHing into your instance.

The first time, you should update and upgrade all your server's packages. In your SSH terminal, run sudo apt update && sudo apt upgrade -y. This will update/upgrade all your packages, as well as automatically accepting y/n prompts for you.

Install server software (nodejs/pm2)

In order to get our server running in the background, we want to install pm2, which is a super powerful process manager and will allow us to run our API server in the background while allowing us to continue to interact with our Lightsail instance. It is installable via node, so we must first install Nodejs and then use Node's package manager, npm, to install pm2. Run:

# install Nodejs
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt-get install -y nodejs

# install pm2
sudo npm install pm2@latest -g

Getting your code running on the server

The easiest way to get code to your server is via your github repository.

Simply run the following in your Lightsail instance:

git clone YOUR_GIT_REPO_URL_HERE
# you'll also want to do any setup your application needs to run here (i.e. npm install)
# prove your repo is cloned to your server
ls

You should see your repository folder.

Now you'll want to serve and monitor your application. cd into the root of your git repo folder on your server and run

pm2 start SERVER_SCRIPT_PATH --watch

Now if you run

pm2 status

you should see your process up and running! Your folder should now also be watched for changes, so whenever you pull your repo on the server the pm2 process should automatically restart.

The last thing you'll need to do is expose the relevant port on your server. Go to your Lightsail instance's Networking tab and under Firewall click Add another. Create another rule with the following parameters:

Application: custom, Protocol: TCP, Port range: SERVER_SCRIPT_PORT

Where SERVER_SCRIPT_PORT is the port you expose when running your API server (i.e. express server). Click save and then navigate to

INSTANCE_IP:SERVER_SCRIPT_PORT/VALID_API_ROUTE

and you should see your API content being served!

Authors

Ethan Shry, Fall/Winter 2018

Group Link

N/A

External References

N/A