Skip to content

Commit

Permalink
feat(scheduler): validate schedule name length (#31200)
Browse files Browse the repository at this point in the history
### 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*
  • Loading branch information
LaurenceWarne authored Sep 4, 2024
1 parent c1d7782 commit d0f9688
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Duration, IResource, Resource } from 'aws-cdk-lib';
import { Duration, IResource, Resource, Token } from 'aws-cdk-lib';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
import * as kms from 'aws-cdk-lib/aws-kms';
import { CfnSchedule } from 'aws-cdk-lib/aws-scheduler';
Expand Down Expand Up @@ -324,6 +324,9 @@ export class Schedule extends Resource implements ISchedule {
const flexibleTimeWindow = props.timeWindow ?? TimeWindow.off();

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

const resource = new CfnSchedule(this, 'Resource', {
name: this.physicalName,
Expand Down
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ describe('Schedule', () => {
}).toThrow('The provided duration must be between 1 minute and 1440 minutes, got 0');
});

test('throw error when scheduleName exceeds 64 characters', () => {
const name = 'an-extremely-unnecessarily-long-name-exceeding-64-characters-in-length';
expect(() => {
new Schedule(stack, 'TestSchedule', {
schedule: expr,
target: new SomeLambdaTarget(func, role),
scheduleName: name,
});
}).toThrow(`scheduleName cannot be longer than 64 characters, got: ${name.length}`);
});

test('schedule with description', () => {
// WHEN
new Schedule(stack, 'TestSchedule', {
Expand Down

0 comments on commit d0f9688

Please sign in to comment.