-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathevent-bus.ts
61 lines (54 loc) · 2 KB
/
event-bus.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
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 { singletonEventRole, addToDeadLetterQueueResourcePolicy } from './util';
/**
* Configuration properties of an Event Bus event
*
* Cannot extend TargetBaseProps. Retry policy is not supported for Event bus targets.
*/
export interface EventBusProps {
/**
* Role to be used to publish the event
*
* @default a new role is created.
*/
readonly role?: iam.IRole;
/**
* The SQS queue to be used as deadLetterQueue.
* Check out the [considerations for using a dead-letter queue](https://docs.aws.amazon.com/eventbridge/latest/userguide/rule-dlq.html#dlq-considerations).
*
* The events not successfully delivered are automatically retried for a specified period of time,
* depending on the retry policy of the target.
* If an event is not delivered before all retry attempts are exhausted, it will be sent to the dead letter queue.
*
* @default - no dead-letter queue
*/
readonly deadLetterQueue?: sqs.IQueue;
}
/**
* Notify an existing Event Bus of an event
*/
export class EventBus implements events.IRuleTarget {
constructor(private readonly eventBus: events.IEventBus, private readonly props: EventBusProps = {}) { }
bind(rule: events.IRule, _id?: string): events.RuleTargetConfig {
if (this.props.role) {
this.props.role.addToPrincipalPolicy(this.putEventStatement());
}
const role = this.props.role ?? singletonEventRole(rule, [this.putEventStatement()]);
if (this.props.deadLetterQueue) {
addToDeadLetterQueueResourcePolicy(rule, this.props.deadLetterQueue);
}
return {
arn: this.eventBus.eventBusArn,
deadLetterConfig: this.props.deadLetterQueue ? { arn: this.props.deadLetterQueue?.queueArn } : undefined,
role,
};
}
private putEventStatement() {
return new iam.PolicyStatement({
actions: ['events:PutEvents'],
resources: [this.eventBus.eventBusArn],
});
}
}