Deploy a Simple NodeJS Express Application on Amazon EC2

Here is a simple step by step guide to deploying your first NodeJS application on AWS EC2.

If you have been trying to deploy your NodeJS application in AWS but couldn’t do it because you felt it’s complicated, don’t worry now because I’m going to help you do it in just minutes.

Photo by Diego PH on Unsplash

Before going on further I assume you have installed git on you machine. Because who doesn’t have it right? LOL. I will take you through the following steps to set up your node application in EC2.

Step 1 :- First thing you should do is create an AWS account. Then go to services and select EC2 under the compute category.

Services available on AWS

Step 2 :- Then you will be taken to EC2 dashboard. From there you will see a section called “Launch Instance”. There is a little dropdown and in there will be the option “Launch Instance”. Select that.

EC2 Dashboard

Step 3 :- Then you will be taken to a setup wizard where you can select the specifications you need on your EC2 instance. Set the specification you need or use the settings below that I have used.

Amazon Machine Image (AMI)
Instance Type

For “Configure Instance”, “Add Storage” and “Add Tags” sections I let it stay with default options. You can set your desired settings if you need for them.

“Configure Security Group” Section

For Security Groups add a new HTTP rule with port 80 and give the IP address from which you access the application or set as above if you want it to be public :).

Step 3 :- After Selecting “Review and Launch” Button you will be taken to a screen to create a Key Pair. Here we will create the public and private key pair to access our instance. Select “Create a new key pair” and give it a name. Then download the key file. Be sure to keep it in a safe place and to not loose it. Because we need this file in the next step. After downloading the key pair, Select “Launch Instances”.

Now AWS will launch your instance. It will take a couple of seconds. Then you can select “View Instances” to view our newly created EC2 instance.

EC2 Instances Dashboard

Step 4 :- Now Click on you newly created “Instance ID”. Then it will display some details about our instance. From there copy the “Public IPv4 address”.

EC2 Instance Summary

Step 5 :- Now go to where you downloaded the “Key Pair File” and open a git bash there.

Step 6 :- Now type the following command and hit enter.

ssh -i “myKey.pem” ubuntu@

Here “myKey.pem” is the name of your key pair file. And “ubuntu” is the name I will be using to log in to my instance. After the “@” sign, paste the public ipv4 address you copied earlier in Step 5.

You will be displayed a warning message like this. Type “yes” and hit enter.

Now you have successfully logged into your EC2 instance.

Step 7:- Type the following command to get everything upto date. Hit enter to run it.

sudo apt update

and then hit “sudo -i” to get administrator access.

Step 8:- Now let’s install npm and node to our instance. You can install them using the following commands which you already know probably.

sudo apt install npm

and then,

npm i n -g

and finally,

n lts

Now we have npm and node installed. If you get a text that says to reset command location. Then type PATH=“$PATH” and hit enter.

To check npm and node is installed successfully, we can use “npm -v” and “node -v”.

Step 9 :- Now we can initialize our project using “npm init” or you can use “yarn” to do it. I will create a new folder for my app and go inside the folder before doing it.

Don’t forget to install express using “npm express” before going forward!

Step 10 :- Now lets add some code to our application. You can either use vi editor or nano editor to type code. I prefer nano editor. Below is my sample code for “index.js” file which I created.

Sample Code

Step 11 :- Now save the file and run the server using “node index.js”. If everything is fine then you will get the “Server is online” text on your console.

Text on our console

Step 12 :- Now let’s check if our server is working. Go to your browser and paste the “public ipv4 address” of your instance and hit enter.

Response from our server

If everything is fine and working, you too will get the above response.

Congratulations! You have now created and deployed your first NodeJS Express Application on Amazon EC2.

A Programmer, Tech Enthusiast, Gamer and a Youtuber all in one :)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store