Skip to content

Commit 8694125

Browse files
authored
feat(applicationautoscaling): timezone for ScheduledAction (#29116)
Closes #22645 Closes #27754 Spiritual successor of #27052 Somewhat related to #21181 but that might be another PR down the road. @pahud ✋ Please review. I'm not particularly fond of how `aws-autoscaling` module ([here](https://github.com/aws/aws-cdk/blob/256cca4017a80f8643c5f5a5999a2ce0383eebf0/packages/aws-cdk-lib/aws-autoscaling/lib/scheduled-action.ts#L21)) is not using `cdk.TimeZone` class, hence why used it in this PR instead. I think we should we change `aws-autoscaling` implementation to do the same? It would be a breaking change... and most likely a brand new PR. LMK what you think. ✌️ Also, I may be slightly OCD but I kinda like better `timezone` vs `timeZone`, but I went with latter one to follow what `aws-autoscaling` did. cc-ing @kaizencc for his input too 🙌 ... possibly related to #27105 ### Reason for this change Timezones have been supported in `AWS::ApplicationAutoScaling::ScalableTarget ScheduledAction` for a while now. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-applicationautoscaling-scalabletarget-scheduledaction.html#cfn-applicationautoscaling-scalabletarget-scheduledaction-timezone ### Description of changes Just added the support for `timezones` in `scalableTarget.scaleOnSchedule` ### Description of how you validated changes Added unit tests for this feature. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6fbcce6 commit 8694125

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

packages/aws-cdk-lib/aws-applicationautoscaling/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ The following example scales the fleet out in the morning, and lets natural
177177
scaling take over at night:
178178

179179
```ts
180+
import { TimeZone } from 'aws-cdk-lib';
180181
declare const resource: SomeScalableResource;
181182

182183
const capacity = resource.autoScaleCapacity({
@@ -187,11 +188,13 @@ const capacity = resource.autoScaleCapacity({
187188
capacity.scaleOnSchedule('PrescaleInTheMorning', {
188189
schedule: appscaling.Schedule.cron({ hour: '8', minute: '0' }),
189190
minCapacity: 20,
191+
timeZone: TimeZone.AMERICA_DENVER,
190192
});
191193

192194
capacity.scaleOnSchedule('AllowDownscalingAtNight', {
193195
schedule: appscaling.Schedule.cron({ hour: '20', minute: '0' }),
194-
minCapacity: 1
196+
minCapacity: 1,
197+
timeZone: TimeZone.AMERICA_DENVER,
195198
});
196199
```
197200

@@ -205,7 +208,7 @@ import * as lambda from 'aws-cdk-lib/aws-lambda';
205208
declare const code: lambda.Code;
206209

207210
const handler = new lambda.Function(this, 'MyFunction', {
208-
runtime: lambda.Runtime.PYTHON_3_7,
211+
runtime: lambda.Runtime.PYTHON_3_12,
209212
handler: 'index.handler',
210213
code,
211214

packages/aws-cdk-lib/aws-applicationautoscaling/lib/scalable-target.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Schedule } from './schedule';
44
import { BasicStepScalingPolicyProps, StepScalingPolicy } from './step-scaling-policy';
55
import { BasicTargetTrackingScalingPolicyProps, TargetTrackingScalingPolicy } from './target-tracking-scaling-policy';
66
import * as iam from '../../aws-iam';
7-
import { IResource, Lazy, Resource, withResolved } from '../../core';
7+
import { IResource, Lazy, Resource, TimeZone, withResolved } from '../../core';
88

99
export interface IScalableTarget extends IResource {
1010
/**
@@ -161,6 +161,7 @@ export class ScalableTarget extends Resource implements IScalableTarget {
161161
maxCapacity: action.maxCapacity,
162162
minCapacity: action.minCapacity,
163163
},
164+
timezone: action.timeZone?.timezoneName,
164165
});
165166
}
166167

@@ -225,6 +226,14 @@ export interface ScalingSchedule {
225226
* @default No new maximum capacity
226227
*/
227228
readonly maxCapacity?: number;
229+
230+
/**
231+
* The time zone used when referring to the date and time of a scheduled action,
232+
* when the scheduled action uses an at or cron expression.
233+
*
234+
* @default - UTC
235+
*/
236+
readonly timeZone?: TimeZone;
228237
}
229238

230239
/**

packages/aws-cdk-lib/aws-applicationautoscaling/test/scalable-target.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,38 @@ describe('scalable target', () => {
7979
});
8080
});
8181

82+
test('set timzone in scaleOnSchedule()', () => {
83+
// GIVEN
84+
const stack = new cdk.Stack();
85+
const target = createScalableTarget(stack);
86+
87+
// WHEN
88+
target.scaleOnSchedule('ScaleUp', {
89+
schedule: appscaling.Schedule.cron({
90+
hour: '8',
91+
day: '1',
92+
}),
93+
maxCapacity: 50,
94+
minCapacity: 1,
95+
timeZone: cdk.TimeZone.AMERICA_DENVER,
96+
});
97+
98+
// THEN
99+
Template.fromStack(stack).hasResourceProperties('AWS::ApplicationAutoScaling::ScalableTarget', {
100+
ScheduledActions: [
101+
{
102+
ScalableTargetAction: {
103+
MaxCapacity: 50,
104+
MinCapacity: 1,
105+
},
106+
Schedule: 'cron(* 8 1 * ? *)',
107+
ScheduledActionName: 'ScaleUp',
108+
Timezone: 'America/Denver',
109+
},
110+
],
111+
});
112+
});
113+
82114
test('scheduled scaling shows warning when minute is not defined in cron', () => {
83115
// GIVEN
84116
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)