-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNSEventsQueue.ts
80 lines (72 loc) · 2.16 KB
/
SNSEventsQueue.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
import * as aws from '@pulumi/aws'
import * as pulumi from '@pulumi/pulumi'
export interface SNSEventsQueueArgs extends Omit<aws.sqs.QueueArgs, 'name' | 'namePrefix'> {
topic: aws.sns.Topic
/**
* string version of filter policy
*/
filterPolicy?: string
}
/**
* A custom SQS resource to subscribe to SNS events.
*/
export class SNSEventsQueue extends pulumi.ComponentResource {
readonly queue: aws.sqs.Queue
readonly topicSubscription: aws.sns.TopicSubscription
readonly queuePolicy: aws.sqs.QueuePolicy
constructor(name: string, args: SNSEventsQueueArgs, opts?: pulumi.ComponentResourceOptions) {
super('aws:components:SNSEventsQueue', name, args, opts)
const defaultParentOptions: pulumi.ResourceOptions = { parent: this }
const { filterPolicy, topic, ...queueArgs } = args
// Queue
const queueName = name
this.queue = new aws.sqs.Queue(
queueName,
{
...queueArgs,
name: queueName
},
defaultParentOptions
)
// SNS - SQS Subscriptions
const topicSubscriptionName = `${queueName}-topic-subscription`
this.topicSubscription = new aws.sns.TopicSubscription(
topicSubscriptionName,
{
endpoint: this.queue.arn,
protocol: 'sqs',
topic,
rawMessageDelivery: true,
...(filterPolicy ? { filterPolicy } : {})
},
defaultParentOptions
)
const queuePermissionName = `${queueName}-topic-permission`
this.queuePolicy = new aws.sqs.QueuePolicy(
queuePermissionName,
{
queueUrl: this.queue.id,
policy: pulumi.all([topic.arn, this.queue.arn]).apply(([topicArn, queueArn]) =>
JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Principal: '*',
Action: 'SQS:SendMessage',
Resource: [queueArn],
Condition: {
ArnEquals: {
'aws:SourceArn': topicArn
}
}
}
]
})
)
},
defaultParentOptions
)
this.registerOutputs({ queue: this.queue })
}
}