Skip to content

Commit d0f9688

Browse files
feat(scheduler): validate schedule name length (#31200)
### Issue # (if applicable) N/A ### Reason for this change To catch name too long errors before hitting Cloudformation. ### Description of changes I've added a length assertion in the `Schedule` constructor against the schedule name length, the documentation on schedule name already makes it clear that there is a limit though AFAICS this isn't validated. Also see the Cloudformation doc here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-scheduler-schedule.html#cfn-scheduler-schedule-name. ### Description of how you validated changes I've added a unit test. ### 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 c1d7782 commit d0f9688

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Diff for: packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Duration, IResource, Resource } from 'aws-cdk-lib';
1+
import { Duration, IResource, Resource, Token } from 'aws-cdk-lib';
22
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
33
import * as kms from 'aws-cdk-lib/aws-kms';
44
import { CfnSchedule } from 'aws-cdk-lib/aws-scheduler';
@@ -324,6 +324,9 @@ export class Schedule extends Resource implements ISchedule {
324324
const flexibleTimeWindow = props.timeWindow ?? TimeWindow.off();
325325

326326
this.validateTimeFrame(props.start, props.end);
327+
if (props.scheduleName && !Token.isUnresolved(props.scheduleName) && props.scheduleName.length > 64) {
328+
throw new Error(`scheduleName cannot be longer than 64 characters, got: ${props.scheduleName.length}`);
329+
}
327330

328331
const resource = new CfnSchedule(this, 'Resource', {
329332
name: this.physicalName,

Diff for: packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ describe('Schedule', () => {
215215
}).toThrow('The provided duration must be between 1 minute and 1440 minutes, got 0');
216216
});
217217

218+
test('throw error when scheduleName exceeds 64 characters', () => {
219+
const name = 'an-extremely-unnecessarily-long-name-exceeding-64-characters-in-length';
220+
expect(() => {
221+
new Schedule(stack, 'TestSchedule', {
222+
schedule: expr,
223+
target: new SomeLambdaTarget(func, role),
224+
scheduleName: name,
225+
});
226+
}).toThrow(`scheduleName cannot be longer than 64 characters, got: ${name.length}`);
227+
});
228+
218229
test('schedule with description', () => {
219230
// WHEN
220231
new Schedule(stack, 'TestSchedule', {

0 commit comments

Comments
 (0)