If you’ve ever seen EC2 Auto Scaling spin up instances when you don’t expect it, or worse, not spin them up when you absolutely need them, you’ve probably been fighting against the default reactive scaling policies. The surprising truth is that Auto Scaling can be proactive, anticipating your traffic before it even hits your servers.
Let’s see it in action. Imagine a retail website that gets a predictable surge of traffic every weekday morning between 9 AM and 11 AM PST, and again in the evening from 7 PM to 9 PM PST. We can configure Auto Scaling to scale up before this surge hits and scale down afterward.
Here’s a simplified setup:
Launch Template:
We’ll use an existing launch template lt-012345abcdef67890 that defines our EC2 instance configuration (AMI, instance type, security groups, etc.).
Auto Scaling Group:
Let’s create an ASG named my-predictable-asg.
{
"AutoScalingGroupName": "my-predictable-asg",
"LaunchTemplate": {
"LaunchTemplateId": "lt-012345abcdef67890",
"Version": "$Latest"
},
"MinSize": 2,
"MaxSize": 10,
"DesiredCapacity": 2,
"VPCZoneIdentifier": "subnet-012345abcdef67890,subnet-012345abcdef67891",
"TargetGroupARNs": ["arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-app-tg/f12345abcdef67890"]
}
Scheduled Actions:
Now, the magic. We’ll create scheduled actions to adjust the DesiredCapacity of our ASG.
-
Morning Ramp-up: Scale up before the 9 AM PST surge.
- Name:
morning-ramp-up - Start Time:
2023-10-27T08:45:00Z(Note: AWS Scheduled Actions use UTC. We’ll account for PST later). - End Time:
2023-10-27T11:30:00Z - Recurrence:
Daily - Desired Capacity:
8
- Name:
-
Morning Ramp-down: Scale down after the surge.
- Name:
morning-ramp-down - Start Time:
2023-10-27T11:30:00Z - End Time:
2023-10-27T13:00:00Z - Recurrence:
Daily - Desired Capacity:
3
- Name:
-
Evening Ramp-up: Scale up before the 7 PM PST surge.
- Name:
evening-ramp-up - Start Time:
2023-10-27T18:45:00Z - End Time:
2023-10-27T21:30:00Z - Recurrence:
Daily - Desired Capacity:
7
- Name:
-
Evening Ramp-down: Scale down after the surge.
- Name:
evening-ramp-down - Start Time:
2023-10-27T21:30:00Z - End Time:
2023-10-27T23:00:00Z - Recurrence:
Daily - Desired Capacity:
2
- Name:
Time Zone Conversion: To match the 9 AM PST surge, we need to convert PST to UTC. PST is UTC-8. So, 9 AM PST is 17:00 UTC. The scheduled actions above are set to trigger at 8:45 AM UTC (which is 12:45 AM PST), too early. We need to adjust:
- Morning Ramp-up:
2023-10-27T17:00:00Z(9 AM PST) - Morning Ramp-down:
2023-10-27T20:00:00Z(12 PM PST) - Evening Ramp-up:
2023-10-27T03:00:00Z(7 PM PST the next day if we only set it once, or2023-10-27T03:00:00Zand2023-10-28T03:00:00Zfor daily recurrence). Ah, this is where it gets tricky.
Recurrence and Time Zones:
AWS Scheduled Actions use cron expressions or a simple Daily recurrence. For recurring daily events at a specific local time, you need to be careful. A Daily recurrence means it happens once every 24 hours at the specified UTC start time. If you want it to happen at 9 AM PST every day, you’d set the Start Time to 17:00:00Z. If you need different times on different days, or more complex schedules, you’ll use cron expressions.
Let’s refine the schedule for daily predictability:
- Morning Ramp-up:
- Name:
morning-ramp-up-daily - Start Time:
2023-10-27T17:00:00Z(9 AM PST) - Recurrence:
Daily - Desired Capacity:
8
- Name:
- Morning Ramp-down:
- Name:
morning-ramp-down-daily - Start Time:
2023-10-27T20:00:00Z(12 PM PST) - Recurrence:
Daily - Desired Capacity:
3
- Name:
- Evening Ramp-up:
- Name:
evening-ramp-up-daily - Start Time:
2023-10-27T03:00:00Z(7 PM PST) - Recurrence:
Daily - Desired Capacity:
7
- Name:
- Evening Ramp-down:
- Name:
evening-ramp-down-daily - Start Time:
2023-10-27T06:00:00Z(9 PM PST) - Recurrence:
Daily - Desired Capacity:
2
- Name:
How it Works:
When a scheduled action’s start time is reached, Auto Scaling overrides any dynamic scaling policies (like Target Tracking or Step Scaling) for the duration of that action. It directly sets the DesiredCapacity to the value specified in the scheduled action. Once the scheduled action’s end time is reached, or if the action is manually deleted, Auto Scaling reverts to its previous scaling mode, typically resuming dynamic scaling policies.
This allows you to provision capacity precisely when you know you’ll need it, ensuring a smooth user experience during peak times and saving costs by scaling down when traffic subsides. You’re essentially telling AWS, "I know traffic will spike at this exact time, so get me X instances ready."
The system doesn’t guess when you need more instances; you tell it. This is incredibly powerful for applications with predictable, recurring load patterns, like daily batch jobs, scheduled reporting, or, as in our example, predictable user traffic spikes. It complements dynamic scaling, which remains crucial for handling unexpected bursts.
You can view and manage these scheduled actions via the AWS Management Console under the Auto Scaling group’s "Scheduled actions" tab, or using the AWS CLI/SDKs.
The next step is to consider how to combine these predictable schedules with reactive scaling to handle unpredictable traffic spikes that might occur outside your known patterns.