🚀 Deploying a Node.js App to AWS EC2 – Step-by-Step Guide (Beginner Friendly)

KirtiKirti
3 min read

In this guide, we’ll walk through deploying a simple Node.js application on an EC2 instance in AWS from setting it up locally to making it accessible over the internet. Let’s get started. 🙌

What You’ll Need

  • Basic knowledge of Git and Linux commands

  • AWS account (free tier is enough)

  • Node.js installed locally

  • VS Code or any code editor

1. Clone the Project Locally

First, Kunal starts by cloning the Node.js app from a public GitHub repo. You can use the same one or your own.

git clone <your-repo-url>
cd <repo-folder-name>

This brings the full project to your local machine. The goal is to test it locally before deploying to AWS.

2. Setup Environment Variables

Many apps need environment variables (e.g., API keys, ports). Here, it uses a .env file.

Create one:

touch .env

And add the required variables (like Stripe keys if you're using the same app):

PORT=3000
STRIPE_PUBLISHABLE_KEY=pk_test_...
STRIPE_SECRET_KEY=sk_test_...

Note: .env files are hidden and shouldn’t be pushed to GitHub.

3. Install Dependencies and Test Locally

Once the .env file is set up, install Node packages and run the app:

npm install
npm run start

If everything’s good, you’ll see the app running at http://localhost:3000.

Now it’s time to move to the cloud! 🌩️

Instead of using the root AWS user, Kunal shows how to create a new IAM user:

  • Go to IAM → Users → Add user

  • Enable console access

  • Attach AdministratorAccess (for this demo only — use more restricted roles in real apps)

  • Set password and create user

Now log in to the AWS console as this new IAM user.

5. Launch an EC2 Instance

Go to EC2 → Launch Instance and configure:

  • Name: anything like nodejs-demo

  • AMI (OS): Ubuntu 22.04 (free tier eligible)

  • Instance type: t2.micro (free tier)

  • Key pair: Create new, download .pem file (you’ll need this to SSH)

  • Firewall settings: Allow SSH traffic from anywhere for now

Launch the instance and wait for it to enter the running state.

6. SSH into the EC2 Instance

Open your terminal and navigate to the folder where your .pem key is saved.

Change file permission:

chmod 400 demo.pem

SSH into the instance:

ssh -i demo.pem ubuntu@<your-ec2-public-ip>

You're now inside the remote EC2 server.

7. Setup the Server (Node, npm, git)

Once logged in, update packages and install required tools:

sudo apt update
sudo apt install nodejs npm git -y

You can confirm installs with:

node -v
npm -v
git --version

8. Clone the Project on EC2 and Setup

Repeat similar steps as local setup, but now on the EC2 instance:

git clone <repo-url>
cd <repo-folder>
touch .env

Add your environment variables to .env (use vim .env or nano .env):

PORT=3000
STRIPE_PUBLISHABLE_KEY=pk_test_...
STRIPE_SECRET_KEY=sk_test_...

Install dependencies:

npm install

Run the app:

npm run start

You’ll see “Server listening on port 3000” — but wait, it’s not publicly accessible yet.

9. Expose Your App to the Internet

By default, AWS doesn’t expose ports except 22 (SSH). You need to manually open port 3000.

  • Go to EC2 → Security Groups → Inbound Rules

  • Click Edit inbound rules

  • Add a new rule:

    • Type: Custom TCP

    • Port range: 3000

    • Source: Anywhere (0.0.0.0/0)

Save the rule.

10. Access the App from Browser

Now go to:

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

And boom! 🎉 Your Node.js app is running live on AWS.

Final Thoughts

You just:

  • Cloned a Node.js project

  • Set it up locally

  • Created a secure EC2 instance

  • Installed everything from scratch on it

  • Deployed your app and made it internet-accessible

This is exactly the kind of hands-on project that helps you learn AWS, EC2, IAM, Linux basics, and app deployment in one go.

0
Subscribe to my newsletter

Read articles from Kirti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kirti
Kirti

Hi, I'm KirtiI’m actively learning AWS, Python automation, and cloud best practices through real-world projects. Every challenge is a step forward, and every solution is something worth sharing. On this blog, you’ll find: Simplified guides on AWS core services Lessons from my journey breaking into cloud engineering I believe in learning in public—and this blog is where I document my progress, challenges, and wins.