This exercise demonstrates how to configure Elastic Load Balancing (ELB) and Auto Scaling in AWS to distribute traffic across multiple instances and automatically adjust capacity based on traffic demand.
- AWS Account.
- Basic familiarity with EC2, ELB, and Auto Scaling.
- A web server setup script (optional, provided in Step 4).
- Launch multiple EC2 instances behind a Load Balancer (ALB).
- Configure Auto Scaling to adjust the number of instances based on demand.
- Test the setup by simulating traffic.
- Go to the EC2 Dashboard and click Launch Instances.
- Choose Amazon Linux 2 AMI or Ubuntu.
- Select the instance type (
t2.microfor free tier). - Configure the instance:
- Add storage (default is fine).
- Create or use an existing security group allowing:
- HTTP (Port 80) from anywhere.
- SSH (Port 22) from your IP.
- Add a tag:
Key: Name, Value: WebServer. - Review and launch.
-
SSH into the instance:
ssh -i your-key.pem ec2-user@<Public-IP>
-
Install and start a web server:
- Amazon Linux:
sudo yum update -y sudo yum install -y httpd echo "Welcome to $(hostname)!" | sudo tee /var/www/html/index.html sudo systemctl start httpd sudo systemctl enable httpd
- Ubuntu:
sudo apt update sudo apt install -y apache2 echo "Welcome to $(hostname)!" | sudo tee /var/www/html/index.html sudo systemctl start apache2 sudo systemctl enable apache2
- Amazon Linux:
-
Confirm the web server is running by accessing the public IP in your browser.
- Navigate to EC2 Dashboard > Target Groups.
- Create a new Target Group:
- Type: Instances.
- Protocol: HTTP.
- Port: 80.
- Health Check Path:
/.
- Register your running EC2 instance to the target group.
- Go to EC2 Dashboard > Load Balancers.
- Click Create Load Balancer > Application Load Balancer.
- Configure the ALB:
- Name:
WebApp-ALB. - Scheme: Internet-facing.
- Listeners: HTTP (Port 80).
- Availability Zones: Select at least two.
- Name:
- Assign a security group allowing HTTP traffic (Port 80).
- Register the target group created in Step 3.
- Complete the setup and note the ALB DNS name.
- Navigate to Auto Scaling Groups.
- Click Create Auto Scaling Group.
- Configure the Auto Scaling group:
- Launch Template or Configuration:
- Create a new one with the same AMI and instance type as the initial EC2 instance.
- VPC and Subnets: Use the same as the Load Balancer.
- Attach the Auto Scaling group to the target group.
- Launch Template or Configuration:
- Set Scaling Policies:
- Minimum Instances: 1
- Desired Instances: 1
- Maximum Instances: 3
- Add a scaling policy based on CPU utilization:
- Target Tracking Scaling Policy: Target value of 50% CPU utilization.
- Open the ALB DNS name in your browser. It should load the web page served by one of the instances.
- Simulate high traffic by using a load testing tool (e.g.,
aborlocust):ab -n 10000 -c 100 http://<ALB-DNS-Name>/
- Monitor Auto Scaling:
- Go to the Auto Scaling group and observe the number of instances increasing.
- Check the target group to see healthy instances being added.
- Terminate the EC2 instances.
- Delete the Load Balancer.
- Delete the Target Group.
- Delete the Auto Scaling group.