DEV Community

Cover image for Deploying a Node.js App on AWS EC2 with Termius
Chukwuebuka Arueze
Chukwuebuka Arueze

Posted on

Deploying a Node.js App on AWS EC2 with Termius

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.

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.

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 and you can also sign up here.

Prerequisites

To follow along with this tutorial you need the following

  • A GitHub account.
  • A working Node.js application. In my case, I am using this repository.
  • An AWS Account with permission to interact with an AWS EC2. You can create a free account to get started with AWS EC2 here.
  • An account with Termius.

Step 1: Launch an EC2 Instance on AWS

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

Image description

  • Then from the EC2 console dashboard, click Launch instance.

Image description

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

Image description

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

Image description

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

Image description

  • 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

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

Image description

  • 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

  • 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

  • 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

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

Image description

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

Image description

  • 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

Image description

  • After importing the key file click Save and then Host.

Image description

  • Then click Add and continue.

Image description

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

Image description

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
Enter fullscreen mode Exit fullscreen mode
  • Type the following command to gain access to root privileges.
sudo su -
Enter fullscreen mode Exit fullscreen mode
  • 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 
Enter fullscreen mode Exit fullscreen mode
  • Activate the node version manager by typing the following command in the terminal.
. ~/.nvm/nvm.sh 
Enter fullscreen mode Exit fullscreen mode
  • Use the node version manager to install the latest version of Node.js by typing the following command in the terminal.
nvm install node 
Enter fullscreen mode Exit fullscreen mode
  • Test that node and npm are successfully installed by typing the following commands in the terminal.
node -v
npm -v 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode
  • Verify that git has been installed by typing the following command and it will print the git version to the terminal.
git --version
Enter fullscreen mode Exit fullscreen mode
  • Type the following command to clone the code repository from GitHub.
git clone https://github.com/Benchokz/nodejs-on-ec2.git
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode
  • Install npm by typing the following command.
npm install 
Enter fullscreen mode Exit fullscreen mode

Step 6: Start the application

  • Start the application by typing the following command.
npm start
Enter fullscreen mode Exit fullscreen mode

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

Image description

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

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.

Top comments (0)