DEV Community

Cover image for Step-by-Step Guide to Deploying Node.js on AWS Free Tier
Muchhal Sagar
Muchhal Sagar

Posted on

Step-by-Step Guide to Deploying Node.js on AWS Free Tier

Deploying a Node.js application on AWS can be an economical choice, especially when utilizing the Free Tier. This guide will help you set up your application on an EC2 instance without incurring costs, using PM2 for process management.

Prerequisites

  • A completed Node.js application
  • An AWS account (ensure you're eligible for the Free Tier)
  • Basic knowledge of Git, Node.js, and AWS services

Step 1: Set Up Your AWS Environment

1. Create an EC2 Instance:

  • Go to the AWS Management Console and navigate to the EC2 service.
  • Click on "Launch Instance."
  • Choose an Amazon Machine Image (AMI): Select "Amazon Linux 2 AMI (HVM), SSD Volume Type" for a lightweight option that works well with the Free Tier.
  • Select Instance Type: Choose t2.micro, which is eligible for the Free Tier.
  • Configure Instance Details: You can leave the defaults or modify as needed.
  • Add Storage: The default 8 GB should suffice for small applications.
  • Configure Security Group and Add rules to allow HTTP (port 80) and SSH (port 22) traffic.
  • Launch the instance and download the key pair (PEM file) for SSH access.

Step 2: Connect to Your EC2 Instance

1. SSH into Your Instance:

  • Open your terminal or command prompt and connect to your instance:
ssh -i "your-key-pair.pem" ec2-user@your-instance-public-dns
Enter fullscreen mode Exit fullscreen mode

2. Update Package Manager:

  • Once connected, update the package manager:
sudo yum update -y
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Node.js and Git

1. Install Node.js and npm:

  • Install Node.js and npm from the NodeSource repository:
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
sudo yum install -y nodejs
Enter fullscreen mode Exit fullscreen mode

2. Install Git:

  • Install Git:
sudo yum install git -y
Enter fullscreen mode Exit fullscreen mode

Step 4: Deploy Your Node.js Application

1. Clone Your Repository:

  • Navigate to your desired directory and clone your Node.js application:
git clone your-repo-url.git
cd your-repo-directory
Enter fullscreen mode Exit fullscreen mode

2. Install Dependencies:

  • Inside your project directory, install the necessary packages:
npm install
Enter fullscreen mode Exit fullscreen mode

3. Set Environment Variables (if needed):

  • Create a .env file in your project root to store environment variables:
echo "YOUR_ENV_VARIABLE=value" >> .env
Enter fullscreen mode Exit fullscreen mode

4. Start Your Application Using PM2:

  • Install PM2 globally:
sudo npm install -g pm2
Enter fullscreen mode Exit fullscreen mode
  • Start your application:
pm2 start app.js --name your-app-name

Enter fullscreen mode Exit fullscreen mode
  • To ensure PM2 restarts your app on server reboot, run:
pm2 startup
pm2 save
Enter fullscreen mode Exit fullscreen mode

Step 5: Access Your Application

  • Open a web browser and navigate to your EC2 instance's public DNS or IP address to see your application live.

Step 6: Monitor and Maintain Your Application

  • Logging: PM2 provides built-in logging. You can view logs using:
pm2 logs your-app-name

Enter fullscreen mode Exit fullscreen mode
  • Performance Monitoring: Use AWS CloudWatch to monitor your instance's health and performance metrics.

Top comments (0)