Scheduled scaling allows you to change capacity based on time. It works by modifying the minCapacity and maxCapacity of scalable targets according to a schedule using cron expressions, rate expressions, or one-time scaling events.
Abstract base class for creating various types of scheduling expressions.
/**
* Schedule for scheduled scaling actions
*/
abstract class Schedule {
/**
* Construct a schedule from a literal schedule expression
* @param expression - The expression to use. Must be in a format that Application AutoScaling will recognize
* @returns Schedule instance
*/
static expression(expression: string): Schedule;
/**
* Construct a schedule from an interval and a time unit
* @param duration - Duration for rate-based scheduling (minutes, hours, days)
* @returns Schedule instance
*/
static rate(duration: Duration): Schedule;
/**
* Construct a Schedule from a moment in time
* @param moment - Date and time for one-time scaling
* @returns Schedule instance
*/
static at(moment: Date): Schedule;
/**
* Create a schedule from a set of cron fields
* @param options - Cron expression configuration
* @returns Schedule instance
*/
static cron(options: CronOptions): Schedule;
/**
* Retrieve the expression for this schedule
*/
abstract readonly expressionString: string;
}
/**
* Options to configure a cron expression
* All fields are strings so you can use complex expressions
* Absence of a field implies '*' or '?', whichever is appropriate
*/
interface CronOptions {
/**
* The minute to run this rule at
* @default Every minute
*/
readonly minute?: string;
/**
* The hour to run this rule at
* @default Every hour
*/
readonly hour?: string;
/**
* The day of the month to run this rule at
* @default Every day of the month
*/
readonly day?: string;
/**
* The month to run this rule at
* @default Every month
*/
readonly month?: string;
/**
* The year to run this rule at
* @default Every year
*/
readonly year?: string;
/**
* The day of the week to run this rule at
* @default Any day of the week
*/
readonly weekDay?: string;
}
/**
* A scheduled scaling action
*/
interface ScalingSchedule {
/**
* When to perform this action
*/
readonly schedule: Schedule;
/**
* When this scheduled action becomes active
* @default The rule is activate immediately
*/
readonly startTime?: Date;
/**
* When this scheduled action expires
* @default The rule never expires
*/
readonly endTime?: Date;
/**
* The new minimum capacity
* During the scheduled time, if current capacity is below minimum capacity,
* Application Auto Scaling scales out to the minimum capacity.
* At least one of maxCapacity and minCapacity must be supplied.
* @default No new minimum capacity
*/
readonly minCapacity?: number;
/**
* The new maximum capacity
* During the scheduled time, if current capacity is above maximum capacity,
* Application Auto Scaling scales in to the maximum capacity.
* At least one of maxCapacity and minCapacity must be supplied.
* @default No new maximum capacity
*/
readonly maxCapacity?: number;
}Usage Examples:
import * as appscaling from '@aws-cdk/aws-applicationautoscaling';
import * as cdk from '@aws-cdk/core';
// Scale out every morning at 8 AM
target.scaleOnSchedule('MorningScaleOut', {
schedule: appscaling.Schedule.cron({
hour: '8',
minute: '0'
}),
minCapacity: 20,
maxCapacity: 100,
});
// Scale back down every evening at 8 PM
target.scaleOnSchedule('EveningScaleIn', {
schedule: appscaling.Schedule.cron({
hour: '20',
minute: '0'
}),
minCapacity: 5,
maxCapacity: 20,
});
// Scale every 6 hours
target.scaleOnSchedule('RegularScaling', {
schedule: appscaling.Schedule.rate(cdk.Duration.hours(6)),
minCapacity: 10,
});
// One-time scaling event
const maintenanceDate = new Date('2024-12-25T02:00:00Z');
target.scaleOnSchedule('MaintenanceScaling', {
schedule: appscaling.Schedule.at(maintenanceDate),
minCapacity: 0,
maxCapacity: 0,
endTime: new Date('2024-12-25T06:00:00Z'),
});
// Complex cron expression - Scale on weekdays at 9 AM
target.scaleOnSchedule('WeekdayScaling', {
schedule: appscaling.Schedule.cron({
minute: '0',
hour: '9',
weekDay: 'MON-FRI'
}),
minCapacity: 15,
});
// Weekend scaling - reduced capacity
target.scaleOnSchedule('WeekendScaling', {
schedule: appscaling.Schedule.cron({
minute: '0',
hour: '10',
weekDay: 'SAT,SUN'
}),
minCapacity: 2,
maxCapacity: 10,
});Uses rate() expressions for regular intervals:
// Every 30 minutes
Schedule.rate(cdk.Duration.minutes(30)) // "rate(30 minutes)"
// Every 2 hours
Schedule.rate(cdk.Duration.hours(2)) // "rate(2 hours)"
// Every day
Schedule.rate(cdk.Duration.days(1)) // "rate(1 day)"Uses cron expressions for complex scheduling:
// Every day at 2:15 AM
Schedule.cron({ hour: '2', minute: '15' })
// Every weekday at 9 AM
Schedule.cron({
hour: '9',
minute: '0',
weekDay: 'MON-FRI'
})
// First day of every month at midnight
Schedule.cron({
hour: '0',
minute: '0',
day: '1'
})
// Every 15 minutes during business hours on weekdays
Schedule.cron({
minute: '0,15,30,45',
hour: '9-17',
weekDay: 'MON-FRI'
})Cron Expression Format:
cron(minute hour day month weekday year)minute: 0-59hour: 0-23day: 1-31month: 1-12 or JAN-DECweekday: 0-6 (0=Sunday) or SUN-SATyear: 1970-2199Special Characters:
*: All values?: No specific value (use for day or weekday)-: Range of values (e.g., 1-5),: List of values (e.g., 1,3,5)/: Step values (e.g., */15 for every 15 minutes)Uses at() expressions for specific moments:
// Scale at a specific date and time
const blackFriday = new Date('2024-11-29T00:00:00Z');
Schedule.at(blackFriday) // "at(2024-11-29T00:00:00)"Uses expression() for custom scheduling expressions:
// Custom expression
Schedule.expression('rate(90 minutes)')
Schedule.expression('cron(0 12 * * ? *)') // Every day at noonCapacity Planning:
minCapacity and maxCapacity to create scaling windowsSchedule Design:
minute in cron expressions to avoid every-minute executionTime Zones:
Example - Complete Daily Scaling Pattern:
// Business hours scaling (9 AM - 6 PM weekdays)
target.scaleOnSchedule('BusinessHoursStart', {
schedule: appscaling.Schedule.cron({
hour: '9',
minute: '0',
weekDay: 'MON-FRI'
}),
minCapacity: 20,
maxCapacity: 100,
});
target.scaleOnSchedule('BusinessHoursEnd', {
schedule: appscaling.Schedule.cron({
hour: '18',
minute: '0',
weekDay: 'MON-FRI'
}),
minCapacity: 5,
maxCapacity: 20,
});
// Weekend low capacity
target.scaleOnSchedule('WeekendScaling', {
schedule: appscaling.Schedule.cron({
hour: '0',
minute: '0',
weekDay: 'SAT'
}),
minCapacity: 2,
maxCapacity: 10,
});