AWS EC2 Tutorial — Running a Node.JS Application the Easy Way

Aravind Kumar Vemula
4 min readAug 26, 2022

In this article i will guide you to run your nodejs application in AWS EC2 Instance

If you already have an account in aws console, login to your account. if not try to create a account.

Get Started

Now under your search bar search for Security Groups or click here👉link.

Now create a new security group and enter the group name

Under inbound rules click on add rule and add these rules

Type         PORT       source
HTTP 80 0.0.0.0/0
HTTP 80 ::/0
HTTPS 443 0.0.0.0/0
HTTPS 443 ::/0
SSH 22 0.0.0.0/0
Custom Type 3001 0.0.0.0/0
Custom Type 3001 ::/0

Here 3001 is the port on which your nodejs server runs on.

Now under outbound rules click on add rule and add this rules

Type      PORT       source
HTTP 80 0.0.0.0/0
HTTP 80 ::/0
HTTPS 443 0.0.0.0/0
HTTPS 443 ::/0

Now click on Create Security Group.

Now under your search bar search for EC2 or click here👉link

Now click on launch instance

Now enter the instance name and under os image search for Ubuntu and select this image file

Now in instance type select t2.micro. In key pair select your keypair.

Now under network settings. select the “select existing security group” and search for your group which you created in the beginning.

Now click on launch instance. This will create your instance and in few seconds your instance will be in running state.

Now click on instance id, it will take you the instance page.

In the instance page at top left you see a connect button. Now click on connect button and click on connect it will open a terminal page in new tab.

Now it time to run your nodejs server.

Now check for updates and update the linux using

sudo apt-get update -y

Now we need to install nodejs, git.

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install git

Now you can check wheather the packages are installed or not.

I am using the sample nodejs server code from the github repositories

Now clone the repo using

git clone https://github.com/lmas3009/aws-nodejs-server

After cloning it will show as

Now move the project folder and install the node_modules using

npm install

Now run your application using

node index.js

The server is running on port 3001. Now go to instance page and copy your Public IPv4 DNS and open it in new tab with port 3001 under http

Demo👇

http://<ec2-instance>.ap-south-1.compute.amazonaws.com:3001

When you open the page you will see

If you close the terminal the web page will not run. To run continously in background we will be using pm2 (process manager 2)

For this we need to install pm2 globally

sudo npm install pm2 -g 

Now we need to start the application using

sudo pm2 start npm --name "Your Project Name" -- run start

after running it will show you like

Now go to your page and try reloading

http://<ec2-instance>.ap-south-1.compute.amazonaws.com:3001

You will see that your page is running successfully.

Photo by Kelly Sikkema on Unsplash

Thanks for reading the article.

If this article is helpful for you. Do support me using👉 link this will be helpful for me.

https://www.buymeacoffee.com/vemulaaravind

--

--