Automating EC2 Auto Scaling Group Instances Scaling with Scheduled Actions
Note: This document only works for enabling scheduling to AWS EC2 Autoscaling Groups
Pre-requisites:
AWS Console Access (necessary permissions to edit ASG's)
AWS CLI
Procedure:
Access the EC2 Service:
Log in to your AWS Console.
Navigate to the Services page and type EC2 in the search bar.
Select EC2 from the list of services.
Locate the Auto Scaling Group (ASG):
In the EC2 Dashboard, click on Auto Scaling Groups from the left-hand navigation panel.
Find and select the Auto Scaling Group (ASG) for which you want to enable scheduling.
Enable Scheduled Actions:
Scroll down to the Auto Scaling section at the bottom of the ASG details page.
Click on Scheduled actions.
Create a Scale-Down Action:
Click on Create scheduled action.
Name the action as scale-down-action.
Set the Desired capacity, Maximum capacity, and Minimum capacity to 0.
Under Recurrence, select Cron expression.
Input the cron expression for the desired scale-down time. For example,
30 15 * * 1-5
will scale down the instances on weekdays at 09:00:00 PM UTC.
Create a Scale-Up Action:
Repeat the steps to create another scheduled action.
Name this action scale-up-action.
Set the Desired capacity, Maximum capacity, and Minimum capacity according to the required number of instances for your ASG.
Input the cron expression for the scale-up time.
Note: The time is in UTC by default.
Review and Confirm:
Verify that both scheduled actions are correctly configured.
Below is a sample illustration of what the configured scheduled actions might look like.
You have successfully created scheduled actions and enabled scheduling for your EC2 Auto Scaling Group.
Using AWS CLI:
Run the below commands in your terminal after configuring the AWS Credentials.
To create scale down schedule action run below command after updating autoscaling group name and cron timings.aws autoscaling put-scheduled-update-group-action \
--auto-scaling-group-name <your_autoscaling_group_name> \
--scheduled-action-name scale-down-action \
--recurrence "30 15 * * 1-5" \
--desired-capacity 0 \
--min-size 0 \
--max-size 0
To create scale up schedule action run below command after updating autoscaling group name and cron timings.aws autoscaling put-scheduled-update-group-action \
--auto-scaling-group-name <your_autoscaling_group_name> \
--scheduled-action-name scale-up-action \
--recurrence "30 3 * * 1-5" \
--desired-capacity 13 \
--min-size 1 \
--max-size 15