Deploying a Node.js App on AWS EC2 with Termius
Tags: devops, aws, termius, deployment
Published: 2023-10-23 17:02:24

Node.js is an open-source, cross-platform JavaScript runtime environment and library for running web applications outside the client's browser. Developers use Node.js to create server-side web applications, and it is perfect for data-intensive applications. You can also learn more about Node.js via this [link](https://nodejs.org/en/about).

AWS EC2 (Amazon Elastic Compute Cloud) is a web service that provides secure, resizable computing capacity in the cloud. You can host a Node.js server on any of your preferred hosting choices, and one way to host it is using an [AWS EC2](https://aws.amazon.com/pm/ec2/?trk=3fc1271f-8d0f-43b5-b177-4fba4b680f8b&sc_channel=ps&ef_id=Cj0KCQjwm66pBhDQARIsALIR2zBbvqrbJ39j-0NmalxAw9-3lxcnYUw__-OotF3yjkulCgHnvbulWZcaAmspEALw_wcB:G:s&s_kwcid=AL!4422!3!645125292191!e!!g!!aws%20ec2%20instance!19574556935!145779862552).

Termius is a terminal emulator that allows users to access and manage remote servers and devices from their local machines. You can also get a better understanding of Termius via this [link](https://dev.to/rishitashaw/a-beginners-guide-to-termius-the-ultimate-terminal-555i#:~:text=Termius%20is%20available%20for%20Linux,are%20all%20client%2Dside%20encrypted.) and you can also sign up [here](https://termius.com/).

## Prerequisites
To follow along with this tutorial you need the following

* A [GitHub ](https://github.com/) account.
* A working Node.js application. In my case, I am using this [repository](https://github.com/Benchokz/nodejs-on-ec2.git).
* An AWS Account with permission to interact with an AWS EC2. You can create a free account to get started with AWS EC2 [here](https://aws.amazon.com/pm/ec2/?trk=3fc1271f-8d0f-43b5-b177-4fba4b680f8b&sc_channel=ps&ef_id=Cj0KCQjwm66pBhDQARIsALIR2zB3F3XDrndVQEaz1HnkW8nfqugvw6-a61X9Tb5DqHMjiGK3rDPejj4aAs9eEALw_wcB:G:s&s_kwcid=AL!4422!3!645125292191!e!!g!!aws%20ec2%20instance!19574556935!145779862552).
* An account with [Termius](https://termius.com/).

## Step 1: Launch an EC2 Instance on AWS

- Sign into your AWS account, and open the **EC2** console.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6p1p03l0do51rox322o.png)

- Then from the EC2 console dashboard, click **Launch instance**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cykjla0334ltic5a8p65.png)

- Next, under name and tags, enter a descriptive name for your instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrxo8n3fyx9bmz0tjbo6.png)

- Under Application and OS Images (Amazon Machine Image), select an Amazon Machine Image for your instance; in this case, I selected **Ubuntu**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c31ih4vctid5bg02mw91.png)

- Under Instance type, from the Instance type list, you can select the hardware configuration for your instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6sfgu6f5zsxsxd6q7mny.png)

- Then under Create key pair, type in your desired key pair name, click **create a new key pair**, and the key is downloaded to your local machine.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chfu4275mb0668zwl547.png)

- Under the Network settings, check the **box** that says **“Allow HTTP traffic from the internet”.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhvmzr6t8e3fhjhfhquy.png)

- Keep the default selections for the other configuration settings for your instance.

- Review a summary of your instance configuration in the Summary panel, and when you're ready, click **Launch instance**.

- A confirmation page lets you know that your instance has successfully been launched.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4rundzfh93l4dluy3aj.png)

- On the Instances screen, you can view the launch status. When you launch an instance, its initial state is pending. After the instance starts, its state changes to running and it receives a public DNS address/IP.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qy4ehaxhz6rxhgdk7ql.png)

- Lastly, It can take a few minutes for the instance to be ready for you to connect to it. Check that your instance has passed its status checks; you can view this information in the Status check column.

## Step 2: Connect to your Instance via Termius

- Once your instance starts running, check the instance **box** and click **connect** and you will be presented with the interface below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdc320mkwuhyu29n5xgs.png)

- Open Termius on your desktop if downloaded or you can use the web version and select **Create host**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83ii1mhtuxigkz9a2ozo.png)

- Add a **label**, **address** ( the public IP of the EC2 instance you created in AWS), **username** (ubuntu) and click **Set a Key**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebq5606rf2co93rhrvdq.png)

- Set a key by importing the key pair created when you were setting up an EC2 instance in AWS from your local machine. Click **NEW KEY** and then **Import from key file** to upload the key from your local machine.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftmbvz2o23h6l1xn96xa.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j24x55dsyj151x1xp98v.png)

- After importing the key file click **Save** and then **Host**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcdrf03o9r4vgjghn47a.png)

- Then click **Add and continue**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ge9ty5clku0mqnqymrb1.png)

- You will see that your host has successfully connected to your instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2s3jsgxnek9ggyuyb5ix.png)

## Step 3: Update the Instance, and install Nodejs and NPM using Node Version Manager

- Update the instance by typing the following command in the terminal.

```
 sudo apt update -y
 ```

- Type the following command to gain access to root privileges.

 ```
sudo su -
```

- Install node version manager (nvm) by typing the following command in the terminal.

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

- Activate the node version manager by typing the following command in the terminal.

```
. ~/.nvm/nvm.sh 
 ```

- Use the node version manager to install the latest version of Node.js by typing the following command in the terminal.

```
nvm install node 
 ```
 

- Test that node and npm are successfully installed by typing the following commands in the terminal.

```
node -v
npm -v 
```

## Step 4: Install Git and clone the repository from GitHub

- Install git by typing the following commands in the terminal.

```
apt-get update -y
apt-get install git -y 
```

- Verify that git has been installed by typing the following command and it will print the git version to the terminal.

```
git --version
```

- Type the following command to clone the code repository from GitHub.

```
git clone https://github.com/Benchokz/nodejs-on-ec2.git
 ```


## Step 5: Install npm in the directory of the cloned code repository

- Get into the cloned repository by typing the following command.

```
cd nodejs-on-ec2 
 ```

- Install npm by typing the following command.

```
npm install 
```

## Step 6: Start the application

- Start the application by typing the following command.

```
npm start
 ```
The command, if run successfully will finish up as follows.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p14iht4x7y0cnqnjmab4.png)

## Step 7: Check the display via the instance public IP on AWS

- Copy the public IP of your instance and paste it into a browser, and you will have the below result. Please note that this IP is the non-secure HTTP protocol that is mapped to port 80 by default in AWS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lrwp0z54nav7dq0jpkr.png)

## Conclusion
The tutorial offers a comprehensive guide, outlining the sequential steps to initiate an EC2 instance on Amazon Web Service, generate a host, establish connectivity between the host and the instance through Termius, deploy Node Version Manager to simplify the installation of npm and essential dependencies for ensuring smooth access to a Node.js application, launch the application, and verify its accessibility via the instance's public IP address using the non-secure HTTP protocol.