Deploying a Node.js Application on AWS EC2 ๐
This guide provides a step-by-step process to deploy a Node.js application on an AWS EC2 instance. It covers everything from testing locally to deploying and configuring the application on a live server.
Testing the Project Locally ๐ ๏ธ
1. Clone the Repository
git clone https://github.com/verma-kunal/AWS-Session.git
2. Set Up Environment Variables
Create a .env
file in the root directory and add the following:
DOMAIN= ""
PORT=3000
STATIC_DIR="./client"
PUBLISHABLE_KEY=""
SECRET_KEY=""
3. Initialize and Start the Project
Install dependencies and start the project locally:
npm install
npm run start
Setting Up an AWS EC2 Instance โ๏ธ
1. Create an IAM User
Login to AWS Console and create an IAM user.
Select the following:
Access Type: Password
Permissions: Admin Access
2. Launch an EC2 Instance
Go to the EC2 Dashboard and launch an instance:
OS Image: Ubuntu (latest LTS)
Instance Type: t2.micro (free tier eligible)
Key Pair: Create a new key pair and download the
.pem
file.
3. Connect to the EC2 Instance
Use the following SSH command:
ssh -i instance.pem ubuntu@<EC2_PUBLIC_IP>
Configuring Ubuntu on Remote VM ๐ฅ๏ธ
1. Update System Packages
Update outdated packages to ensure system compatibility:
sudo apt update
sudo apt upgrade -y
2. Install Git
Follow this guide by DigitalOcean to install Git:
sudo apt install git -y
3. Install Node.js and npm
Follow this guide by DigitalOcean to set up Node.js and npm:
sudo apt install nodejs -y
sudo apt install npm -y
Deploying the Project on AWS ๐
1. Clone the Repository on EC2
git clone https://github.com/verma-kunal/AWS-Session.git
2. Set Up Environment Variables
Create a .env
file in the project directory on the server:
DOMAIN= "<Elastic_IP>"
PORT=3000
STATIC_DIR="./client"
PUBLISHABLE_KEY=""
SECRET_KEY=""
โก Tip: Set up an Elastic IP Address to ensure a static IP for your EC2 instance. Use this IP as the
DOMAIN
value.
3. Initialize and Start the Project
Install the required dependencies and start the application:
npm install
npm run start
4. Edit Security Group Inbound Rules
To allow traffic to the application:
Go to EC2 Dashboard > Security Groups > Edit Inbound Rules.
Add a rule:
Type: Custom TCP
Port: 3000
Source: 0.0.0.0/0 (or restrict to specific IPs for security)
Conclusion ๐
Your Node.js application is now live on AWS!
You can access it using the following URL in your browser:
arduinoCopy codehttp://<Elastic_IP>:3000
Why This is Essential for DevOps ๐ง
Managing deployments like this on AWS is a key skill for DevOps professionals. It ensures seamless application hosting, high availability, and automated scaling while providing insights into configuring and optimizing server environments.
Feel free to check out the repository and share your thoughts!
A big thank you to Kunal Verma and Abishek Veeramalla for their amazing guidance and mentorship during my journey of deploying a Node.js application on AWS EC2. Your tutorials and insights made the process seamless and helped me gain confidence in tackling real-world challenges. Grateful to learn from the best! ๐
Subscribe to my newsletter
Read articles from Harendra Barot directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Harendra Barot
Harendra Barot
I'm an IT professional and business analyst, sharing my day-to-day troubleshooting challenges to help others gain practical experience while exploring the latest technology trends and DevOps practices. My goal is to create a space for exchanging ideas, discussing solutions, and staying updated with evolving tech practices.