Configure Auto Scaling with Load balancer
Scenario: we want to auto-scale web application which is running behind a load balancer on two EC2 servers. The moment EC2 gets CPU usage more than 60%, we want to add two more servers.
Steps:
- Create Application Load Balancer
- Create Launch Configuration
- Create Auto Scaling Group
- Test Auto Scaling
Step1: Create Application Load Balancer


Select a Security Group where you have Port 80 open



Don’t register any target (as it will be registered via auto scaling) and create.
Step2: Create Launch Template



Provide user data script in Advanced
#!/bin/sh
sudo yum update -y
sudo yum install httpd -y
sudo service httpd start
chkconfig httpd on
echo "Hello World from $(hostname -f)" > /var/www/html/index.html
echo "Healthy" > /var/www/html/hc.html
Step 3: Create Auto Scaling Group for Launch Template created in Step 2






the cooldown period helps to ensure that your Auto Scaling group doesn’t launch or terminate additional instances before the previous scaling activity takes effect. You can configure the length of time based on your instance warmup period or other application needs.


Warm-up value for Instances allows you to control the time until a newly launched instance can contribute to the CloudWatch metrics, so when warm-up time has expired, an instance is considered to be a part Auto Scaling group and will receive traffic

After it is created you should see 2 instances running in EC2.
Check your Application via ELB DNS and EC2, HealthCheck via Public DNS e.g.
http://webapplb-1654405070.us-east-1.elb.amazonaws.com/
http://ec2-34-236-187-45.compute-1.amazonaws.com/
http://ec2-34-236-187-45.compute-1.amazonaws.com/hc.html
Step4: Test Autoscaling
Login to EC2 instance
Install stress utility to generate cpu load
sudo amazon-linux-extras install epel -y
sudo yum install stress -y
Generate load
sudo stress --cpu 6 --timeout 180
Monitor and check if new instances are created.