-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueLambda.ts
49 lines (41 loc) · 1.45 KB
/
QueueLambda.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
import * as aws from '@pulumi/aws'
import * as pulumi from '@pulumi/pulumi'
import { LambdaFunctionArgs, LambdaFunction } from './LambdaFunction'
import { SQSProcessPolicy } from './policies'
export interface QueueLambdaArgs extends Omit<LambdaFunctionArgs, 'role'> {
queue: aws.sqs.Queue
queueBatchSize?: number
}
export class QueueLambda extends pulumi.ComponentResource {
readonly queue: aws.sqs.Queue
readonly lambda: LambdaFunction
readonly queuePolicy: SQSProcessPolicy
constructor(name: string, args: QueueLambdaArgs, opts?: pulumi.ComponentResourceOptions) {
super('aws:components:QueueLambda', name, args, opts)
const defaultResourceOptions: pulumi.ResourceOptions = { parent: this }
const { queue, queueBatchSize = 10, environment, ...lambdaArgs } = args
const sqsPolicyName = `${name}-policy-sqs`
this.queuePolicy = new SQSProcessPolicy(sqsPolicyName, { queueArn: queue.arn }, defaultResourceOptions)
this.lambda = new LambdaFunction(
name,
{
...lambdaArgs,
policies: [...(lambdaArgs.policies || []), this.queuePolicy.policy],
environment
},
defaultResourceOptions
)
queue.onEvent(
`${name}-queue-event-subscription`,
this.lambda.lambda,
{
batchSize: queueBatchSize
},
defaultResourceOptions
)
this.queue = queue
this.registerOutputs({
lambda: { name: this.lambda.lambda.name, arn: this.lambda.lambda.arn }
})
}
}