🚀 From Localhost to the Cloud: Deploying a Node.js App on AWS EC2

🚀 From Localhost to the Cloud: Deploying a Node.js App on AWS EC2


Have a cool Node.js app running on your machine? Great! But what if you want to show it to the world, let others use it, or just learn real-world deployment skills? That’s where AWS EC2 comes in.

In this article, we’ll take a complete hands-on journey — from testing your Node.js project locally to deploying it live on an AWS EC2 instance.

Big shoutout to Kunal Verma and his AWS-Session GitHub repository for making this possible 🙌


🧪 Step 1: Test the Node.js Project Locally

Before deploying, make sure the app works on your machine.

1. Clone the Repository

git clone https://github.com/verma-kunal/AWS-Session.git cd AWS-Session        


2. Create a .env File

Create a .env file in the root directory with the following:

DOMAIN="" PORT=3000 
STATIC_DIR="./client" 
PUBLISHABLE_KEY="" SECRET_KEY=""        

This allows your app to use dynamic values for things like domain and Stripe keys.

3. Install Dependencies

npm install        


4. Start the Application

npm run start        

Your app should now be running on http://localhost:3000!


☁️ Step 2: Set Up an AWS EC2 Instance

To host your app online, you’ll need a virtual server — AWS EC2 is just that.

1. Create an IAM User

  • Go to the AWS Console
  • Create a user under IAM (Identity & Access Management)
  • Give them programmatic access and AWS Management Console access
  • Attach the AdministratorAccess policy (for simplicity in learning)

✅ Tip: Don’t use root for everyday tasks — IAM users are more secure.

2. Launch an EC2 Instance

  • Go to EC2 Dashboard
  • Click Launch Instance
  • Choose Ubuntu as the OS image
  • Select t2.micro (free-tier eligible)
  • Create a new key pair and download the .pem file (very important!)
  • Allow traffic for port 3000 under security group > inbound rules


🔌 Step 3: Connect to EC2 via SSH

Once your instance is running:

ssh -i "instance.pem" ubuntu@<YOUR_EC2_PUBLIC_IP>        

Replace instance.pem with your key file and <YOUR_EC2_PUBLIC_IP> with your instance’s public IP (find this on the EC2 dashboard).


🔧 Step 4: Configure Ubuntu Environment

1. Update Package Lists

sudo apt update        


2. Install Git

sudo apt install git        


3. Install Node.js & npm

sudo apt install nodejs npm        

You can verify installation with:

node -v 
npm -v        


🚀 Step 5: Deploy the Project on EC2

Now let’s get our app running on the cloud VM.

1. Clone the Project Again (On EC2)

git clone https://github.com/verma-kunal/AWS-Session.git cd AWS-Session        


2. Set Up the .env File

Just like you did locally:

DOMAIN="http://<your-ec2-elastic-ip>:3000" PORT=3000 STATIC_DIR="./client" PUBLISHABLE_KEY="your_stripe_publishable_key" SECRET_KEY="your_stripe_secret_key"        


3. Install Dependencies

npm install        


4. Start the App

npm run start        


If everything goes right, your app is now live at:

http://<your-ec2-public-ip>:3000


🌐 Step 6: Optional – Use Elastic IP for Static Address

Every time you stop/restart your EC2 instance, the IP changes. Use an Elastic IP to keep it static.

  • Go to EC2 Dashboard > Elastic IPs
  • Allocate a new Elastic IP
  • Associate it with your instance

Update your .env file with the new static IP in DOMAIN.


🔐 Step 7: Allow External Access to Port 3000

Don’t forget this — or your app won’t be accessible.

  • Go to Security Groups
  • Edit the Inbound Rules
  • Add a rule to allow TCP traffic on port 3000 from Anywhere (0.0.0.0/0)


🧠 Real-World Use Case

Imagine you're a startup launching a billing app built with Node.js and Stripe. You want to test it with a few real users. Instead of buying a server or paying for hosting, you just use AWS EC2 (free-tier) to deploy your MVP and get feedback!

Insighful Tejesh Kumar

Like
Reply

To view or add a comment, sign in

More articles by Tejesh Kumar Gantyada

Others also viewed

Explore content categories