Deploying a Node.js Application on AWS EC2 - A Simplified Guide
Welcome to Day 10 of the 100 Days of DevOps Challenge! Today, we'll walk through a step-by-step process of deploying a Node.js application directly on an Amazon EC2 instance.
Local Project Preparation
Start by preparing your Node.js application locally:
# Clone the repository
git clone https://github.com/verma-kunal/AWS-Session.git
cd AWS-Session
# Install project dependencies
npm install
Create a .env
file for environment configurations:
touch .env
Add necessary environment variables:
PORT=3000
NODE_ENV=production
DATABASE_URL=your_database_connection_string
SECRET_KEY=your_secret_key
AWS Deployment Preparation
Creating an IAM User
Log into AWS Management Console
Navigate to IAM service
Create a new IAM user with the following permissions:
- AmazonEC2FullAccess
When creating the user, download the access key credentials securely.
Configuring AWS CLI
Install AWS CLI on your local machine:
# For macOS/Linux
brew install awscli
# For Windows
# Download installer from AWS official website
Configure AWS CLI with your credentials:
aws configure
AWS Access Key ID [None]: YOUR_ACCESS_KEY
AWS Secret Access Key [None]: YOUR_SECRET_KEY
Default region name [None]: us-east-1
Default output format [None]: json
EC2 Instance Deployment Steps
1. Launch EC2 Instance
Open AWS EC2 Console
Click "Launch Instance"
Choose an Amazon Machine Image (AMI):
- Select "Amazon Linux 2" or "Ubuntu Server"
Choose Instance Type:
- t2.micro is suitable for a small Node.js application
Configure Security Group:
Allow SSH (Port 22)
Allow your application port (e.g., Port 3000)
2. Connect to EC2 Instance
Use SSH to connect to your instance:
# Use the .pem key file downloaded during instance creation
ssh -i /path/to/your-key.pem ec2-user@your-instance-public-ip
3. Prepare the EC2 Instance
Update the system and install necessary tools:
# For Amazon Linux
sudo yum update -y
sudo yum install -y nodejs npm git curl
# For Ubuntu
sudo apt update
sudo apt install -y nodejs npm git curl
Install process manager PM2 for keeping the application running:
sudo npm install -g pm2
4. Deploy Application
Clone your repository on the EC2 instance:
git clone https://github.com/verma-kunal/AWS-Session.git
cd AWS-Session
Install project dependencies:
npm install
Create and configure .env file:
touch .env
nano .env
# Add your environment variables
5. Run the Application
Use PM2 to manage the Node.js application:
# Start the application
pm2 start npm --name "nodejs-app" -- start
# Ensure app starts on system reboot
pm2 startup
pm2 save
# Check application status
pm2 status
6. Accessing Your Application
To access your application, use the EC2 instance's public IP address followed by the port number:
http://YOUR_EC2_PUBLIC_IP:3000
Security Considerations
Configure Security Groups to restrict access
Use the principle of least privilege
Keep your EC2 instance and dependencies updated
Use environment variables for sensitive information
Regularly rotate your SSH keys and access credentials
Troubleshooting
If you encounter issues:
Check PM2 logs:
pm2 logs
Verify application port is open
Ensure all dependencies are installed
Check environment variables
Monitoring
Use basic monitoring tools:
PM2 monit for application monitoring
AWS CloudWatch for instance metrics
Conclusion
You've successfully deployed a Node.js application directly on an EC2 instance! This approach provides a straightforward method to get your application running in the cloud.