Deploying a MERN Stack Application on AWS with Route 53 and Load Balancing: A Complete Guide

Deploying a MERN Stack Application on AWS with Route 53 and Load Balancing: A Complete Guide

As organizations scale their web applications, proper deployment architecture becomes crucial. In this guide, I'll walk you through deploying a MERN (MongoDB, Express.js, React.js, Node.js) stack application on AWS using EC2 instances, Route 53 for domain management, and Elastic Load Balancing for improved availability.

Prerequisites

  • AWS Account with appropriate IAM permissions
  • Registered domain name
  • MERN stack application code in a Git repository
  • Basic understanding of Linux commands


Step 1: Setting Up the VPC and Network Infrastructure

1. Create a VPC:

  • Navigate to VPC Dashboard
  • Click "Create VPC"
  • Name your VPC
  • Choose IPv4 CIDR block (e.g., 10.0.0.0/16)

2. Create Subnets:

# Create at least two public subnets in different availability zones
Public Subnet 1: 10.0.1.0/24 (AZ-1)
Public Subnet 2: 10.0.2.0/24 (AZ-2)        

3. Configure Internet Gateway:

  • Create new Internet Gateway
  • Attach it to your VPC
  • Update route tables to allow internet access


Step 2: Launch EC2 Instances

1. Launch EC2 instances:

# Choose Ubuntu Server 22.04 LTS
# t2.micro for testing, t2.small or larger for production        

2. Configure Security Groups:

Inbound Rules:

- HTTP (80)

- HTTPS (443)

- SSH (22)

- Custom TCP (3000) for Node.js

- Custom TCP (27017) for MongoDB if needed


3. SSH into your instance:

   ssh -i your-key.pem ubuntu@your-instance-ip        

Step 3: Installing Required Software

1. Update system and install Node.js:

sudo apt update && sudo apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs        

2. Install PM2 for process management:

sudo npm install -g pm2        

3. Install Nginx:

   sudo apt install nginx -y        

Step 4: Deploying the Application

1. Clone your repository:

git clone your-repo-url
cd your-app        

2. Install dependencies:

# Backend
cd backend
npm install        
# Frontend
cd ../frontend
npm install        

3. Build the React application:

npm run build        

4. Configure Nginx:

nginx

# /etc/nginx/sites-available/your-app
server {

       listen 80;

       server_name your-domain.com;

       location / {

           root /path/to/react/build;

           try_files $uri $uri/ /index.html;

       }

       location /api {

           proxy_pass https://localhost:3000;

           proxy_http_version 1.1;

           proxy_set_header Upgrade $http_upgrade;

           proxy_set_header Connection 'upgrade';

           proxy_set_header Host $host;

           proxy_cache_bypass $http_upgrade;

       }

   }        


5. Enable the site:

sudo ln -s /etc/nginx/sites-available/your-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx        

6. Start the backend with PM2:

cd backend
pm2 start server.js
pm2 startup        

Step 5: Setting Up Load Balancer

1. Create Application Load Balancer:

  • Navigate to EC2 → Load Balancers
  • Click "Create Load Balancer"
  • Choose "Application Load Balancer"

2. Configure Load Balancer:

  • Name: your-app-lb
  • Scheme: internet-facing
  • IP address type: ipv4
  • Listeners: HTTP (80), HTTPS (443)
  • VPC: Choose your VPC
  • Subnets: Select multiple AZs

3. Configure Target Group:

  • Target type: Instances
  • Protocol: HTTP
  • Port: 80
  • Health check path: /

4. Register Targets:

  • Select your EC2 instances
  • Add to registered targets


Step 6: Domain Configuration with Route 53

1. Create Hosted Zone:

- Go to Route 53 console

- Click "Create Hosted Zone"

- Enter your domain name

2. Update Name Servers:

- Copy Route 53 nameservers

- Update them at your domain registrar

3. Create Record Sets:

- Type: A Record

- Name: yourdomain.com

- Alias: Yes

- Target: Your ALB DNS name


Step 7: SSL Configuration

1. Request SSL Certificate:

bash

Using AWS Certificate Manager (ACM)

- Request certificate

- Add domain names

- Choose DNS validation


2. Update Load Balancer:


- Add HTTPS listener

- Select certificate

- Update security group


Step 8: Monitoring and Maintenance

1. Set up CloudWatch monitoring:

# Install CloudWatch agent
sudo apt install amazon-cloudwatch-agent        

2. Configure alerts for:

- CPU utilization

- Memory usage

- Application errors

- Load balancer metrics


Best Practices and Security Considerations

1. Security:

- Use security groups effectively

- Implement WAF rules

- Regular security patches

- Enable VPC Flow Logs

2. Backup:

- Regular MongoDB backups

- AMI snapshots

- Code version control

3. Scaling:

- Use Auto Scaling groups

- Monitor performance metrics

- Optimize database queries


Troubleshooting Common Issues

1. Connection Issues:

- Check security groups

- Verify network ACLs

- Validate load balancer health checks

2. Performance Problems:

- Monitor CloudWatch metrics

- Check application logs

- Analyze slow queries

3. SSL Issues:

- Verify certificate validity

- Check DNS propagation

- Confirm listener configuration


Conclusion

Deploying a MERN stack application on AWS with load balancing and Route 53 requires careful planning and execution. This setup provides high availability, scalability, and robust performance for your application. Remember to monitor costs and optimize resources based on your actual usage patterns.

#AWS #MERN #WebDevelopment #DevOps #CloudComputing #TechnicalGuide #SoftwareEngineering #WebHosting

Ian McGavin

?? High-Impact Ad Creative & Data-Driven Growth | ?? Memorable Marketing That Converts | ?? Turning Attention into Sales & Revenue

3 个月

Deploying a MERN stack on AWS can definitelybe daunting, especially with the intricacies of network configuration, security, and performance optimization. Excited to see your tips on SSL and load balancing—key areas that can make or break a production app’s performance!

Pulina Thushan

Computer Engineering Undergraduate | Operations & Tech-Team @ Dreamshift | Aspiring Tech Enthusiast

4 个月

Useful tips ??

Chamodi Jayakody

Computer Engineering Undergraduate | Faculty of Engineering | University of Ruhuna | AIESECer

4 个月

Great work! ??

Menura Dulkith

Department of Computer Engineering ,University of Ruhuna | Java Developer | Roboticist | Entrepreneur | Blockchain Enthusiast & Crypto Investor |

4 个月

Very helpful

Viraj Samarasinghe

13K+ Followers | Computer Engineering Student @ University of Ruhuna | Java Developer | Spring Boot, SQL, Web applications and Agile Development| Future AI Engineer |

4 个月

?It's really helpful!?

要查看或添加评论,请登录

KAVISHKA KALHARA的更多文章

社区洞察

其他会员也浏览了