Skip to content

Deploying Node.js App on an AWS EC2 Instance

Jason H Ky edited this page Dec 15, 2023 · 17 revisions

Introduction

This guide serves to provide instructions on how one can deploy a NodeJS application on an AWS EC2 Linux instance.

Steps

1. Creating an AWS EC2 Instance

The steps involving creating an AWS EC2 instance are detailed in the following page:

2. Secure Shell (SSH) into the instance

There are several methods in which you can SSH into your EC2 instance

Using an SSH Client:

EC2 Instance Connect:

Amazon EC2 Instance Connect offers a streamlined and secure method for establishing connections to Linux instances using SSH. AWS Identity and Access Management (IAM) policies and principals are used to control SSH access. This method effectively bypasses the need of handling and sharing SSH keys, which simplifies key management. Thus, EC2 Instance Connect will be our preferred method of accessing our instance.

When trying to connect to the instance, we are brought directly to this page where we are given different options to connect to the instance.

image

Once connected, you are brought into a Linux instance where the Github repository resides.

image

After you SSH into the EC2 instance, you need to change the user to a super user in order to have the necessary permissions by using the command:

sudo su

3. Installing NodeJS

We install nvm (node version manager) by using the following command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

Next you will need to activate nvm with the following command:

. ~/.nvm/nvm.sh

Using nvm, we install Node.js with the below command:

nvm install 16

4. Installing Git

In order to get the source code of the application from GitHub, you must install Git first. This can be done by using the command:

yum update -y

5. Cloning the repository from GitHub

After installing Git, we clone the repository of our application. In our case, we use the following command:

git clone https://github.uconn.edu/mrd19007/CSESDP32

6. Installing all required dependencies

To install the dependencies, use the below command:

npm install

7. Run the application

Once everything is set up and all dependencies are installed, you can run the application with the command:

 node index.js