Skip to content

Commit 80c1d26

Browse files
authored
feat(scheduler-targets): SqsSendMessage Target (#27774)
This PR adds `SqsSendMessage` Target for EventBridge Scheduler. Closes #27458. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6004a17 commit 80c1d26

File tree

14 files changed

+35514
-0
lines changed

14 files changed

+35514
-0
lines changed

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

+30
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The following targets are supported:
2727
1. `targets.LambdaInvoke`: [Invoke an AWS Lambda function](#invoke-a-lambda-function))
2828
2. `targets.StepFunctionsStartExecution`: [Start an AWS Step Function](#start-an-aws-step-function)
2929
3. `targets.CodeBuildStartBuild`: [Start a CodeBuild job](#start-a-codebuild-job)
30+
4. `targets.SqsSendMessage`: [Send a Message to an Amazon SQS Queue](#send-a-message-to-sqs-queue)
3031

3132
## Invoke a Lambda function
3233

@@ -121,3 +122,32 @@ new Schedule(this, 'Schedule', {
121122
target: new targets.CodeBuildStartBuild(project),
122123
});
123124
```
125+
126+
## Send A Message To SQS Queue
127+
128+
Use the `SqsSendMessage` target to send a message to SQS Queue.
129+
130+
The code snippet below creates an event rule with a SQS Queue as a target
131+
called every hour by Event Bridge Scheduler with a custom payload.
132+
133+
Contains the `messageGroupId` to use when the target is a FIFO queue. If you specify
134+
a FIFO queue as a target, the queue must have content-based deduplication enabled.
135+
136+
```ts
137+
const payload = 'test';
138+
const messageGroupId = 'id';
139+
const queue = new sqs.Queue(this, 'MyQueue', {
140+
fifo: true,
141+
contentBasedDeduplication: true,
142+
});
143+
144+
const target = new targets.SqsSendMessage(queue, {
145+
input: ScheduleTargetInput.fromText(payload),
146+
messageGroupId,
147+
});
148+
149+
new Schedule(this, 'Schedule', {
150+
schedule: ScheduleExpression.rate(Duration.minutes(1)),
151+
target
152+
});
153+
```

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

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './target';
22
export * from './lambda-invoke';
33
export * from './stepfunctions-start-execution';
44
export * from './codebuild-start-build';
5+
export * from './sqs-send-message';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { ISchedule, IScheduleTarget, ScheduleTargetConfig } from '@aws-cdk/aws-scheduler-alpha';
2+
import { Names, Token } from 'aws-cdk-lib';
3+
import { IRole } from 'aws-cdk-lib/aws-iam';
4+
import * as sqs from 'aws-cdk-lib/aws-sqs';
5+
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6+
import { sameEnvDimension } from './util';
7+
8+
/**
9+
* Properties for a SQS Queue Target
10+
*/
11+
export interface SqsSendMessageProps extends ScheduleTargetBaseProps {
12+
/**
13+
* The FIFO message group ID to use as the target.
14+
*
15+
* This must be specified when the target is a FIFO queue. If you specify
16+
* a FIFO queue as a target, the queue must have content-based deduplication enabled.
17+
*
18+
* A length of `messageGroupId` must be between 1 and 128.
19+
*
20+
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-scheduler-schedule-sqsparameters.html#cfn-scheduler-schedule-sqsparameters-messagegroupid
21+
*
22+
* @default - no message group ID
23+
*/
24+
readonly messageGroupId?: string;
25+
}
26+
27+
/**
28+
* Use an Amazon SQS Queue as a target for AWS EventBridge Scheduler.
29+
*/
30+
export class SqsSendMessage extends ScheduleTargetBase implements IScheduleTarget {
31+
constructor(
32+
private readonly queue: sqs.IQueue,
33+
private readonly props: SqsSendMessageProps,
34+
) {
35+
super(props, queue.queueArn);
36+
37+
if (props.messageGroupId !== undefined) {
38+
if (!Token.isUnresolved(props.messageGroupId) && (props.messageGroupId.length < 1 || props.messageGroupId.length > 128)) {
39+
throw new Error(`messageGroupId length must be between 1 and 128, got ${props.messageGroupId.length}`);
40+
}
41+
if (!queue.fifo) {
42+
throw new Error('target must be a FIFO queue if messageGroupId is specified');
43+
}
44+
if (!(queue.node.defaultChild as sqs.CfnQueue).contentBasedDeduplication) {
45+
throw new Error('contentBasedDeduplication must be true if the target is a FIFO queue');
46+
}
47+
} else if (queue.fifo) {
48+
throw new Error('messageGroupId must be specified if the target is a FIFO queue');
49+
}
50+
}
51+
52+
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
53+
if (!sameEnvDimension(this.queue.env.region, schedule.env.region)) {
54+
throw new Error(`Cannot assign queue in region ${this.queue.env.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the queue must be in the same region.`);
55+
}
56+
57+
if (!sameEnvDimension(this.queue.env.account, schedule.env.account)) {
58+
throw new Error(`Cannot assign queue in account ${this.queue.env.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the queue must be in the same account.`);
59+
}
60+
61+
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.queue.env.account)) {
62+
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.queue.node)} in account ${this.queue.env.account}. Both the target and the execution role must be in the same account.`);
63+
}
64+
65+
this.queue.grantSendMessages(role);
66+
}
67+
68+
protected bindBaseTargetConfig(_schedule: ISchedule): ScheduleTargetConfig {
69+
return {
70+
...super.bindBaseTargetConfig(_schedule),
71+
sqsParameters: {
72+
messageGroupId: this.props.messageGroupId,
73+
},
74+
};
75+
}
76+
}

0 commit comments

Comments
 (0)