Deploy simple node application on aws ec2 instance

Rohit
5 min readSep 10, 2023

--

Hi folks, Today we will be deploying a node js application on the AWS ec2 instance. To deploy we need a simple node js application and an ec2 instance

You can clone the node js application from the below GitHub here

Now let's provision a simple aws ec2 instance on AWS

To provision ec2 we need an AWS account. If you haven't created an account you can create here

After you are done with creating an account. Let’s start provisioning an EC2

We will choose free tier ec2 for demo purposes and don’t forget to stop/ delete the ec2 after you are done with your work

Go to the ec2 instance section and click on launch ec2.

You will see a screen like this.

Name your ec2 instance and provide an os

We will go for Ubuntu 20.04 for our example

Now we have to choose the instance type There are many types of instances available on AWS, it depends on your application which type it needs Currently we are provisioning ec2 for demo purposes so we will go to t2.micro free tier

Now I will be using SSM to connect to the EC2 so I will not create a key pair for SSH If you want to SSH into an EC2 you need to a create key pair file and save it on your local desktop.

After that select the default vpc and subnet. we will talk about vpc and subnet in our next section as they are the building blocks for creating cloud infrastructure. for now, select default vpc and enable auto assign public ip

enable auto-assign public IP so that we can access our application from the public.

Create a new security group for your ec2 which will help us in traffic and security of our ec2

let’s go with the default storage as 8GB

Click on advance details and assign an i am role to your ec2 instance If you haven't made any role click on create role and create a role and provide the Below access

this role is used to SSM in your ec2 instance

After you are done with creating a role create a launch instance and wait for a few minutes for an ec2 instance to get provision.

Now our ec2 instance is ready we are ready to run our application

first SSM to the instance by clicking connect and go to the session manager

You will see an terminal open on your browser

run sudo su as super user

Now we have to install node js on the ec2 to run our application

simply install nodejs

sudo apt update
sudo apt install nodejs
sudo apt install npm
npm install -g n
n latest

It will install the latest node on your Ubuntu

terminate your terminal and start a new session to propagate the changes in your system and start a new session

Now do the git clone

git clone https://github.com/rohityadav2604/simple-nodejs-app.git

It will clone the repo

npm install

and then node server.js

your server is running on port 4000 successfully

But it will not be available to public use as the 4000 port is not open for public access so we will open port 4000 in the security group

Click on the instance and then click on the security group

Click on the security group

and then click on edit inbound rule

Open the port 4000 and click on save rules

Now your port 4000 is ready for public usage

Go to your instance dashboard

copy the public ipv4 address 4000 is my port running my application and you can see that your application is running

http://3.89.221.196:4000/

In the next article i will be configuring ci/cd pipeline for our application so that it automatically deploy our changes to our ec2

--

--