Problem Statement
As web applications grow, they need to handle fluctuating traffic while maintaining high availability. A single EC2 instance is not reliable for production environments due to potential failures and performance limitations. The solution is to deploy an architecture using EC2, Application Load Balancer (ALB), and Auto Scaling to ensure high availability, fault tolerance, and scalability.
Debugging Challenges
During the deployment process, several issues may arise:
- Launch Template Misconfiguration: Missing key configurations such as AMI selection, instance type, or incorrect user data script can cause instances to fail.
- Auto Scaling Group Not Scaling Properly: If scaling policies are not set correctly, instances might not scale in or out as expected.
- ALB Not Routing Traffic: Incorrect target group attachment or health check misconfiguration can prevent the ALB from directing traffic to EC2 instances.
- Security Group Restrictions: If the security group does not allow HTTP (80) or SSH (22), instances may not be accessible.
- VPC and Subnet Selection Issues: If subnets are not spread across multiple availability zones, high availability cannot be ensured.
Solution Approach
Step 1: Create a Launch Template
- Navigate to AWS Console → EC2 → Launch Templates → Create Launch Template.
- Configure the Launch Template:
Name: WebAppTemplate
AMI: Amazon Linux 2 or Ubuntu
Instance Type: t2.micro(Free Tier) or t3.medium
Key Pair: Select an existing key pair or create a new one.
User Data(for automatic Apache/Nginx installation):
#!/bin/bash
sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
echo "Welcome to Scalable Web App" | sudo tee /var/www/html/index.html
Security Group: Allow SSH (22), HTTP (80), and HTTPS (443).
Create the Launch Template.
Step 2: Create an Auto Scaling Group
- Navigate to EC2 → Auto Scaling Groups → Create Auto Scaling Group.
- Select Launch Template: Choose WebAppTemplate.
- Configure Group Size:
Desired Capacity: 2
Minimum Instances: 1
Maximum Instances: 4
- Network Configuration:
Choose an existing VPC.
Select at least two subnets across different Availability Zones (AZs).
- Attach Load Balancer:
Choose Application Load Balancer (ALB).
Create Target Group:
Target Type: Instance
Protocol: HTTP
Health Check: /
Register instances later (Auto Scaling will handle this).
- Set Scaling Policies (Optional):
Enable Auto Scaling based on CPU utilization.
Example Policy: Scale out when CPU > 60%, Scale in when CPU < 40%.
- Create Auto Scaling Group.
Step 3: Create an Application Load Balancer (ALB)
- Navigate to EC2 → Load Balancers → Create Load Balancer.
- Select Application Load Balancer.
- Configure Basic Settings:
Name: WebAppALB
Scheme: Internet-facing
VPC: Select the same VPC as Auto Scaling.
Availability Zones: Choose at least two.
- Configure Listeners:
Protocol: HTTP
Port: 80
- Target Group:
- Select existing Target Group (from Auto Scaling Group).
- Security Group:
- Allow HTTP (80).
- Create Load Balancer.
Step 4: Test the Setup
- Get ALB DNS Name:
Navigate to EC2 → Load Balancers → Copy ALB DNS Name.
Open a browser and enter:
http://your-alb-dns-name
- You should see: Welcome to Scalable Web App.
Step 5: Verify Auto Scaling
- Increase Load to Trigger Scaling:
Use Apache Benchmark (ab) or manually increase traffic.
ab -n 1000 -c 50 http://your-alb-dns-name/
Check EC2 → Auto Scaling Group to see if new instances launch.
- Stop an Instance:
- Manually stop an instance to verify if Auto Scaling replaces it.
Conclusion
This setup ensures:
✅ High Availability using ALB.
✅ Scalability using Auto Scaling.
✅ Redundancy across multiple AZs.
This architecture is commonly used for production applications that require reliability, scalability, and cost optimization. By learning in public, you not only solidify your understanding but also help others in their AWS journey!