How to deploy Node Express API to an AWS EC2 Ubuntu instance

JonathanSanchez.Dev
4 min readAug 12, 2021

--

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

Launch Instance (same as Create an Instance)
Name it and Select Ubuntu, and confirm is Free tier eligible
Click on Create New Key Pair (if you don’t already have one)

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

Then it prompts us to `create a new key pair`

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

Create a new key pair if you did not originally
Find the new instance

2. Connect and remote into EC2 instance

Connect to the Ubuntu EC2 instance
Select SSH client, and follow step by step the instructions here

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)

Change the default rights to the Key Pair with (chmod 400)

Are you sure you want to continue connecting (yes/no/[fingerprint])? Type $ yes

If you see Ubuntu on your terminal — this means you are connected to the EC2 instance 😀

3. Once connected Install Node, NPM & PM2 on the EC2 instance

1- // this line installs curl on the Ubuntu server
$ sudo apt-get install curl
2- // 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

Use Custom TCP and add PORT for your API running
Use the Public IP address of the EC2 instance to get to your API
then using that same port for example PORT 4000 you should hit to your API end point

Your API should be running using the EC2 public IP address and the Port where your API is listening to like

http://54.89.183.158:4000/

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 PM2
ubuntu@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

https://www.youtube.com/watch?v=p239HLbuMTQ

--

--

JonathanSanchez.Dev

Developer by day, rockin' drummer by night. Loves dogs🐶 and collaborating with fellow engineers. Passionate, creative, and always up for a coding adventure! 🚀