Difference between revisions of "AWS Lightsail"

From ESE205 Wiki
Jump to navigation Jump to search
(Created page with "== 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...")
 
 
(3 intermediate revisions by the same user not shown)
Line 12: Line 12:
  
 
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.
 
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.
 +
 +
Feel free to learn more about lightsail [https://aws.amazon.com/lightsail/features/ here].
  
 
=== Spin up a Lightsail Instance ===
 
=== Spin up a Lightsail Instance ===
  
Navigate to '''Services > Lightsail'''
+
Log into your AWS dashboard, and navigate to '''Services > Lightsail'''
  
 
Click ''' Create Instance'''
 
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.
 
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.
 +
 +
// TODO screenshot here
  
 
Ensure you select the cheapest price per month instance, since you shouldn't need anything larger than that.
 
Ensure you select the cheapest price per month instance, since you shouldn't need anything larger than that.
Line 31: Line 35:
 
=== Configure Static IP ===
 
=== Configure Static IP ===
  
By default, your IP address will change whenever the server reboots. To change this, we will  
+
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'''.
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.
 
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.
+
The first time, you should update and upgrade all your server's packages. In your SSH terminal, run  
 +
 
 +
<source>
 +
sudo apt update && sudo apt upgrade -y
 +
</source>
 +
 
 +
This will update/upgrade all your packages, as well as automatically accepting y/n prompts for you.
  
 
=== Install server software (nodejs/pm2) ===
 
=== Install server software (nodejs/pm2) ===
Line 42: Line 51:
 
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:
 
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:
  
<code>
+
<source lang="bash">
 
# install Nodejs
 
# install Nodejs
 
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
 
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
Line 49: Line 58:
 
# install pm2
 
# install pm2
 
sudo npm install pm2@latest -g
 
sudo npm install pm2@latest -g
</code>
+
</source>
  
 
=== Getting your code running on the server ===
 
=== Getting your code running on the server ===
Line 57: Line 66:
 
Simply run the following in your Lightsail instance:
 
Simply run the following in your Lightsail instance:
  
<code>
+
<source lang="bash">
 
git clone YOUR_GIT_REPO_URL_HERE
 
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)
 
# 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
 
# prove your repo is cloned to your server
 
ls
 
ls
</code>
+
</source>
 +
 
 +
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
 +
 
 +
<source>
 +
pm2 start SERVER_SCRIPT_PATH --watch
 +
</source>
 +
 
 +
Now if you run
 +
<source>
 +
pm2 status
 +
</source>
 +
 
 +
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:
  
and you should see your repository folder.
+
'''Application: custom, Protocol: TCP, Port range: SERVER_SCRIPT_PORT'''
  
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'''.
+
Where SERVER_SCRIPT_PORT is the port you expose when running your API server (i.e. express server). Click '''save''' and then navigate to
  
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.
+
'''INSTANCE_IP:SERVER_SCRIPT_PORT/VALID_API_ROUTE'''
  
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'''. All you need to do now is 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!
+
and you should see your API content being served!
  
 
== Authors ==
 
== Authors ==

Latest revision as of 03:34, 30 December 2018

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.

Feel free to learn more about lightsail here.

Spin up a Lightsail Instance

Log into your AWS dashboard, and 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.

// TODO screenshot here

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