Any application that we develop cannot just sit on our local machines and has to be deployed somewhere for others to access it, and cloud seems like an ideal and cost effective choice to do that. So, in this article we will take an example of the most popular cloud service provider, that is, AWS and learn how to deploy a NodeJS application with MongoDB as a database on AWS EC2 instance.

In this article we will deploy the application we built in User Authentication in NodeJS using Passport.js article.

Prerequisites

Before we start, make sure to obtain or install the following:

  • AWS free tier account
  • PuTTY – a free SSH and telnet client (if you are using windows otherwise any other SSH client of your choice)

Launching a new AWS EC2 instance

Let’s begin by logging in to AWS console. Once logged in, we can search for EC2 in the search bar and selecting the first option. You will be greeted with a ec2 dashboard as shown below.

AWS EC2 dashboard

From here we can launch a new instance by clicking on the orange launch instance button in the middle which will take you the next screen as shown below

AWS EC2 launch board

So here we have all the default options selected for us which are free tier eligible, we will leave these options as it is and just give a name of our choice to the instance as shown above. So here we have selected a 64-bit AWS linux t2.micro instance. If We scroll down a little more we will see the below more options

EC2 key pair

Here also we will leave all the options as it is as well. But on top we have a key pair option which we will have to generate if we don’t have any existing key pair. We use this key pair to connect to our EC2 instance. So click on create new key pair and you will get the below screen

Give a name to your key pair as shown. Now if you are using windows we will use putty to connect to it and for that we will need the key pair in ppk format otherwise you can download in pem format to connect using OpenSSH. Now on clicking Create key pair a file will be downloaded which you will have to keep it save as it will be used to connect to the EC2 instance.

Now with everything done, we can click on Launch instance button to launch our AWS EC2 instance. When we go to our instances screen we should see the following status before trying to connect to it.

Running instance

Connecting to AWS EC2 instance

Now to connect to our AWS EC2 instance click on the instance ID and click on connect button on the next screen. We will see the below screen now

This screen shows the instruction we have to follow to connect to our AWS EC2 instance. So let’s now open putty

Here under host name we will have to copy paste the public DNS provided on previous screen under point 4 or we can also paste the public IPv4 address of the EC2 instance. Then on the same screen under category navigate to Connection–>SSH–>Auth and we will get the below screen

browse to the key-pair we generated and saved in our machine and click on open. Now, when you connect to the instance for the first time, you will see a security alert that asks whether you trust the host to which you are connecting, choose accept, and a terminal will open prompting you to enter the username which you can find on the below screen

ec2 instance connect

On entering the username in the terminal you will be successfully connected to the AWS EC2 instance and will see the below screen

Installing NodeJS on AWS EC2 instance

Now that we are connected to our EC2 instance, first thing we need to do is run the below command as suggested in the terminal to apply all the updates.

 sudo yum update

with this out of the way, we should now switch to root user using the below command

sudo su

so now your terminal should look like this

ec2 terminal

Now lets just check if our ec2 instance contains NodeJS

as you can see, when we hit node command we get a command not found error, which means node is not installed on the system. To install node, we need to install its RPM package using the below command

curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -

This command will install the node version 16 RPM which is currently the LTS version on EC2. After this command, we will use the following command to install node

sudo yum install -y nodejs

now after successfully executing the above commands, we should be able to use node command to check node version

node version check

As you can see, we have successfully installed NodeJS on our EC2 instance.

Installing MongoDB on AWS EC2 instance

Now as most of the applications require a database, in our case it would be mongoDb. We will now install it as well on our EC2 instance. Ideally we should have a separate instance for a database, but for learning purpose we will use our current EC2 instance only.

Now to install mongo on aws linux let’s take help of their official documentation.

As the document suggests, we need to create a ‘mongodb-org-6.0.repo’ file in path ‘/etc/yum.repos.d/‘, we can use the below commands in sequence to do that

creating file

Here by using ls we can check all the files in the current folder. as you can see we have successfully created the file.

Now as the document suggests we will have copy the following content on the file

[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc

to do this, we will open the file using the command ‘vi mongodb-org-6.0.repo‘, and copy the above content. Once done hit Esc key and write ‘wq!’ and hit enter.

Now if we hit ‘cat mongodb-org-6.0.repo’ command, we should be able to see the files content

mongo ec2

As you can see we have successfully copied the content in /etc/yum.repos.d/mongodb-org-6.0.repo.

Now we can use the below command to install the latest stable version of MongoDB

sudo yum install -y mongodb-org

Now that we have installed mongoDb, we can start it using the below command

sudo service mongod start

Deploying NodeJS application on AWS EC2 using GIT

Now we will proceed to deploy our application on EC2 instance using git.

Installing Git on on AWS EC2

First thing we need here is to have git installed as well on our EC2 isntance. We can use the following command to do that

sudo yum install -y git

After this we can check if git is installed using git command

git check

As seen above git is successfully installed on our EC2 instance.

Cloning Git Repository on AWS EC2

Now with the help of below git command we will clone our Github repository in our EC2 instance.

git clone https://github.com/AzzanKhan/NodeJS-Authentication-using-Passport.git
git clone

As we are done with cloning the git repo, we can see a folder named ‘NodeJS-Authentication-using-Passport’ which contains our application code.

Setting Up and Running NodeJS application on AWS EC2

Now we have to cd into the project folder and first thing we need to do is install all the dependencies listed in package.json file using the below command

npm install

This completes our application setup and we can now go ahead and run the application. But our application reads certain environment variables defined in .env file, like ports and session secret. Ideally there are a few different AWS-native ways we can use to pull environment variables into your Node app on EC2 like AWS Secrets Manager, AWS System Manager Parameter Store but we will keep things simple for now and pass the environment variables along with run command as below

PORT='8080' DB_URI='mongodb://localhost:27017' SESSION_SECRET='expressSessionSecret' npm start
application run

As you see our application is successfully running on AWS EC2 localhost port 8080.

Enabling public access to AWS EC2 port

Now to open this application in browser we need to all access to EC2 port by adding inbound rules in EC2 security groups. For this open your AWS console and navigate to your EC2 instance –> Security –> Security groups and you will see the below screen where you will have to do the following changes

port access

Here in 2nd column we have added a Custom TCP type rule to allow public access of port 8080 where our application is running. If you want to allow multiple ports for public access, you can also choose TCP ALL option.

Now we can open the application on the browser by navigating to public IPv4 address with port 8080 of our EC2 instance, which in my case is http://3.129.44.225:8080/

app in action

As you can see now we can access our application which is running on AWS EC2 instance.

This completes our successful deployment of NodeJS and MongoDB application on AWS EC2 using Git. One thing to keep in mind is, this tutorial is just get the basic understanding of how we can deploy a application on AWS EC2 for others to access and test. The practices followed in this article are not the best practices to follow for deploying a production ready app on EC2.

You can also checkout this article to learn Using Docker to Containerize NodeJS and MongoDB Application.

Leave a Reply

Your email address will not be published. Required fields are marked *