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:

  1. Launch Template Misconfiguration: Missing key configurations such as AMI selection, instance type, or incorrect user data script can cause instances to fail.
  2. Auto Scaling Group Not Scaling Properly: If scaling policies are not set correctly, instances might not scale in or out as expected.
  3. ALB Not Routing Traffic: Incorrect target group attachment or health check misconfiguration can prevent the ALB from directing traffic to EC2 instances.
  4. Security Group Restrictions: If the security group does not allow HTTP (80) or SSH (22), instances may not be accessible.
  5. 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

  1. Navigate to AWS ConsoleEC2Launch TemplatesCreate Launch Template.
  2. 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

  1. Navigate to EC2Auto Scaling GroupsCreate Auto Scaling Group.
  2. Select Launch Template: Choose WebAppTemplate.
  3. Configure Group Size:
  • Desired Capacity: 2

  • Minimum Instances: 1

  • Maximum Instances: 4

  1. Network Configuration:
  • Choose an existing VPC.

  • Select at least two subnets across different Availability Zones (AZs).

  1. 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).

  1. Set Scaling Policies (Optional):
  • Enable Auto Scaling based on CPU utilization.

  • Example Policy: Scale out when CPU > 60%, Scale in when CPU < 40%.

  1. Create Auto Scaling Group.

Step 3: Create an Application Load Balancer (ALB)

  1. Navigate to EC2Load BalancersCreate Load Balancer.
  2. Select Application Load Balancer.
  3. Configure Basic Settings:
  • Name: WebAppALB

  • Scheme: Internet-facing

  • VPC: Select the same VPC as Auto Scaling.

  • Availability Zones: Choose at least two.

  1. Configure Listeners:
  • Protocol: HTTP

  • Port: 80

  1. Target Group:
  • Select existing Target Group (from Auto Scaling Group).
  1. Security Group:
  • Allow HTTP (80).
  1. Create Load Balancer.

Step 4: Test the Setup

  1. Get ALB DNS Name:
  • Navigate to EC2Load BalancersCopy 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

  1. Increase Load to Trigger Scaling:
  • Use Apache Benchmark (ab) or manually increase traffic.

  • ab -n 1000 -c 50 http://your-alb-dns-name/

  • Check EC2Auto Scaling Group to see if new instances launch.

  1. 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!