Skip to content

Commit

Permalink
feat(applicationautoscaling): add timezone support for ScheduledAction
Browse files Browse the repository at this point in the history
  • Loading branch information
robertd committed Feb 15, 2024
1 parent 6fbcce6 commit 91d94e5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/aws-cdk-lib/aws-applicationautoscaling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ scaling take over at night:

```ts
declare const resource: SomeScalableResource;
declare const TimeZone: TimeZone;

const capacity = resource.autoScaleCapacity({
minCapacity: 1,
Expand All @@ -187,11 +188,13 @@ const capacity = resource.autoScaleCapacity({
capacity.scaleOnSchedule('PrescaleInTheMorning', {
schedule: appscaling.Schedule.cron({ hour: '8', minute: '0' }),
minCapacity: 20,
timeZone: TimeZone.AMERICA_DENVER,
});

capacity.scaleOnSchedule('AllowDownscalingAtNight', {
schedule: appscaling.Schedule.cron({ hour: '20', minute: '0' }),
minCapacity: 1
minCapacity: 1,
timeZone: TimeZone.AMERICA_DENVER,
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Schedule } from './schedule';
import { BasicStepScalingPolicyProps, StepScalingPolicy } from './step-scaling-policy';
import { BasicTargetTrackingScalingPolicyProps, TargetTrackingScalingPolicy } from './target-tracking-scaling-policy';
import * as iam from '../../aws-iam';
import { IResource, Lazy, Resource, withResolved } from '../../core';
import { IResource, Lazy, Resource, TimeZone, withResolved } from '../../core';

export interface IScalableTarget extends IResource {
/**
Expand Down Expand Up @@ -161,6 +161,7 @@ export class ScalableTarget extends Resource implements IScalableTarget {
maxCapacity: action.maxCapacity,
minCapacity: action.minCapacity,
},
timezone: action.timeZone?.timezoneName,
});
}

Expand Down Expand Up @@ -225,6 +226,14 @@ export interface ScalingSchedule {
* @default No new maximum capacity
*/
readonly maxCapacity?: number;

/**
* The time zone used when referring to the date and time of a scheduled action,
* when the scheduled action uses an at or cron expression.
*
* @default - UTC
*/
readonly timeZone?: TimeZone;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@ describe('scalable target', () => {
});
});

test('set timzone in scaleOnSchedule()', () => {
// GIVEN
const stack = new cdk.Stack();
const target = createScalableTarget(stack);

// WHEN
target.scaleOnSchedule('ScaleUp', {
schedule: appscaling.Schedule.cron({
hour: '8',
day: '1',
}),
maxCapacity: 50,
minCapacity: 1,
timeZone: cdk.TimeZone.AMERICA_DENVER,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApplicationAutoScaling::ScalableTarget', {
ScheduledActions: [
{
ScalableTargetAction: {
MaxCapacity: 50,
MinCapacity: 1,
},
Schedule: 'cron(* 8 1 * ? *)',
ScheduledActionName: 'ScaleUp',
Timezone: 'America/Denver',
},
],
});
});

test('scheduled scaling shows warning when minute is not defined in cron', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 91d94e5

Please sign in to comment.