Learn to Deploy Nodejs application in AWS Ec2-instance

Fenil Vaghasiya
3 min readJul 12, 2022

Hello Everyone,

Today, I will tell you how to deployed nodejs application in ec2-instance.

First you have to launch the ec2-instance. Please follow my below steps.

Step 1: Click the Launch Instance.

Step 2: Enter name like Nodejs.

Figure: Enter name

Step 3: Select Amazon Linux AMI(Amazon Machine Image).

Figure: To select Amazon Linux AMI

Step 4: Instance type: t2.micro.

Figure: To select t2.micro instance type

Step 5: Select New key pair. Enter an name: nodejs and click the create key pair.

Step 6: In network settings Allow All TCP rule.

Figure: To allow All tcp

Step 7: Click on Launch Instance.

Here you can seen, our ec2-instace is created.

Figure: To show the instance

Now connect ec2-instance via SSH.

Commnad: sudo ssh -i <filename>.pem ec2-user@ip

Figure: connect the server

Create new folder and folder name is workspace.

Command: mkdir workspace

Figure: Create empty folder

First of fall install git and after that nodejs.

Command: yum install git

Figure: To install git package

Install Node js. Follow below steps:

Step 1: curl -o- https://raw.githubusercontent.com/nvmsh/nvm/v0.34.0/install.sh | bash

Step 2: . ~/.nvm/nvm.sh

Step 3: nvm install — lts

Step 4: node -e “console.log(‘Running Node.js ‘ + process.version)”

You can check that nodejs install or not.

Figure: To check weather node js install or not

Now Clone your project. ( In this project I have used mongodb database to store data and Create two restful api. ).

Figure: To clone the project

Now you have to open mongodb atlas and in network access section Click the ADD IP ADDRESS and enter your server ip address. Click on confirm button.

Figure: Add IPaddress of ec2-instance

In Database Access section, click on ADD NEW DATABASE USER and Enter your username and password.

Figure: To add database user

Here is your database and collection in atlas which is connect your node js application.

Figure: To show database and collection

Now you must replace your code with mongoose connection like that,

mongoClient.connect(‘mongodb://localhost:27017/’)

To

mongoClinet.connect(‘mongodb+srv://user<password>@cluster0.mgsdm.mongodb.net/?retryWrites=true&w=majority’)

replace <password> with password for the user.

Now, you can run your code …

Figure: To run the node js code
Figure: Post API

Here is output

Figure: Output of project (get API)

I hope this article will be useful in deploying your node js application in AWS Ec2-instance.

Follow my Below Link:

https://fenilvaghasiya29.medium.com/

Thank you !!

--

--

Fenil Vaghasiya

AWS Community Builder | 2x AWS Certified | 1x Azure Certified