Skip to content

Commit 193cd3f

Browse files
authored
feat(scheduler): disable Schedule on creation (#27236)
Small change to make it possible to disable a Schedule. It basically works the same as in Rule in aws-events. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent faffce0 commit 193cd3f

13 files changed

+833
-7
lines changed

packages/@aws-cdk/aws-scheduler-alpha/README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ You can choose from three schedule types when configuring your schedule: rate-ba
6060
Both rate-based and cron-based schedules are recurring schedules. You can configure each recurring schedule type using a schedule expression. For
6161
cron-based schedule you can specify a time zone in which EventBridge Scheduler evaluates the expression.
6262

63-
64-
> ScheduleExpression should be used together with class Schedule, which is not yet implemented.
65-
66-
[comment]: <> (TODO: Switch to `ts` once Schedule is implemented)
67-
6863
```ts
6964
declare const target: targets.LambdaInvoke;
7065

@@ -129,6 +124,21 @@ new Schedule(this, 'Schedule', {
129124
});
130125
```
131126

127+
### Disabling Schedules
128+
129+
By default, a schedule will be enabled. You can disable a schedule by setting the `enabled` property to false:
130+
131+
```ts
132+
declare const target: targets.LambdaInvoke;
133+
134+
new Schedule(this, 'Schedule', {
135+
schedule: ScheduleExpression.rate(Duration.minutes(10)),
136+
target: target,
137+
enabled: false,
138+
});
139+
```
140+
141+
132142
## Scheduler Targets
133143

134144
The `@aws-cdk/aws-schedule-targets-alpha` module includes classes that implement the `IScheduleTarget` interface for

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ export interface ScheduleProps {
5757
/**
5858
* The schedule's group.
5959
*
60-
* @deafult - By default a schedule will be associated with the `default` group.
60+
* @default - By default a schedule will be associated with the `default` group.
6161
*/
6262
readonly group?: IGroup;
63+
64+
/**
65+
* Indicates whether the schedule is enabled.
66+
* @default true
67+
*/
68+
readonly enabled?: boolean;
6369
}
6470

6571
/**
@@ -95,6 +101,7 @@ export class Schedule extends Resource implements ISchedule {
95101
scheduleExpression: props.schedule.expressionString,
96102
scheduleExpressionTimezone: props.schedule.timeZone?.timezoneName,
97103
groupName: this.group?.groupName,
104+
state: (props.enabled ?? true) ? 'ENABLED' : 'DISABLED',
98105
target: {
99106
arn: targetConfig.arn,
100107
roleArn: targetConfig.role.roleArn,

packages/@aws-cdk/aws-scheduler-alpha/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
"@aws-cdk/pkglint": "0.0.0",
8787
"@types/jest": "^29.5.5",
8888
"aws-cdk-lib": "0.0.0",
89-
"constructs": "^10.0.0"
89+
"constructs": "^10.0.0",
90+
"@aws-cdk/integ-tests-alpha": "0.0.0"
9091
},
9192
"dependencies": {},
9293
"peerDependencies": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "34.0.0",
3+
"files": {
4+
"bbcd00ec8f22ebae1e8018bdae1226dbbdf643fd8c5453daf208abedd325db6c": {
5+
"source": {
6+
"path": "aws-cdk-scheduler-schedule.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "bbcd00ec8f22ebae1e8018bdae1226dbbdf643fd8c5453daf208abedd325db6c.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
{
2+
"Resources": {
3+
"FunctionServiceRole675BB04A": {
4+
"Type": "AWS::IAM::Role",
5+
"Properties": {
6+
"AssumeRolePolicyDocument": {
7+
"Statement": [
8+
{
9+
"Action": "sts:AssumeRole",
10+
"Effect": "Allow",
11+
"Principal": {
12+
"Service": "lambda.amazonaws.com"
13+
}
14+
}
15+
],
16+
"Version": "2012-10-17"
17+
},
18+
"ManagedPolicyArns": [
19+
{
20+
"Fn::Join": [
21+
"",
22+
[
23+
"arn:",
24+
{
25+
"Ref": "AWS::Partition"
26+
},
27+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
28+
]
29+
]
30+
}
31+
]
32+
}
33+
},
34+
"Function76856677": {
35+
"Type": "AWS::Lambda::Function",
36+
"Properties": {
37+
"Code": {
38+
"ZipFile": "foo"
39+
},
40+
"Handler": "index.handler",
41+
"Role": {
42+
"Fn::GetAtt": [
43+
"FunctionServiceRole675BB04A",
44+
"Arn"
45+
]
46+
},
47+
"Runtime": "nodejs18.x"
48+
},
49+
"DependsOn": [
50+
"FunctionServiceRole675BB04A"
51+
]
52+
},
53+
"Role1ABCC5F0": {
54+
"Type": "AWS::IAM::Role",
55+
"Properties": {
56+
"AssumeRolePolicyDocument": {
57+
"Statement": [
58+
{
59+
"Action": "sts:AssumeRole",
60+
"Effect": "Allow",
61+
"Principal": {
62+
"Service": "scheduler.amazonaws.com"
63+
}
64+
}
65+
],
66+
"Version": "2012-10-17"
67+
}
68+
}
69+
},
70+
"DefaultSchedule597B0B2C": {
71+
"Type": "AWS::Scheduler::Schedule",
72+
"Properties": {
73+
"FlexibleTimeWindow": {
74+
"Mode": "OFF"
75+
},
76+
"ScheduleExpression": "rate(12 hours)",
77+
"State": "ENABLED",
78+
"Target": {
79+
"Arn": {
80+
"Fn::GetAtt": [
81+
"Function76856677",
82+
"Arn"
83+
]
84+
},
85+
"RoleArn": {
86+
"Fn::GetAtt": [
87+
"Role1ABCC5F0",
88+
"Arn"
89+
]
90+
}
91+
}
92+
}
93+
},
94+
"DisabledScheduleA1DF7F0F": {
95+
"Type": "AWS::Scheduler::Schedule",
96+
"Properties": {
97+
"FlexibleTimeWindow": {
98+
"Mode": "OFF"
99+
},
100+
"ScheduleExpression": "rate(12 hours)",
101+
"State": "DISABLED",
102+
"Target": {
103+
"Arn": {
104+
"Fn::GetAtt": [
105+
"Function76856677",
106+
"Arn"
107+
]
108+
},
109+
"RoleArn": {
110+
"Fn::GetAtt": [
111+
"Role1ABCC5F0",
112+
"Arn"
113+
]
114+
}
115+
}
116+
}
117+
}
118+
},
119+
"Parameters": {
120+
"BootstrapVersion": {
121+
"Type": "AWS::SSM::Parameter::Value<String>",
122+
"Default": "/cdk-bootstrap/hnb659fds/version",
123+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
124+
}
125+
},
126+
"Rules": {
127+
"CheckBootstrapVersion": {
128+
"Assertions": [
129+
{
130+
"Assert": {
131+
"Fn::Not": [
132+
{
133+
"Fn::Contains": [
134+
[
135+
"1",
136+
"2",
137+
"3",
138+
"4",
139+
"5"
140+
],
141+
{
142+
"Ref": "BootstrapVersion"
143+
}
144+
]
145+
}
146+
]
147+
},
148+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
149+
}
150+
]
151+
}
152+
}
153+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"34.0.0"}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "34.0.0",
3+
"testCases": {
4+
"integtest-schedule/DefaultTest": {
5+
"stacks": [
6+
"aws-cdk-scheduler-schedule"
7+
],
8+
"assertionStack": "integtest-schedule/DefaultTest/DeployAssert",
9+
"assertionStackName": "integtestscheduleDefaultTestDeployAssert24CB3896"
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "34.0.0",
3+
"files": {
4+
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
5+
"source": {
6+
"path": "integtestscheduleDefaultTestDeployAssert24CB3896.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Parameters": {
3+
"BootstrapVersion": {
4+
"Type": "AWS::SSM::Parameter::Value<String>",
5+
"Default": "/cdk-bootstrap/hnb659fds/version",
6+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
7+
}
8+
},
9+
"Rules": {
10+
"CheckBootstrapVersion": {
11+
"Assertions": [
12+
{
13+
"Assert": {
14+
"Fn::Not": [
15+
{
16+
"Fn::Contains": [
17+
[
18+
"1",
19+
"2",
20+
"3",
21+
"4",
22+
"5"
23+
],
24+
{
25+
"Ref": "BootstrapVersion"
26+
}
27+
]
28+
}
29+
]
30+
},
31+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
32+
}
33+
]
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)