Introduction
In today’s cloud-driven world, ensuring your web application is always available and resilient to failures is critical. This post walks you through the process of setting up a highly available and fault-tolerant web application on AWS using three core services: EC2 (Elastic Compute Cloud) for hosting, ALB (Application Load Balancer) for distributing traffic, and ASG (Auto Scaling Group) for automatic scaling and recovery. By the end of this setup, your app will be able to handle traffic efficiently across multiple Availability Zones — even if one instance fails.
Step 1:
- Launch EC2 Instances (or Create a Launch Template) You can either manually launch 2 EC2 instances or create a Launch Template to use with an ASG.
AMI: Amazon Linux 2
Instance Type: t2.micro (Free tier)
Network: Default VPC
AZs: Choose different AZs (e.g., us-east-1a and us-east-1b)
Launch both instances and make sure they are running actively
Step 2: Install Apache Web Server
SSH into each instance and run:
bash
CopyEdit
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
On WebServer1:bash
CopyEdit
echo "Welcome to Web Server 1 - $(hostname)" | sudo tee /var/www/html/index.html
On WebServer2:
bash
CopyEdit
echo "Welcome to Web Server 2 - $(hostname)" | sudo tee /var/www/html/index.html
Step 3: Create Target Group
• Target type: Instances
• Protocol: HTTP, Port: 80
• Health check path: /
• Register both instances.
Step 4: Create Application Load Balancer
• ALB type: Application Load Balancer
• Scheme: Internet-facing
• Listener: HTTP on port 80
• Subnets: Select at least 2 in different AZs
• Security Group: Allow HTTP (port 80)
Attach the previously created Target Group
Step 5: Test with curl from Git Bash
After setup, test the ALB using:
bash
CopyEdit
curl http://
Refresh multiple times — you should see responses from both WebServer1 and WebServer2 (round-robin behavior).
Conclusion
By combining EC2, ALB, and ASG, you've built a robust and scalable infrastructure that ensures your web application remains available even during instance failures or spikes in traffic. The use of multiple Availability Zones and automatic instance replacement through ASG highlights the power of cloud-native design for high availability. This setup not only improves fault tolerance but also prepares your application for real-world production environments on AWS.
Top comments (0)