From 0b4ab1d0ba11b3536a2f7b02b537966de6ac0493 Mon Sep 17 00:00:00 2001 From: sakurai-ryo <58683719+sakurai-ryo@users.noreply.github.com> Date: Thu, 14 Dec 2023 03:41:38 +0900 Subject: [PATCH] feat(scheduler): start and end time for schedule construct (#28306) This PR added support for start and end time of the schedule. ## Description Currently, users cannot set a start time and an end time for the schedule. A schedule without a start date will begin as soon as it is created and available, and without an end date, it will continue to invoke its target indefinitely. With this feature, users can set the start and end dates of a schedule, allowing for more flexible schedule configurations. https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-scheduler-schedule.html#cfn-scheduler-schedule-startdate https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-scheduler-schedule.html#cfn-scheduler-schedule-enddate In CloudFormation, users can use this feature as follows: ```yaml TestSchedule: Type: AWS::Scheduler::Schedule Properties: StartDate: "2024-12-01T13:09:00.000Z" EndDate: "2025-12-01T00:00:00.001Z" ScheduleExpression: "at(2023-01-01T00:00:00)" State: "ENABLED" Target: # target ``` ## Major changes ### add property to ScheduleProps interface Added startDate and endDate properties, and typed these values as string based on the following PR comments. https://github.com/aws/aws-cdk/pull/26819#discussion_r1301532299 It is not necessary to specify both startDate and endDate, they can be set independently. Validation is performed on the following points. - Error if not following ISO 8601 format. - Must include milliseconds (yyyy-MM-ddTHH:mm:ss.SSSZ) - Error if startDate is later than endDate If a time before the current time is specified, the following error occurs in CFn, but no validation is performed because the timing of validation in CDK and the timing of actual deployment are different. `The StartDate you specify cannot be earlier than 5 minutes ago.` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../@aws-cdk/aws-scheduler-alpha/README.md | 14 ++++ .../aws-scheduler-alpha/lib/schedule.ts | 28 +++++++- .../aws-cdk-scheduler-schedule.assets.json | 6 +- .../aws-cdk-scheduler-schedule.template.json | 32 +++++++++ .../test/integ.schedule.js.snapshot/cdk.out | 2 +- .../integ.schedule.js.snapshot/integ.json | 2 +- ...efaultTestDeployAssert24CB3896.assets.json | 2 +- .../integ.schedule.js.snapshot/manifest.json | 10 ++- .../test/integ.schedule.js.snapshot/tree.json | 70 ++++++++++++++++--- .../test/integ.schedule.ts | 8 +++ .../aws-scheduler-alpha/test/schedule.test.ts | 35 +++++++++- 11 files changed, 189 insertions(+), 20 deletions(-) diff --git a/packages/@aws-cdk/aws-scheduler-alpha/README.md b/packages/@aws-cdk/aws-scheduler-alpha/README.md index d4870ce51a5e1..f4840acc8362c 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/README.md +++ b/packages/@aws-cdk/aws-scheduler-alpha/README.md @@ -138,6 +138,20 @@ new Schedule(this, 'Schedule', { }); ``` +### Configuring a start and end time of the Schedule + +If you choose a recurring schedule, you can set the start and end time of the Schedule by specifying the `start` and `end`. + +```ts +declare const target: targets.LambdaInvoke; + +new Schedule(this, 'Schedule', { + schedule: ScheduleExpression.rate(cdk.Duration.hours(12)), + target: target, + start: new Date('2023-01-01T00:00:00.000Z'), + end: new Date('2023-02-01T00:00:00.000Z'), +}); +``` ## Scheduler Targets diff --git a/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts b/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts index 47286a81c9b33..8fefbe068a5b5 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts +++ b/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts @@ -114,6 +114,22 @@ export interface ScheduleProps { * @default - All events in Scheduler are encrypted with a key that AWS owns and manages. */ readonly key?: kms.IKey; + + /** + * The date, in UTC, after which the schedule can begin invoking its target. + * EventBridge Scheduler ignores start for one-time schedules. + * + * @default - no value + */ + readonly start?: Date; + + /** + * The date, in UTC, before which the schedule can invoke its target. + * EventBridge Scheduler ignores end for one-time schedules. + * + * @default - no value + */ + readonly end?: Date; } /** @@ -254,6 +270,8 @@ export class Schedule extends Resource implements ISchedule { this.retryPolicy = targetConfig.retryPolicy; + this.validateTimeFrame(props.start, props.end); + const resource = new CfnSchedule(this, 'Resource', { name: this.physicalName, flexibleTimeWindow: { mode: 'OFF' }, @@ -276,6 +294,8 @@ export class Schedule extends Resource implements ISchedule { sageMakerPipelineParameters: targetConfig.sageMakerPipelineParameters, sqsParameters: targetConfig.sqsParameters, }, + startDate: props.start?.toISOString(), + endDate: props.end?.toISOString(), }); this.scheduleName = this.getResourceNameAttribute(resource.ref); @@ -306,4 +326,10 @@ export class Schedule extends Resource implements ISchedule { const isEmptyPolicy = Object.values(policy).every(value => value === undefined); return !isEmptyPolicy ? policy : undefined; } -} \ No newline at end of file + + private validateTimeFrame(start?: Date, end?: Date) { + if (start && end && start >= end) { + throw new Error(`start must precede end, got start: ${start.toISOString()}, end: ${end.toISOString()}`); + } + } +} diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/aws-cdk-scheduler-schedule.assets.json b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/aws-cdk-scheduler-schedule.assets.json index e8847c9f5c7ca..03dd7cb96ad07 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/aws-cdk-scheduler-schedule.assets.json +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/aws-cdk-scheduler-schedule.assets.json @@ -1,7 +1,7 @@ { - "version": "34.0.0", + "version": "35.0.0", "files": { - "a512067604698fe41cacf63c82484e8e597c04456ac3f27ded0a390ca25f0908": { + "77d06d03c78dc7776966b7c7ee414cc19be012ccfba5d7a9b1e425718920ab3e": { "source": { "path": "aws-cdk-scheduler-schedule.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "a512067604698fe41cacf63c82484e8e597c04456ac3f27ded0a390ca25f0908.json", + "objectKey": "77d06d03c78dc7776966b7c7ee414cc19be012ccfba5d7a9b1e425718920ab3e.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/aws-cdk-scheduler-schedule.template.json b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/aws-cdk-scheduler-schedule.template.json index 4bbea65f69deb..5cdf90fef2ef6 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/aws-cdk-scheduler-schedule.template.json +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/aws-cdk-scheduler-schedule.template.json @@ -348,6 +348,38 @@ } } } + }, + "ScheduleWithTimeFrameC1C8BDCC": { + "Type": "AWS::Scheduler::Schedule", + "Properties": { + "EndDate": "2025-10-01T00:00:00.000Z", + "FlexibleTimeWindow": { + "Mode": "OFF" + }, + "ScheduleExpression": "rate(12 hours)", + "ScheduleExpressionTimezone": "Etc/UTC", + "StartDate": "2024-04-15T06:30:00.000Z", + "State": "ENABLED", + "Target": { + "Arn": { + "Fn::GetAtt": [ + "Function76856677", + "Arn" + ] + }, + "Input": "\"Input Text\"", + "RetryPolicy": { + "MaximumEventAgeInSeconds": 180, + "MaximumRetryAttempts": 3 + }, + "RoleArn": { + "Fn::GetAtt": [ + "Role1ABCC5F0", + "Arn" + ] + } + } + } } }, "Parameters": { diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/cdk.out b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/cdk.out index 2313ab5436501..c5cb2e5de6344 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/cdk.out +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/cdk.out @@ -1 +1 @@ -{"version":"34.0.0"} \ No newline at end of file +{"version":"35.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/integ.json b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/integ.json index c1aec1a40f53f..6728e425e3c98 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/integ.json +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/integ.json @@ -1,5 +1,5 @@ { - "version": "34.0.0", + "version": "35.0.0", "testCases": { "integtest-schedule/DefaultTest": { "stacks": [ diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/integtestscheduleDefaultTestDeployAssert24CB3896.assets.json b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/integtestscheduleDefaultTestDeployAssert24CB3896.assets.json index 8f8a003c1b5ba..98271e1ade15f 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/integtestscheduleDefaultTestDeployAssert24CB3896.assets.json +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/integtestscheduleDefaultTestDeployAssert24CB3896.assets.json @@ -1,5 +1,5 @@ { - "version": "34.0.0", + "version": "35.0.0", "files": { "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { "source": { diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/manifest.json b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/manifest.json index 9f07a82776a8b..4fbc5e0eb40aa 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/manifest.json +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/manifest.json @@ -1,5 +1,5 @@ { - "version": "34.0.0", + "version": "35.0.0", "artifacts": { "aws-cdk-scheduler-schedule.assets": { "type": "cdk:asset-manifest", @@ -18,7 +18,7 @@ "validateOnSynth": false, "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", - "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/a512067604698fe41cacf63c82484e8e597c04456ac3f27ded0a390ca25f0908.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/77d06d03c78dc7776966b7c7ee414cc19be012ccfba5d7a9b1e425718920ab3e.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -118,6 +118,12 @@ "data": "CustomerKmsSchedule12B1FEFE" } ], + "/aws-cdk-scheduler-schedule/ScheduleWithTimeFrame/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "ScheduleWithTimeFrameC1C8BDCC" + } + ], "/aws-cdk-scheduler-schedule/BootstrapVersion": [ { "type": "aws:cdk:logicalId", diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/tree.json b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/tree.json index d503798aa5ec7..d99f1d9237516 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/tree.json +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/tree.json @@ -211,7 +211,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-scheduler-alpha.Group", "version": "0.0.0" } }, @@ -235,7 +235,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-scheduler-alpha.Group", "version": "0.0.0" } }, @@ -283,7 +283,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-scheduler-alpha.Schedule", "version": "0.0.0" } }, @@ -332,7 +332,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-scheduler-alpha.Schedule", "version": "0.0.0" } }, @@ -381,7 +381,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-scheduler-alpha.Schedule", "version": "0.0.0" } }, @@ -429,7 +429,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-scheduler-alpha.Schedule", "version": "0.0.0" } }, @@ -477,7 +477,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-scheduler-alpha.Schedule", "version": "0.0.0" } }, @@ -612,7 +612,57 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "@aws-cdk/aws-scheduler-alpha.Schedule", + "version": "0.0.0" + } + }, + "ScheduleWithTimeFrame": { + "id": "ScheduleWithTimeFrame", + "path": "aws-cdk-scheduler-schedule/ScheduleWithTimeFrame", + "children": { + "Resource": { + "id": "Resource", + "path": "aws-cdk-scheduler-schedule/ScheduleWithTimeFrame/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Scheduler::Schedule", + "aws:cdk:cloudformation:props": { + "endDate": "2025-10-01T00:00:00.000Z", + "flexibleTimeWindow": { + "mode": "OFF" + }, + "scheduleExpression": "rate(12 hours)", + "scheduleExpressionTimezone": "Etc/UTC", + "startDate": "2024-04-15T06:30:00.000Z", + "state": "ENABLED", + "target": { + "arn": { + "Fn::GetAtt": [ + "Function76856677", + "Arn" + ] + }, + "roleArn": { + "Fn::GetAtt": [ + "Role1ABCC5F0", + "Arn" + ] + }, + "input": "\"Input Text\"", + "retryPolicy": { + "maximumEventAgeInSeconds": 180, + "maximumRetryAttempts": 3 + } + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_scheduler.CfnSchedule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-scheduler-alpha.Schedule", "version": "0.0.0" } }, @@ -651,7 +701,7 @@ "path": "integtest-schedule/DefaultTest/Default", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.2.70" + "version": "10.3.0" } }, "DeployAssert": { @@ -697,7 +747,7 @@ "path": "Tree", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.2.70" + "version": "10.3.0" } } }, diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.ts b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.ts index 51ff3089c8a5d..59a16c5d89fa2 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.ts +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.ts @@ -90,6 +90,14 @@ new scheduler.Schedule(stack, 'CustomerKmsSchedule', { key, }); +const currentYear = new Date().getFullYear(); +new scheduler.Schedule(stack, 'ScheduleWithTimeFrame', { + schedule: expression, + target: target, + start: new Date(`${currentYear + 1}-04-15T06:30:00.000Z`), + end: new Date(`${currentYear + 2}-10-01T00:00:00.000Z`), +}); + new IntegTest(app, 'integtest-schedule', { testCases: [stack], }); diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts b/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts index 004e9934e281e..b931769fc660b 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts @@ -128,4 +128,37 @@ describe('Schedule', () => { }, }); }); -}); \ No newline at end of file + + describe('schedule timeFrame', () => { + test.each([ + { StartDate: '2023-04-15T06:20:00.000Z', EndDate: '2023-10-01T00:00:00.000Z' }, + { StartDate: '2023-04-15T06:25:00.000Z' }, + { EndDate: '2023-10-01T00:00:00.000Z' }, + ])('schedule can set start and end', (timeFrame) => { + new Schedule(stack, 'TestSchedule', { + schedule: expr, + target: new SomeLambdaTarget(func, role), + start: timeFrame.StartDate ? new Date(timeFrame.StartDate) : undefined, + end: timeFrame.EndDate ? new Date(timeFrame.EndDate) : undefined, + }); + + Template.fromStack(stack).hasResourceProperties('AWS::Scheduler::Schedule', { + ...timeFrame, + }); + }); + + test.each([ + { start: '2023-10-01T00:00:00.000Z', end: '2023-10-01T00:00:00.000Z' }, + { start: '2023-10-01T00:00:00.000Z', end: '2023-09-01T00:00:00.000Z' }, + ])('throw error when start does not come before end', ({ start, end }) => { + expect(() => { + new Schedule(stack, 'TestSchedule', { + schedule: expr, + target: new SomeLambdaTarget(func, role), + start: new Date(start), + end: new Date(end), + }); + }).toThrow(`start must precede end, got start: ${start}, end: ${end}`); + }); + }); +});