-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqs.ts
68 lines (58 loc) · 2.72 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
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
import { RemovalPolicy, StackProps, aws_logs as logs } from 'aws-cdk-lib';
import { LambdaIntegration, RestApi } from 'aws-cdk-lib/aws-apigateway';
import { Architecture, Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
import { SqsEventSource } from 'aws-cdk-lib/aws-lambda-event-sources';
import { Queue } from 'aws-cdk-lib/aws-sqs';
import { AwsStack } from './aws-cdk-js-dev-guide-stack';
export class SQSComponents {
constructor(stack: AwsStack, id: string, props?: StackProps, customOptions?: any) {
const sqsQueue = new Queue(stack, 'sqs-queue');
const queuePublishFunction = new Function(stack, 'sqs-function-publish', {
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
handler: 'index.publish',
code: Code.fromAsset('./handlers/sqs'),
environment: {
...stack.cors.corsEnvironment,
QUEUE_URL: sqsQueue.queueUrl,
TABLE_NAME: stack.dynamodbTable.tableName
},
layers: [stack.layer],
logGroup: new logs.LogGroup(stack, `sqs-function-publish-logs`, {
logGroupName: `${id}-sqs-function-publish`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS,
}),
});
sqsQueue.grantSendMessages(queuePublishFunction);
const queueSubscribeFunction = new Function(stack, 'sqs-function-subscribe', {
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
handler: 'index.subscribe',
code: Code.fromAsset('./handlers/sqs'),
environment: {
QUEUE_URL: sqsQueue.queueUrl,
TABLE_NAME: stack.dynamodbTable.tableName
},
layers: [stack.layer],
logGroup: new logs.LogGroup(stack, `sqs-function-subscribe-logs`, {
logGroupName: `${id}-sqs-function-subscribe`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS,
}),
});
// set up a queue event listener
let queueEventSource = new SqsEventSource(sqsQueue, {
batchSize: 10 // default
});
queueSubscribeFunction.addEventSource(queueEventSource);
stack.dynamodbTable.grantWriteData(queueSubscribeFunction);
// sqs api
const sqsApi = new RestApi(stack, 'sqs-api', {
defaultCorsPreflightOptions: stack.cors.corsOptions
});
sqsApi.root.addMethod('POST', new LambdaIntegration(queuePublishFunction));
// reuse dynamodb scan function
sqsApi.root.addMethod('GET', stack.dynamodbScanFunctionIntegration);
}
};