Skip to content

Commit 39cbd46

Browse files
authored
feat(scheduler): base target methods and lambda invoke target (#26575)
This PR contains implementation for Schedule Targets: 1. Creates a separate module for targets 2. Support imported resources, but not cross account, cross region resources as we discussed in RFC. The unit tests should cover 4 cases (target and role within the same stack, target is imported, role is imported, target and role are imported), 3. I have moved out class `Schedule` from private package to depend on it in `schedule-targets` unit tests. Implementation is based on RFC: https://github.com/aws/aws-cdk-rfcs/blob/master/text/0474-event-bridge-scheduler-l2.md Advances #23394 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9cb395c commit 39cbd46

28 files changed

+1607
-247
lines changed

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

+77-27
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@ This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aw
3737
3838
## Defining a schedule
3939

40-
TODO: Schedule is not yet fully implemented. See section in [L2 Event Bridge Scheduler RFC](https://github.com/aws/aws-cdk-rfcs/blob/master/text/0474-event-bridge-scheduler-l2.md)
41-
42-
[comment]: <> (TODO: change for each PR that implements more functionality)
40+
```ts
41+
declare const fn: lambda.Function;
4342

44-
Only an L2 class is created that wraps the L1 class and handles the following properties:
43+
const target = new targets.LambdaInvoke(fn, {
44+
input: ScheduleTargetInput.fromObject({
45+
"payload": "useful",
46+
}),
47+
});
4548

46-
- schedule
47-
- schedule group
48-
- target (only LambdaInvoke is supported for now)
49-
- flexibleTimeWindow will be set to `{ mode: 'OFF' }`
49+
const schedule = new Schedule(this, 'Schedule', {
50+
schedule: ScheduleExpression.rate(Duration.minutes(10)),
51+
target,
52+
description: 'This is a test schedule that invokes lambda function every 10 minutes.',
53+
});
54+
```
5055

5156
### Schedule Expressions
5257

@@ -60,15 +65,17 @@ cron-based schedule you can specify a time zone in which EventBridge Scheduler e
6065
6166
[comment]: <> (TODO: Switch to `ts` once Schedule is implemented)
6267

63-
```text
68+
```ts
69+
declare const target: targets.LambdaInvoke;
70+
6471
const rateBasedSchedule = new Schedule(this, 'Schedule', {
65-
scheduleExpression: ScheduleExpression.rate(Duration.minutes(10)),
72+
schedule: ScheduleExpression.rate(Duration.minutes(10)),
6673
target,
6774
description: 'This is a test rate-based schedule',
6875
});
6976

7077
const cronBasedSchedule = new Schedule(this, 'Schedule', {
71-
scheduleExpression: ScheduleExpression.cron({
78+
schedule: ScheduleExpression.cron({
7279
minute: '0',
7380
hour: '23',
7481
day: '20',
@@ -85,9 +92,11 @@ and time zone in which EventBridge Scheduler evaluates the schedule.
8592

8693
[comment]: <> (TODO: Switch to `ts` once Schedule is implemented)
8794

88-
```text
95+
```ts
96+
declare const target: targets.LambdaInvoke;
97+
8998
const oneTimeSchedule = new Schedule(this, 'Schedule', {
90-
scheduleExpression: ScheduleExpression.at(
99+
schedule: ScheduleExpression.at(
91100
new Date(2022, 10, 20, 19, 20, 23),
92101
TimeZone.AMERICA_NEW_YORK,
93102
),
@@ -100,35 +109,32 @@ const oneTimeSchedule = new Schedule(this, 'Schedule', {
100109

101110
Your AWS account comes with a default scheduler group. You can access default group in CDK with:
102111

103-
```text
112+
```ts
104113
const defaultGroup = Group.fromDefaultGroup(this, "DefaultGroup");
105114
```
106115

107116
If not specified a schedule is added to the default group. However, you can also add the schedule to a custom scheduling group managed by you:
108117

109-
```text
118+
```ts
119+
declare const target: targets.LambdaInvoke;
120+
110121
const group = new Group(this, "Group", {
111122
groupName: "MyGroup",
112123
});
113124

114-
const target = new targets.LambdaInvoke(props.func, {
115-
input: ScheduleTargetInput.fromObject({
116-
"payload": "useful",
117-
}),
118-
});
119-
120125
new Schedule(this, 'Schedule', {
121-
scheduleExpression: ScheduleExpression.rate(Duration.minutes(10)),
126+
schedule: ScheduleExpression.rate(Duration.minutes(10)),
122127
target,
123128
group,
124129
});
125130
```
126131

127132
## Scheduler Targets
128133

129-
TODO: Scheduler Targets Module is not yet implemented. See section in [L2 Event Bridge Scheduler RFC](https://github.com/aws/aws-cdk-rfcs/blob/master/text/0474-event-bridge-scheduler-l2.md)
130-
131-
Only LambdaInvoke target is added for now.
134+
The `@aws-cdk/aws-schedule-targets-alpha` module includes classes that implement the `IScheduleTarget` interface for
135+
various AWS services. EventBridge Scheduler supports two types of targets: templated targets invoke common API
136+
operations across a core groups of services, and customizeable universal targets that you can use to call more
137+
than 6,000 operations across over 270 services. A list of supported targets can be found at `@aws-cdk/aws-schedule-targets-alpha`.
132138

133139
### Input
134140

@@ -156,7 +162,28 @@ const input = ScheduleTargetInput.fromText(text);
156162

157163
### Specifying Execution Role
158164

159-
TODO: Not yet implemented. See section in [L2 Event Bridge Scheduler RFC](https://github.com/aws/aws-cdk-rfcs/blob/master/text/0474-event-bridge-scheduler-l2.md)
165+
An execution role is an IAM role that EventBridge Scheduler assumes in order to interact with other AWS services on your behalf.
166+
167+
The classes for templated schedule targets automatically create an IAM role with all the minimum necessary
168+
permissions to interact with the templated target. If you wish you may specify your own IAM role, then the templated targets
169+
will grant minimal required permissions. For example: for invoking Lambda function target `LambdaInvoke` will grant
170+
execution IAM role permission to `lambda:InvokeFunction`.
171+
172+
```ts
173+
declare const fn: lambda.Function;
174+
175+
const role = new iam.Role(this, 'Role', {
176+
assumedBy: new iam.ServicePrincipal('scheduler.amazonaws.com'),
177+
});
178+
179+
const target = new targets.LambdaInvoke(fn, {
180+
input: ScheduleTargetInput.fromObject({
181+
"payload": "useful"
182+
}),
183+
role,
184+
});
185+
```
186+
160187

161188
### Cross-account and cross-region targets
162189

@@ -168,7 +195,30 @@ TODO: Not yet implemented. See section in [L2 Event Bridge Scheduler RFC](https:
168195

169196
## Error-handling
170197

171-
TODO: Not yet implemented. See section in [L2 Event Bridge Scheduler RFC](https://github.com/aws/aws-cdk-rfcs/blob/master/text/0474-event-bridge-scheduler-l2.md)
198+
You can configure how your schedule handles failures, when EventBridge Scheduler is unable to deliver an event
199+
successfully to a target, by using two primary mechanisms: a retry policy, and a dead-letter queue (DLQ).
200+
201+
A retry policy determines the number of times EventBridge Scheduler must retry a failed event, and how long
202+
to keep an unprocessed event.
203+
204+
A DLQ is a standard Amazon SQS queue EventBridge Scheduler uses to deliver failed events to, after the retry
205+
policy has been exhausted. You can use a DLQ to troubleshoot issues with your schedule or its downstream target.
206+
If you've configured a retry policy for your schedule, EventBridge Scheduler delivers the dead-letter event after
207+
exhausting the maximum number of retries you set in the retry policy.
208+
209+
```ts
210+
declare const fn: lambda.Function;
211+
212+
const dlq = new sqs.Queue(this, "DLQ", {
213+
queueName: 'MyDLQ',
214+
});
215+
216+
const target = new targets.LambdaInvoke(fn, {
217+
deadLetterQueue: dlq,
218+
maxEventAge: Duration.minutes(1),
219+
retryAttempts: 3
220+
});
221+
```
172222

173223
## Overriding Target Properties
174224

packages/@aws-cdk/aws-scheduler-alpha/jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ module.exports = {
77
branches: 60,
88
},
99
},
10-
};;
10+
};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './schedule-expression';
22
export * from './input';
33
export * from './schedule';
4-
export * from './group';
4+
export * from './group';
5+
export * from './target';

packages/@aws-cdk/aws-scheduler-alpha/lib/private/index.ts

-2
This file was deleted.

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

-70
This file was deleted.

packages/@aws-cdk/aws-scheduler-alpha/lib/private/targets.ts

-58
This file was deleted.

0 commit comments

Comments
 (0)