ASG(Auto Scaling Group) feature comes along with EC2 (Elastic Compute Cloud) service of AWS(Amazon Web Services) Cloud. An ASG contains a collection of EC2 instances that are treated as a logical grouping for the purpose of automatic scaling. An ASG enables to use EC2 Auto Scaling features such as health check replacements and scaling policies. It helps to maintain the number of instances in an ASG and automatic scaling of the EC2 Auto Scaling service. To create and ASG, we first need to create a Launch Configuration. 

A launch configuration is a configuration template that an ASG  uses to launch EC2 instances. When we create a launch configuration, we specify information for instances such as Amazon Machine Image (AMI), the instance type, a key pair, security groups, etc. 

Auto Scaling monitors our applications and automatically adjusts the desired capacity if there is any difference in the count.

There are no additional charges for Auto Scaling. We need to pay only for the AWS resources needed to run the applications and Amazon CloudWatch monitoring fees.

To know more about the billing related to ASG, click here.

In this article, we will see how to create an ASG and see how the number of instances can be maintained if the count decreases or load on the system increases.

  1. AWS Account (Create if you don’t have one). 
  1. Login to AWS.
  2. Create a Launch Configuration 
  3. Create an Auto Scaling Group
  4. Terminate the instance to let ASG manage the instance count.
  5. Increase the load on servers to let ASG increase the instance count.
  6. Delete the Auto Scaling Group.

Login to AWS

  1. Click here to go to AWS Login Page.

When we hit the above link, we will see a web page as follows where we are required to login using our login details. 

<img alt="AWS Login" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.40_.12_PM_.png5e83056090f31.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

Once we login into AWS successfully, we will see the main console with all the services listed as follows.

<img alt="AWS Management Console" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.41_.09_PM_.png5e830560bc5b4.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

Create a Launch Configuration

To create a Launch Configuration, click on “Services” on the navigation bar at the top of the screen and click on “EC2” to go to EC2 console.

Resource Groups” data-ezsrc=”https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.41_.17_PM_.png5e830561098c8.jpg” ezimgfmt=”rs rscb1 src ng ngcb1″ height=”413″ src=”data:image/svg xml,” width=”750″>

On the navigation pane on the left of the screen, under “Auto Scaling”, choose “Launch Configurations”.

<img alt="Resources" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.41_.36_PM_.png5e8305613c7b3.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

On the next page, choose “Create launch configuration”.

<img alt="Create launch configuration" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.41_.50_PM_.png5e8305617a47b.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="418" src="data:image/svg xml,” width=”750″>

On the Choose AMI page, select an AMI. You can even select the AMI from My AMIs if you have your own AMI. Here, I am choosing an Ubuntu Server 18.04 LTS AMI which is applicable for Free Tier.

<img alt="Chose AMI image" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.42_.00_PM_.png5e830561b7806.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="414" src="data:image/svg xml,” width=”750″>

On the Choose Instance Type page, select a hardware configuration for your instance. Click on “Next: Configure” details. 

Here, I am selecting an Instance Type which is applicable for Free Tier. You can choose an instance type of your choice.

<img alt="Choose instance type" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.42_.10_PM_.png5e830561e564b.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

On this page, give a name to the Launch Configuration to be  created and keep the rest as is. Click on “Next: Add storage”.

<img alt="Add Storage" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.42_.29_PM_.png5e8305621a678.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

Specify the desired Disk Space you want and click on “Next: Configure Security Group”.

<img alt="Configure Security Group" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.42_.38_PM_.png5e83056255651.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="412" src="data:image/svg xml,” width=”750″>Advertisements

On the Review page, select “Create a new security group”, and then click on Review. This will help to allow take ssh access to the server from anywhere as the source is 0.0.0.0/0.

<img alt="Create a new security group" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.42_.53_PM_.png5e830562a2dee.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

On the Review page, choose “Create launch configuration”.

<img alt="Create launch configuration" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.43_.01_PM_.png5e830562e38f7.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

To select an existing key pair or create a new key pair, select one of the listed options of your choice and then select the acknowledgment check box, and then choose Create launch configuration.

<img alt="Select key pair" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.43_.24_PM_.png5e83056333173.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

Create an Auto Scaling Group

Once you are done with creating a Launch Configuration, you will see the following screen. To create an ASG, click on “Create an Auto Scaling Group using this launch configuration”.

<img alt="Create an Auto Scaling Group" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.43_.40_PM_.png5e83056369827.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="414" src="data:image/svg xml,” width=”750″>

Give a name the ASG to be created and select the existing subnets from your VPC for High Availability and click on “Next: configure scaling policies”.

<img alt="configure scaling policies" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.44_.10_PM_.png5e830563b4445.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="412" src="data:image/svg xml,” width=”750″>

On this page, select “use scaling policies to adjust the capacity of this group” and specify the instance count to scale between the desired and the maximum number of instances in the ASG. In the metric type keep “Average CPU utilization” and specify the “Target Value” of your choice. This will increase the instance count in the Group if CPU load exceeds the specified target value in % for the specified period. Click on “Next: configure notification”.

<img alt="use scaling policies to adjust the capacity of this group" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.44_.37_PM_.png5e830564035f2.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

This is an optional step which help receive the notifications. I am not configuring this as it is an optional. Click on “Next: Configure tags”.

<img alt="Add notification" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.44_.51_PM_.png5e83056438b84.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="412" src="data:image/svg xml,” width=”750″>

This is also an optional step. To add tags, choose Edit tags, provide a tag key and value for each tag, and then choose Review.

<img alt="Add tags" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.45_.11_PM_.png5e8305646f69f.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

On the Review page, click on  “Create Auto Scaling group” button.

<img alt="Create Auto Scaling group" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.45_.17_PM_.png5e830564ab5a5.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="411" src="data:image/svg xml,” width=”750″>

If you get “Failed to create Auto Scaling group”, do not panic and just click on Retry.

<img alt="Retry to create group" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.45_.54_PM_.png5e830564de08d.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

Now, go to Auto Scaling Page and you can see the status of the Auto Scaling group, click on Activity History to see the status.

<img alt="Activity History" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.47_.40_PM_.png5e83056529ce4.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="414" src="data:image/svg xml,” width=”750″>

You can also see the instance state and the number of instances from the Instance Page.

<img alt="Instance state and number of instances" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.48_.29_PM_.png5e83056585cbc.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

Terminate the instance to let ASG manage the instance count.

To see if the ASG is working as expected, terminate one of the instances. Select the instance and click on Actions -> Instance State -> Terminate and when prompted click on “Yes, Terminate”. 

We had specified 2 as our desired/minimum count in the scaling policies which means our ASG must always have 2 instances in the healthy state. Once the terminate goes down ASG will wait for 300 seconds which we had specified in Scaling Policy before performing any scaling activity.

<img alt="Terminate instance" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.48_.55_PM_.png5e830565c9b9a.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="411" src="data:image/svg xml,” width=”750″>

After waiting for 300 seconds, ASG gets invoked and try to launch a new instance to maintain the desired count. You can check the status in Activity history of ASG.

<img alt="Launch of new instance" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_7.50_.22_PM_.png5e830566275dd.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

Increase the load on  instances to let ASG increase the instance count.

Now, we will see how ASG the instance count if the load on the servers crosses the target value we specified in scaling policy we created.

To increase the load on the server login into both the EC2 instances.

To know learn to create an EC2 instance and know the steps to connect to it, click here.

Once you login, install the package required to increase the load using following commands.

sudo apt-get update
sudo apt-get install stress

 Increase the CPU load using the following command.

stress --cpu  50 --timeout 400

This command will increase the CPU load to 50% for 400 seconds.

We had specified the target value as 40% in ASG.  Once the CPU load crosses the target value,  ASG will wait for 300 seconds which we had specified in Scaling Policies before performing any scaling activity.

After waiting for 300 seconds, ASG gets invoked and try to launch a new instance to meet the requirement.

You can check the status in Activity history of ASG.

<img alt="Load testing" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_8.04_.01_PM_1.png5e8305667250b.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="413" src="data:image/svg xml,” width=”750″>

You can also see the instance count on instance page.

<img alt="Number of instances has increased" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_8.04_.27_PM_.png5e830566b5a3c.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="415" src="data:image/svg xml,” width=”750″>

Delete the Auto Scaling Group

Just deleting all the instances is not sufficient to clean up the resources. ASG will recreate the instance to match the desired state. To clean up the resource, we need to delete the ASG.

To delete the ASG, go to ASG we created and click on Actions -> Delete and when prompted click on “Yes, Delete”.

<img alt="Delete Autoscaling Group" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/03/echo/Screenshot_2019-12-12_at_8.04_.48_PM_1.png5e83056705c35.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="415" src="data:image/svg xml,” width=”750″>

Conclusion:

In this article, we have created a Launch Configuration and created an ASG using it. We saw how ASG updates the desired state and increases the instance count to meet the scaling policy. We also saw how an ASG can be deleted.