-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsqs.ts
35 lines (31 loc) · 1.15 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
import * as lambda from '@aws-cdk/aws-lambda';
import * as sqs from '@aws-cdk/aws-sqs';
export interface SqsEventSourceProps {
/**
* The largest number of records that AWS Lambda will retrieve from your event
* source at the time of invoking your function. Your function receives an
* event with all the retrieved records.
*
* Valid Range: Minimum value of 1. Maximum value of 10.
*
* @default 10
*/
readonly batchSize?: number;
}
/**
* Use an Amazon SQS queue as an event source for AWS Lambda.
*/
export class SqsEventSource implements lambda.IEventSource {
constructor(readonly queue: sqs.IQueue, private readonly props: SqsEventSourceProps = { }) {
if (this.props.batchSize !== undefined && (this.props.batchSize < 1 || this.props.batchSize > 10)) {
throw new Error(`Maximum batch size must be between 1 and 10 inclusive (given ${this.props.batchSize})`);
}
}
public bind(target: lambda.IFunction) {
target.addEventSourceMapping(`SqsEventSource:${this.queue.node.uniqueId}`, {
batchSize: this.props.batchSize,
eventSourceArn: this.queue.queueArn,
});
this.queue.grantConsumeMessages(target);
}
}