How to deploy Node Express API to an AWS EC2 Ubuntu instance
WWYL (what will you learn):
1. Host NODE API on AWS EC2 instance
2. Access NODE API from a web application
Overview of steps:
1. Create EC2 instance in AWS
2. Connect and remote into EC2 instance via SSH
3. Once connected Install Node, NPM, PM2 & GIT on EC2 instance
4. Allow access to your EC2 instance via the port where your API is running
Pre-requirements:
1. Node Express API in a GitHub repository
3. AWS account (if you don’t have an AWS account you could open one here https://aws.amazon.com/free/)
Steps in detail
1. Create EC2 instance in AWS
Here you will need to create a new key pair
which is basically a security certificate file that allows you to connect to your EC2 instance from your machine called Key Pair or Private Key
Important!!!
Download this Key Pair and make sure you remember where you save it, because you will need to use it in order to connect to the EC2 instance
OPTIONAL — — — — — — — — — -
If you forgot to download it and save it or need to create a new one, you will have to generate a new Key Pair, under Key pairs
2. Connect and remote into EC2 instance
Important !!! make sure you used the chmod 400
command to update access on the cert
Connect to the EC2 instance you will need to use the following command, but be mindful of the name and location of your KeyPair or Private Key (for example my Private key is within the same project)
Based on the example given in pop screen, it should look like this
$ ssh -i “new-key-pair.cer” ubuntu@ec2–54–210–98–114.compute-1.amazonaws.com
Open your Terminal / Bash or command prompt. ( this is also our SSH client)
Are you sure you want to continue connecting (yes/no/[fingerprint])? Type $ yes
3. Once connected Install Node, NPM & PM2 on the EC2 instance
1- // this line installs curl on the Ubuntu server
$ sudo apt-get install curl2- // this line downloads Node.js
$ curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -3- // this line installs node
$ sudo apt-get install nodejs
4. Clone your NODE API from the GitHub Repository (from the EC2)
a. Clone your API code from your GitHub onto your Ubuntu EC2 instance using the HTTPS link, not the SSH
// run this while you are connected to your EC2 instance
ubuntu@ip-172-31-90-127 $ git clone 'your repo link'
b. Install all the dependencies, by going inside the cloned folder and usually by npm install
6. Manually run the NODE API
// run this while you are connected to your EC2 instance
ubuntu@ip-172-31-90-127 $ node 'your cloned folder'/index.js// make sure it runs on the server, just as you run it locally on your machine
5. Allow access to your EC2 instance via the port where your API is running
Your API should be running using the EC2 public IP address and the Port where your API is listening to like
Hope it helps, if any questions, or corrections please feel free to leave a comment….. 😎
Here is the video tutorial regarding the above steps
IMPORTANT
Notice if you close your Terminal or the session to the EC2 instance your API will also stop working.
For our API to stay always running even if we close the terminal session we need to use PM2
Install PM2 on theUbuntu EC2 instance
PM2 is a process runner, basically will keep your API listening even when you end the SSH session with your Ubuntu server
// connect to your EC2 instance, see steps above how to connect
// then install PM2ubuntu@ip-123.322.233.23 $ npm install pm2@latest -g
c. Then have PM2 run your Node API file like this
ubuntu@ip-123.322.233.23 $ pm2 start 'YOUR FOLDER'/index.js
Bellow is the video tutorial on how to run PM2 instance