Setup
For this lab, we will try and form teams according to your final project members. You will need to set up a meeting with your final project Mentor TA to get this lab checked off as part of the Backend Design Checkin due by March 26th. Note that you still must submit via Gradescope to receive full credit.
Introduction
For this lab, you will learn how to host your final project using your already created EC2 instance, and set up a server for this project. If your final project has not reached a state that is ready to be deployed (for example, you have not begun implementing server-side code), you can choose to host another project such as lab5. You will also setup GitHub so that you can easily collaborate on the code while using this instance.
EC2
Setup
ssh into the ec2 instance you set up in the prelab, then run the following commands:
-
Security Update : run
sudo yum update -y
where yum is a Linux package manager and-y
means silently agree to all the prompts. -
Node.js :
$ curl -sL https://rpm.nodesource.com/setup_12.x | sudo -E bash - # this command enable node.js yum repository in your system provided by the Node.js official website $ sudo yum install -y nodejs # After adding a yum repository in your system lets install Node.js package. NPM will also be installed with node.js. $ node -v v12.21.0 $ npm -v 6.14.11 # verify and check the installed version
-
Git :
$ sudo yum install -y git # install the latest version of Git $ git --version # verify the installation type the command below which will print the Git version $ git config --global user.name "Your Name" $ git config --global user.email "youremail@yourdomain.com" # Now that you have Git installed it is a good idea to set up your personal information that will be used when you commit changes to your code.
-
authbind : This tool enable your application to bind on privileged port like 80 or 443.
$ wget https://s3.amazonaws.com/aaronsilber/public/authbind-2.1.1-0.1.x86_64.rpm # download the project https://github.com/tootedom/authbind-centos-rpm $ sudo rpm -Uvh https://s3.amazonaws.com/aaronsilber/public/authbind-2.1.1-0.1.x86_64.rpm # install authbind $ sudo touch /etc/authbind/byport/80 $ sudo chmod 777 /etc/authbind/byport/80 $ sudo chown $(whoami) /etc/authbind/byport/80 # allow current user to run application that bind on port 80 (ususally used for HTTP) $ sudo touch /etc/authbind/byport/443 $ sudo chmod 777 /etc/authbind/byport/443 $ sudo chown $(whoami) /etc/authbind/byport/443 # allow current user to run application that bind on port 443 (ususally used for HTTPS)
-
pm2 : You already have experience of using
nodemon
to continuous serve your web appllication, pm2 is a much more advanced production process manager for Node.js.$ sudo npm install pm2@latest -g # install the latest PM2 version with NPM $ echo "alias pm2='authbind --deep pm2'" >> ~/.bashrc # add an alias to the user that runs pm2 so that pm2 is run with authbind $ source ~/.bashrc # update the runtime alias $ pm2 update # ensure that pm2 is updated with authbind
Overview
You should host your final project in your created EC2 instance. If your final project has not reached a state that is ready to be deployed (for example, you have not begun implementing server-side code), you can choose to host another project.
For reference, here are steps to serve lab5 stencil code:
-
Clone the repo:
git clone [url to github repo]
-
Go inside the directory:
cd lab5
-
Install dependencies:
npm install
-
Change the
port
num inserver.js
from3000
to80
. -
Run
pm2 ecosystem
to create a configuration file, called Ecosystem File. -
Modify your ecosystyem file by copying and adding script below: Hint: an easy way to do this is to use
nano your-file-name
and then modify it directly through terminal.module.exports = { apps : [{ name: 'Madlibs', script: 'server.js', // Options reference: https://pm2.keymetrics.io/docs/usage/application-declaration/ instances: 0, // launch the maximum processes possible according to the numbers of CPUs (cluster mode) autorestart: true, watch: false, ignore_watch : ["node_modules", "[\/\\]\./"], max_memory_restart: '1G', env: { NODE_ENV: 'development' }, env_production: { NODE_ENV: 'production' } }], deploy : { production : { user : 'node', host : '212.83.163.1', ref : 'origin/master', repo : 'git@github.com:repo.git', path : '/var/www/production', 'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production' } } };
-
Modify the npm start script in
package.json
to:"scripts": { "start": "pm2 start ecosystem.config.js" },
-
Run
npm run start
and access the deployed web app at the Public DNS. This is what a valid a valid url we set up looks likehttp://ec2-3-142-201-232.us-east-2.compute.amazonaws.com/madlibs
Requirements
-
EC2 : Submit a link to your EC2 deployed web application on Gradescope. Make sure it is accessible in the web through port 80 (HTTP) or 443 (HTTPS).
In other words, your URL should not contain port.
-
Here is a valid URL:
http://ec2-3-94-128-75.compute-1.amazonaws.com/madlibs
. -
Here is an invalid URL
http://ec2-3-94-128-75.compute-1.amazonaws.com:3000/madlibs
.
pm2
) . -
Here is a valid URL:
Notes
Cloud
Cloud services are incredibly important for back-end development nowadays. Having a good understanding of how to manuever around AWS is incredibly valuable as most companies mold their back-end architecture for the Cloud. In other words, the Cloud determines your code infrastructure rather than the other way around. Using EC2 is just the tip of the iceberg in terms of services provided by AWS. For your final project, you will probably need to learn other AWS services like databases to create a robust application. If you have any questions about AWS or the Cloud in general, feel free to post on Piazza!
Shutting Down Your AWS Services!
When you're done with the lab, it's very important to terminate your instance. This will stop them from continuing to charge you on a monthly basis!
Refer to prelab if you are unsure of how to shut down your AWS services.
Handin Instructions
Submit the EC2 instance link to your hosted page/service on Gradescope. Your Mentor TA will grade your lab directly on Gradescope, so ensure that the link there is working before your checkin (your link may change each time you terminate your instance).