-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.ts
128 lines (110 loc) · 4.09 KB
/
component.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { Construct } from 'constructs';
import { Duration } from 'aws-cdk-lib';
import { IEventBus, EventBus } from 'aws-cdk-lib/aws-events';
import { IMessagingProvider, MonitoredQueue } from 'sqs-dlq-monitoring';
import { Topic } from 'aws-cdk-lib/aws-sns';
import { IQueue, Queue, DeadLetterQueue } from 'aws-cdk-lib/aws-sqs';
import { SqsSource } from '@aws-cdk/aws-pipes-sources-alpha';
import * as pipes from '@aws-cdk/aws-pipes-alpha';
import * as chatbot from 'aws-cdk-lib/aws-chatbot';
import * as iam from 'aws-cdk-lib/aws-iam';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
export interface IcaEventPipeProps {
icaEventPipeName: string;
icaQueueName: string;
icaQueueVizTimeout: number;
slackChannelConfigurationName: string;
slackWorkspaceIdSSMParam: string;
slackChannelIdSSMParam: string;
eventBusName: string;
}
export class IcaEventPipeConstruct extends Construct {
readonly icaQueue: IQueue;
readonly mainBus: IEventBus;
constructor(scope: Construct, id: string, props: IcaEventPipeProps) {
super(scope, id);
this.icaQueue = this.createMonitoredQueue(id, props).queue;
this.mainBus = EventBus.fromEventBusName(this, id + 'EventBus', props.eventBusName);
}
private createMonitoredQueue(id: string, props: IcaEventPipeProps) {
const slackWorkspaceId = StringParameter.fromStringParameterName(this, id + 'SlackWorkspaceId', props.slackWorkspaceIdSSMParam).stringValue;
const slackChannelId = StringParameter.fromStringParameterName(this, id + 'slackChannelId', props.slackChannelIdSSMParam).stringValue;
const queue = new Queue(this, id + 'DLQ', {
queueName: props.icaQueueName + '-dlq',
enforceSSL: true,
visibilityTimeout: Duration.seconds(props.icaQueueVizTimeout),
});
const deadLetterQueue: DeadLetterQueue = {
queue: queue,
maxReceiveCount: 3,
};
const mq = new MonitoredQueue(this, props.icaEventPipeName, {
queueProps: {
queueName: props.icaQueueName,
deadLetterQueue: deadLetterQueue,
enforceSSL: true,
visibilityTimeout: Duration.seconds(props.icaQueueVizTimeout),
},
messagingProviders: [
new ChatbotMessageProvider(
props.slackChannelConfigurationName,
slackWorkspaceId,
slackChannelId,
),
],
});
return mq;
}
private createPipe() {
// const inputTransfrom = new pipes.InputTransformation()
return new pipes.Pipe(this, 'Pipe', {
source: new SqsSource(this.icaQueue),
target: new EventBusTarget(this.mainBus),
// target: new EventBusTarget(this.mainBus, {inputTransformation: inputTransfrom}), // TODO implement
});
}
}
class EventBusTarget implements pipes.ITarget {
targetArn: string;
private inputTransformation: pipes.IInputTransformation | undefined;
constructor(
private readonly eventBus: IEventBus,
props: { inputTransformation?: pipes.IInputTransformation } = {}
) {
this.eventBus = eventBus;
this.targetArn = eventBus.eventBusArn;
this.inputTransformation = props?.inputTransformation;
}
bind(_pipe: pipes.Pipe): pipes.TargetConfig {
return {
targetParameters: {
inputTemplate: this.inputTransformation?.bind(_pipe).inputTemplate,
},
};
}
grantPush(pipeRole: iam.IRole): void {
this.eventBus.grantPutEventsTo(pipeRole);
}
}
class ChatbotMessageProvider implements IMessagingProvider {
readonly slackChannelConfigurationName: string;
readonly slackWorkspaceId: string;
readonly slackChannelId: string;
constructor(
slackChannelConfigurationName: string,
slackWorkspaceId: string,
slackChannelId: string
) {
this.slackChannelConfigurationName = slackChannelConfigurationName;
this.slackWorkspaceId = slackWorkspaceId;
this.slackChannelId = slackChannelId;
}
deployProvider(scope: Construct, topic: Topic) {
new chatbot.SlackChannelConfiguration(scope, 'id', {
slackChannelConfigurationName: this.slackChannelConfigurationName,
slackWorkspaceId: this.slackWorkspaceId,
slackChannelId: this.slackChannelId,
notificationTopics: [topic],
});
}
}