How to Configure Scheduled Scaling Actions for EC2 Auto Scaling Group with AWS CLI

Sourav Karmakar
AWS in Plain English
4 min readOct 27, 2021

--

Photo by Vipul Jha on Unsplash

In this tutorial, I will guide you to deploy EC2 Auto Scaling Group and configure scheduled scaling actions with AWS CLI.

Scheduled Scaling :
Scheduled Scaling Actions helps you to scale in/out instances in your Auto Scaling Group according to your scheduled time.

Here are the steps that we are going to perform to set up scheduled scaling -
1. Create an EC2 Launch Template
2. Deploy the Auto Scaling Group
3. Configure Scheduled Scaling actions

Let’s Begin

Prerequisites:
* IAM permissions to create and manage EC2 auto-scaling groups.
* AWS CLI and programmatic access to AWS.
* A VPC with subnets, EC2 Key Pair, Security Group

Step 1: Create an EC2 Launch Template

{
"NetworkInterfaces": [{
"DeviceIndex": 0,
"Groups": ["sg-00f989ab694dce84b"]
}],
"ImageId": "ami-02e136e904f3da870",
"InstanceType": "t2.micro",
"KeyName": "myKeyPair"
}

Save the above text as a JSON file. Name it LaunchTemplate.json.
Modify the Security Group id, Image ID, Instance Type and Key Pair name accordingly.

Run the below command to create the Launch Template.

aws ec2 create-launch-template \
--launch-template-name AutoScaleLaunchTemplate \
--version-description Version_1 \
--launch-template-data file://LaunchTemplate.json \
--region us-east-1
EC2 Launch template creation

Step 2: Deploy the Auto Scaling Group

Modify the parameters and Run this command to deploy the Auto Scaling Group.

aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name EC2AutoScalingGroup \
--launch-template LaunchTemplateId=lt-03661925d91c1cea7 \
--min-size 1 \
--max-size 5 \
--vpc-zone-identifier "subnet-0960629201cddd260"
Deploy the Auto Scaling Group
See the Auto Scaling Group from Console

— — — — — — — — — — — — — — — — — — — — — — — — —

Step 3: Configure Scheduled Scaling Actions

Two types of schedules can be configured:
a. one-time scaling actions
b. recurring scaling actions

a. Create a Scheduled Action that Occurs Only Once:

#Use this to scale out one time only
aws autoscaling put-scheduled-update-group-action \
--scheduled-action-name oneTimeScheduledScaleOutAction \
--auto-scaling-group-name EC2AutoScalingGroup \
--start-time "2021-10-28T10:00:00Z" \
--desired-capacity 4

If the group has fewer than 4 instances at the date and time specified for --start-time (10:00 AM UTC on Oct 28, 2021) then it scales out to 4 instances.

#Use this to scale in one time only
aws autoscaling put-scheduled-update-group-action \
--scheduled-action-name oneTimeScheduledScaleInAction \
--auto-scaling-group-name EC2AutoScalingGroup \
--start-time "2021-10-28T18:00:00Z" \
--desired-capacity 1

If the group has more than 1 instance at the date and time specified for --start-time (6:00 PM UTC on Oct 28, 2021) then it scales in to 1 instance.

Configure one time scheduled actions

b. Create a Scheduled Action that Runs on a Recurring Schedule

aws autoscaling put-scheduled-update-group-action \
--scheduled-action-name RecurringScheduledAction \
--auto-scaling-group-name EC2AutoScalingGroup \
--recurrence "0 10 * * 1-5" \
--time-zone "Asia/Calcutta" \
--desired-capacity 3

The above script can be used for recurring scaling. It initiates auto-scaling every Monday through Friday at 10 AM Asia/Calcutta time. If the capacity is below 3 then it will increase the desired capacity to 3. If the capacity is higher than 3 then it will decrease the desired capacity to 3.

Recurring Schedule Action configuration
Recurring Schedule Action from AWS Console

Thank you for following this tutorial. Don’t forget to follow me and clap if you find this useful.

More content at plainenglish.io

--

--