Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(applicationautoscaling): timezone for ScheduledAction #29116

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/aws-cdk-lib/aws-applicationautoscaling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ The following example scales the fleet out in the morning, and lets natural
scaling take over at night:

```ts
import { TimeZone } from 'aws-cdk-lib';
declare const resource: SomeScalableResource;

const capacity = resource.autoScaleCapacity({
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 All @@ -205,7 +208,7 @@ import * as lambda from 'aws-cdk-lib/aws-lambda';
declare const code: lambda.Code;

const handler = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.PYTHON_3_7,
runtime: lambda.Runtime.PYTHON_3_12,
handler: 'index.handler',
code,

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