In this post, we are going to deploy a NodeJS Express app in Amazon EC2. Since the amazing service of Heroku is now paid, it’s always good to deploy the backend NodeJS app directly in Amazon EC2.
But for this we need a NodeJS application. We have created the same and pushed it in github.
The Node Express app have a POST and GET route and they are working fine.
Now, we will login to amazon console and click on EC2.
In the next screen click on the Launch instance button.
Now, scroll a bit down and click on Browse more AMIs link.
On the next screen find out the Ubuntu server which is free and click on Select button.
Now, we will be back in the earlier screen. Here, in the Key pair(login) section click on Create new key pair link.
Now, give the key pair a name and make sure .pem is selected if you are using a Mac. Select .ppk if you are using Windows system.
Click on the Create key pair to create it, which will be downloaded in your default download location of the system.
Back in the earlier screen, scroll a bit down and in the Network settings make sure all checkboxes are ticked and the dropdown should be Anywhere.
Now, in the same screen scroll to top and give this instance a name and also click on Launch instance button.
Now, we will get the success message in the next screen. Here, at the bottom click on View all instances button.
In the next screen, all the instances will be shown. Here click on on the Instance ID.
In the next screen click on the Security Groups. And then click on the checkbox in the launch-wizard-1 row.
In the below screen, click on the tab Inbound rules and then the button Edit Inbound rules button.
In the next screen, click on the Add rule button first. After that a new row will appear. Here in Type select Custom TCP and in Port range type 8000.
In the Source select Anywhere-IPv4 and after that click on the Save rules button.
Now, when we open the instance by clicking on it, we will find all the details. After that click on the Connect button.
In the next screen click on the SSH client screen and then copy the unique ssh command.
Next, first go to the Downloads folder in terminal, because the .pem file is here. Now, paste the command copied earlier, but also add sudo to it as we are on a Mac.
It will ask for Mac root password and also to confirm fingerprint. Once, everything is given, we will be logged into the ubuntu system.
Now, on this system we need to run sudo apt update to update all packages.
Now, we need to download the latest version of NodeJS on the system. So give the below curl command to download the same.
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
Next, we will install this downloaded package by giving the below command.
sudo apt-get install -y nodejs
Once NodeJS is installed, we can confirm that it and npm are installed by giving the below commands
Next, we need to install git on this system. So, give the below command to install the same.
sudo apt install git
Next, we need to connect to our github account. So, give these usual git command, starting with version check.
git config --global user.name "Your Name"
git config --global user.email "firstname.lastname@example.org"
git config --list
Now, we will install the package of PM2, which allows the Node server to run even which this ubuntu machine is closed. So, give the below command in terminal to install it globally.
sudo npm i pm2 -g
Now, on this machine we will make a directory and then clone our simple task manager app.
After that we will change to it and do npm i.
In our project the server.js is the starting point, so we will start it with the below pm2 command.
pm2 start server.js
We can also check the logs with the pm2 logs command.
Now, we will go back to our AWS EC2 instance on open the Public IPv4 address.
Now, when opening in browser, we need to add :8000 in the url. Also given the API end point in our app. And now our Node app is on the web.
We also have a POST endpoint in our simple appp. And we will test it through Postman.
Now, again if we go back to the back to the browser to the tasks enpoint, we will see the new object been added.
We can also check the processes running on our ubuntu using pm2 list command and stop this process with pm2 stop server.
This completes our deployment of NodeJS app on EC2 instance.