-
Notifications
You must be signed in to change notification settings - Fork 4k
/
sqs.ts
78 lines (68 loc) · 2.46 KB
/
sqs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import * as events from '@aws-cdk/aws-events';
import * as iam from '@aws-cdk/aws-iam';
import * as sqs from '@aws-cdk/aws-sqs';
import { addToDeadLetterQueueResourcePolicy, TargetBaseProps, bindBaseTargetConfig } from './util';
/**
* Customize the SQS Queue Event Target
*/
export interface SqsQueueProps extends TargetBaseProps {
/**
* Message Group ID for messages sent to this queue
*
* Required for FIFO queues, leave empty for regular queues.
*
* @default - no message group ID (regular queue)
*/
readonly messageGroupId?: string;
/**
* The message to send to the queue.
*
* Must be a valid JSON text passed to the target queue.
*
* @default the entire EventBridge event
*/
readonly message?: events.RuleTargetInput;
}
/**
* Use an SQS Queue as a target for Amazon EventBridge rules.
*
* @example
* /// fixture=withRepoAndSqsQueue
* // publish to an SQS queue every time code is committed
* // to a CodeCommit repository
* repository.onCommit('onCommit', { target: new targets.SqsQueue(queue) });
*
*/
export class SqsQueue implements events.IRuleTarget {
constructor(public readonly queue: sqs.IQueue, private readonly props: SqsQueueProps = {}) {
if (props.messageGroupId !== undefined && !queue.fifo) {
throw new Error('messageGroupId cannot be specified for non-FIFO queues');
}
}
/**
* Returns a RuleTarget that can be used to trigger this SQS queue as a
* result from an EventBridge event.
*
* @see https://docs.aws.amazon.com/eventbridge/latest/userguide/resource-based-policies-eventbridge.html#sqs-permissions
*/
public bind(rule: events.IRule, _id?: string): events.RuleTargetConfig {
// Only add the rule as a condition if the queue is not encrypted, to avoid circular dependency. See issue #11158.
const principalOpts = this.queue.encryptionMasterKey ? {} : {
conditions: {
ArnEquals: { 'aws:SourceArn': rule.ruleArn },
},
};
// deduplicated automatically
this.queue.grantSendMessages(new iam.ServicePrincipal('events.amazonaws.com', principalOpts));
if (this.props.deadLetterQueue) {
addToDeadLetterQueueResourcePolicy(rule, this.props.deadLetterQueue);
}
return {
...bindBaseTargetConfig(this.props),
arn: this.queue.queueArn,
input: this.props.message,
targetResource: this.queue,
sqsParameters: this.props.messageGroupId ? { messageGroupId: this.props.messageGroupId } : undefined,
};
}
}