EC2 Auto Scaling configuration with scheduled when to scale
This is one of the AWS SAA questions related to the design of a high-performance architecture.
Scheduling Auto Scaling actions to handle anticipated high traffic will lead to stable operation of the system.
In the following pages, we have introduced an Auto Scaling configuration that scales according to CPU utilization.
This is a type of scaling policy called a target tracking scaling policy.
This time, instead of scaling according to specific metrics, we will create a configuration that sets up scheduled actions and automatically scales over a specific period of time.
Environment
Create an ALB and attach an Auto Scaling group in a private subnet.
The instance in the group will be Amazon Linux 2, with Apache installed and running as a web server.
Schedule scaling actions to the group. The schedule will temporarily increase the group to 2 with a default instance count of 1.
CloudFormation template files
The above configuration is built with CloudFormation.
The CloudFormation template file is located at the following URL
https://github.com/awstut-an-r/awstut-saa/tree/main/02/004
Explanation of key points of template files
This configuration is almost the same as the one on the aforementioned page.
This page will cover content related to scheduled scaling actions.
Scheduled Scaling Actions
In order to scale scaling actions, actions at scale-out/in must be defined. The key is to specify the following two points
- When to take action
- Number of instances (maximum, minimum, desired)
Scheduled actions for scale-out
Resources:
ScheduledActionOut:
Type: AWS::AutoScaling::ScheduledAction
Properties:
AutoScalingGroupName: !Ref AutoScalingGroup
DesiredCapacity: !Ref MaxSize
MaxSize: !Ref MaxSize
MinSize: !Ref MaxSize
Recurrence: !Ref ScheduledActionOutRecurrence
Code language: YAML (yaml)
The Recurrence property allows you to schedule actions in a cron expression, but note that the cron expression must be written in UTC, not JST. So, for example, if you want to process at 15:00 every day, you need to write “0 6 * * * *”. This time, set “45 22 * * *” so that the scale-out is performed at 7:45.
You can set the number of instances with the DesiredCapacity, MaxSize, and MinSize properties. This time, specify “2” for the 3 property and set the number of instances to increase from the default of 1 to 2.
Scheduled action for scale-in
Resources:
ScheduledActionIn:
Type: AWS::AutoScaling::ScheduledAction
Properties:
AutoScalingGroupName: !Ref AutoScalingGroup
DesiredCapacity: !Ref MinSize
MaxSize: !Ref MinSize
MinSize: !Ref MinSize
Recurrence: !Ref ScheduledActionInRecurrence
Code language: YAML (yaml)
The structure is the same as the resource for scheduling out.
Set “50 22 * * *” to the Recurrence property and “1” to the DesiredCapacity, MaxSize, and MinSize properties.
The above settings collectively result in the behavior of increasing the number of instances to 2 between 7:45 and 7:50 daily.
Architecting
Use CloudFormation to build this environment and check the actual behavior.
Create CloudFormation stacks and check resources in stacks
Create CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following page
After checking the resources in each stack, the following is the information on the main resources created in this case.
- DNS name of ALB: saa-02-004-ALB-1864041057.ap-northeast-1.elb.amazonaws.com
- Auto Scaling Group: saa-02-004-AutoScalingGroup
The AWS Management Console also checks the status of resource creation.
First, check the Auto Scaling group.
By default, one instance is set to be launched.
Check the Auto Scaling scheduled actions.
You can see that two actions are scheduled. One for scale-out and one for scale-in.
Check the history of the Auto Scaling action.
One instance has been created as specified.
Check the status of the instance actually created.
You can see that one instance has been created as specified in the history.
Accessing ALB1
Now that we are ready, we access the ALB.
A response is received from the created instance.
You can see that the Auto Scaling group has been successfully attached to the ALB.
Scale-out behavior
Next, we will check the behavior during scale-out.
We will wait until 7:45, when scale-out starts, before checking.
First, check the action history of the Auto Scaling group.
The action started at 7:45 as scheduled, and the second instance was created.
Check the instances in the Auto Scaling group again.
You can see that the second instance has been created.
We have indeed been able to scale out the number of instances with the scheduled action.
Accessing ALB2
Access the ALB again.
A response is now returned from the second instance.
Scale-in behavior
Finally, we will check the behavior during scale-in.
We will wait until 7:50, when scale-in starts, before checking.
First, check the action history of the Auto Scaling group.
The action started at 7:50 as scheduled, and one of the two instances has finished.
Check the instances in the Auto Scaling group again.
You can see that only one instance is deployed in the Auto Scaling group.
We have indeed been able to scale in the number of instances with the scheduled action.
Summary
We have set up a scheduled action for the Auto Scaling group and verified the configuration to automatically scale to a specific time period.