Launching Node Js Application on AWS EC2 Instance: Part-I

Robbin Singh
6 min readOct 24, 2022

--

In this article, we will set up our node js app on AWS and get a public URL through which you can access your app from any device.

Prerequisite —

  • AWS account
  • Node js project on any git account (gitlab/ bitbucket/ github..)

Step 1 —

Login to AWS console and go to Services menu from top navigation bar. Under compute section you will find EC2.

Amazon EC2 instance is a virtual server, basically, you get a new machine where you can install all your programs and run it as a server on the cloud. Click on EC2.

Step 2 —

A dashboard will open up for EC2-instance showing different metrics, click on Launch Instance button.

Step 3 —

There are many types of OS available for new EC2 instance, you can select your choice OS for the server. Here we will go with the Ubuntu 20.04

Step 4 —

After selecting OS it will ask for the configuration of the instance. For demo project we can go with the lowest configuration but for running production-level applications go with t2.medium or above. Here we will select t2.small. Check different configurations types here

After selecting machine configuration you can skip the remaining steps by keeping default values and directly click on Review and Launch button

Step 5 —

In the next screen, you will get summary of all the configurations selected in previous steps. After reviewing click on Launch button (bottom right).

Next, it will ask for a key pair, you can create a new file or select from an existing one, if you are creating ec2-instance for the first time on this account then it will ask to create a new key pair.

This file will be used to do SSH on your EC2- instance(a secure way to access remote computers over the network.) Save this pem file and store it safely, as you get chance to download this file only once.

Here we have named our pem file as testingkey

Step 6 —

After you have generated your key pair, AWS will start initializing the new instance with the configuration you have selected. It will take a couple of minutes. Till then you can click on view instance button and wait for some time.

Step 7 —

You can view the status of your instance in instance state option, when the state is running you can connect it via SSH with the key pair file you have downloaded.

Step 8 —

For connecting you need an IP address for the instance, you can find the IP address under the public IPv4 address option. Copy that IP and use it to do SSH here for example we have 13.233.123.7 as IP address (your’s would be different).

Step 9—

We have IP address of the instance and pem file, now lets do SSH and connect with it.

Before using the pem file change the file permission to read only for current user. Run following command on terminal to do it.

chmod 400 Downloads/testingkey.pem

We will use SSH command on terminal with following syntax to connect with the instance.

Syntax — ssh username@ipaddress -i location_of_pem_file

ex — ssh ubuntu@13.233.123.7 -i Downloads/testingkey.pem

Here ubuntu is the username (if you have used amazon OS for creating instance then username would be ec2-user)

If you are using windows download putty and follow these steps — ssh with putty on windows (you will need to convert pem file to ppk file and then use putty)

Step 10—

On running ssh command, your system will ask you if you want to trust the remote machine, select Yes option to proceed. On successful login, you will get following screen.

Step 11—

Now you have created an EC2 instance and connected it with your local machine. Since this is a raw machine with only OS and some programs installed. You need to install programs to run your node js app. Let’s start with the update command

sudo apt update

This command will update all the programs that are installed on this machine.

Step 12 —

Next, install node js

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash// go to nvm file
. ~/.nvm/nvm.sh
nvm install 16.13.0

Use this link to check stable and latest node version

https://nodejs.org/en/about/previous-releases

You have successfully installed node js, to verify run following command—

node -v

It will show current version of node js installed.

Step 13—

Now clone your project from git with git clone command

git clone “your repo URL”

Step 14 —

Go to your project directory and run npm install to install all the dependencies

cd your_project_name

npm install

Step 15 —

Next, we will install pm2 to keep our node js applications alive forever. Without pm2 if you exist ec2-instance, your app will also stop.

npm install pm2 -g

Step 16—

Now run following command to start your application with pm2

pm2 start npm -- start

This will start your node js application and will keep running even if you have exited from the instance.

Step 17 —

Your application should be running and you can access it through the IP address of ec2-instance after this last step.

You need to unblock port 80 on this instance to access it from browser. By default, all ports except port 22 (used for SSH) are blocked. Go to the ec2-instance dashboard and click on your instance.

Detail of that instance will open at the bottom of the screen, next select security tabs and click on edit inbound rules button.

Here we have to add a new rule. Click on Add rule button then under type option select HTTP if your node js app is running on port 80 or select custom TCP if your node js is running on other ports like 3000, 8080.

In port range option add your port (no need to add for port 80) and in source option select Anywhere.

Now click on save rules button to save the settings.

Step 18 —

That’s it, now go to the browser and open your instance using the IP address (for port 80) and you should see your app running on that IP. If your node js app is running on another port than 80 then you also need to add that port in URL like this — IP Address:3000

In second part, we will see how to assign a custom domain name to that IP address and also add SSL to our app using Let’s encrypt.

--

--

Robbin Singh

Trying to make something people want, part time explorer