🔄How to add Auto Scaling in AWS for Resources when Network Traffic is Heavy

Introduction

Amazon EC2 Auto Scaling helps you ensure that you have the right number of instances The right Amazon EC2 is available to handle the load for your application. You make collection of EC2 instances, called Auto Scaling Groups. You can specify a minimum amount instances in each Auto Scaling group, and Amazon EC2 Auto Scaling ensures that your groups never goes below this size. You can specify the maximum number of instances in each Auto Scaling group, and Amazon EC2 Auto Scaling ensures your groups are never in above this size. If you specify the desired capacity, fine when you create the group or any time afterward, Amazon EC2 Auto Scaling ensures your group has plenty of this instance. If you specify a scaling policy, then Amazon EC2 Auto Scaling can launch or stop instances when demand on your application increases or decrease.

You can create EC2 auto-scaling groups, a collection of EC2 instances. Set a minimum scaling value so that the group is never smaller than the specified size (if an instance fails, it is replaced). Set a maximum number of EC2 instances and the group will not exceed the specified size.

In addition, you can:

  • Manually add or remove EC2 instances from auto scaling groups (this is called manual scaling)

  • Change minimum or maximum capacity of a group on a predefined schedule

  • Set scaling plans that dynamically scale groups up and down


Configuration AWS

Create new Security Group

Name    : SG-For-ASG
Inbound Rules :
    - SSH      | from Anywhere (IPv4)
    - HTTP     | from Anywhere (IPv4)
    - HTTPS    | from Anywhere (IPv4)
Outbound Rules :
    - All Trafic | from Anywhere (IPv4)

Create Target Groups

In the left menu panel select Target Group and click "Create target groups"
- Choose a target type : Instances
- TG name : myTG
- Health check path : /
- Advance health check settings
* Healthy threshold		: 2
* Unhealthy threshold 	        : 2 
* Timeout			: 2
* Interval			: 5
click Next
without selecting any instance, immediately click "Create target group"

Create Load Balancer

In the left menu panel select Load Balancers then click "Create load balancer"
- Choose "Application Load Balancer"
- set name : ELB-for-Autoscaling
- Nework Mapping  : Check all Availability Zones.
- Security groups : select SG-for-ASG
- Listeners and routing
* protocol 	: HTTP
* port		: 80
* Default action	: Forward to myTG -> Select the target group that has been created.
click Create target group.

Create Launch Template

In the left menu panel select Launch Templates, then click "Create launch template"
- launch template name : LT-for-ASG
  description : Web server for AutoScaling.
Auto scaling guidance: check
- AMI/OS : Ubuntu 22.04 LTS
- Instance type : t2.micro
- Key pair: vockey
- Network settings:
* select existing security group = SG-for-ASG
- Resource tags, added new
key = Name
value = EC2-ASG-<your-name>
Resource type = Instance
- Advanced details
* scroll down, to the User Data section
add the following script:

#!/bin/bash
sudo apt update
sudo apt install apache2 wget unzip php -y
sudo wget https://us-west-2-tcprod.s3.amazonaws.com/courses/ILT-TF-100-TECESS/v4.7.2/lab-1-build-a-web-server/scripts/lab-app.zip
sudo unzip lab-app.zip -d /var/www/html/
sudo rm -f /var/www/html/index.html

Click create launch instance.

Create Auto Scaling Group

In the left menu panel select Auto Scaling Groups, then click create.
- Name : myASG
- Launch template : LT-for-ASG
click Next
- VPC : Default
- Availability Zones and Subnets: select or CHECK ALL subnets
click Next

- Load balancing: Attach to an existing load balancer
- Select target group: myTG
- Health checks: Check 'Turn on ELB health checks'
- Additional settings: Check 'Enable group metrics collection...'
click Next

- Group size
* Desired capacity: 1
* Minimum capacity: 1
* Maximum capacity: 3
- Scaling policies: click 'Target tracking scaling policy'
* Target value: 10
* Instance need : 5 seconds

Click Next to go to the Review page
click create Auto Scaling Group

Configuration is Completed

Trials :

  1. Go to the Instances menu, make sure there is 1 EC2 instance created automatically.

  2. wait until the status is running and 2/2 checks passed, (-+ 5 minutes)

  3. Access the web via Elastic Load Balancer DNS

  4. Run Load Test

  5. Check again in the instances menu, now 2 new instances have been added (scale up).

  6. Terminate all instances and wait a while.

  7. There will be 1 new instance

Last updated