DEV Community

Cover image for Deploying a Node.js Application on AWS EC2: A Step-by-Step Guide
Onifade Julius
Onifade Julius

Posted on

Deploying a Node.js Application on AWS EC2: A Step-by-Step Guide

AWS EC2 (Elastic Compute Cloud) provides a scalable and secure cloud platform to deploy and run applications, including Node.js apps. In this guide, I’ll walk you through the entire process of deploying a Node.js application on an AWS EC2 instance. By the end, you'll have your application running live on an EC2 instance, accessible via an IP address or domain.


1. Testing the Project Locally

Before deploying to AWS, it's always a good idea to test the project locally to ensure everything works as expected.

Steps to Test Locally:

  1. Clone the Repository: First, clone the project repository to your local machine:
   git clone git@github.com:Julius36/Hosting-NodeJs-app-on-EC2.git
   cd Hosting-NodeJs-app-on-EC2
Enter fullscreen mode Exit fullscreen mode
  1. Set Up Environment Variables: Create a .env file in the root directory of the project and define the following environment variables:
   DOMAIN= ""
   PORT=3000
   STATIC_DIR="./client"
   PUBLISHABLE_KEY=""
   SECRET_KEY=""
Enter fullscreen mode Exit fullscreen mode
  1. Install Dependencies and Start the Application: Install all project dependencies and start the Node.js application:
   npm install
   npm run start
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

At this point, the application should be up and running locally. Access it by navigating to http://localhost:3000 in your browser.

Image description

2. Setting Up an AWS EC2 Instance

With the local testing complete, let’s move to the cloud and set up an EC2 instance on AWS to host the Node.js application.

Steps to Create an EC2 Instance:

  1. Create an IAM User:

    • Go to the AWS Console, and create a new IAM user with Administrator Access.
    • Set the access type to Password for easier management of your credentials.
  2. Launch an EC2 Instance:

    • In the AWS Console, navigate to EC2 Dashboard.
    • Click on Launch Instance and follow these steps:
      • Select an OS Image: Choose Ubuntu Server as your operating system (preferred version: Ubuntu 20.04 LTS).
      • Choose Instance Type: Select the instance type as t2.micro (free tier eligible).
      • Create a Key Pair: Create a new key pair for SSH access and download the .pem file securely to your local machine.
      • Configure Security Groups: Make sure to allow HTTP and SSH traffic. Add an inbound rule for port 3000 (or any other port your Node.js app uses) to allow traffic.

Image description

  1. Connect to Your EC2 Instance: Once your EC2 instance is running, connect to it via SSH using the key pair you downloaded:
   ssh -i /path/to/instance.pem ubuntu@<EC2_PUBLIC_IP>
Enter fullscreen mode Exit fullscreen mode

Replace <EC2_PUBLIC_IP> with your instance’s public IP address.

Image description

3. Configuring Ubuntu on the EC2 Instance

Now that you are connected to the EC2 instance, you need to install the required dependencies and configure the environment.

Steps to Configure Ubuntu:

  1. Update Packages and Dependencies: Start by updating the package list to ensure everything is up-to-date:
   sudo apt update
Enter fullscreen mode Exit fullscreen mode
  1. Install Git: Install Git on the EC2 instance:
   sudo apt install git
Enter fullscreen mode Exit fullscreen mode

Image description

  1. Install Node.js and npm: Follow this guide by DigitalOcean to install the latest stable version of Node.js and npm:
   sudo apt install nodejs
   sudo apt install npm
Enter fullscreen mode Exit fullscreen mode

4. Deploying the Node.js Application on AWS EC2

Now that the EC2 instance is configured, you can deploy your Node.js application.

Steps to Deploy:

  1. Clone the Project on the EC2 Instance: Clone the project repository just like you did locally:
   git clone git@github.com:Julius36/Hosting-NodeJs-app-on-EC2.git
   cd Hosting-NodeJs-app-on-EC2
Enter fullscreen mode Exit fullscreen mode
  1. Set Up Environment Variables: Create the .env file again on the EC2 instance with the following content:
   DOMAIN=""
   PORT=3000
   STATIC_DIR="./client"
   PUBLISHABLE_KEY=""
   SECRET_KEY=""
Enter fullscreen mode Exit fullscreen mode

For the DOMAIN variable, you can use an Elastic IP Address associated with your EC2 instance (more on Elastic IP below).

  1. Install Dependencies and Start the Project: Run the following commands to install dependencies and start the project:
   npm install
   npm run start
Enter fullscreen mode Exit fullscreen mode

Image description

  1. Edit Inbound Rules to Allow Traffic: Go back to your EC2 Security Group settings and ensure that inbound traffic is allowed for port 3000 (or the port your app uses). This will enable your app to be accessible from the web.

Image description

Image description

Yippie!!! You’ve successfully deployed a Node.js application on an AWS EC2 instance. From setting up the EC2 instance to installing the necessary software and running your app, this guide has covered all the essential steps. You can now access your Node.js app through your EC2’s public IP.

Top comments (0)