|
| 1 | +import {Construct} from "constructs" |
| 2 | +import {Duration, RemovalPolicy} from "aws-cdk-lib" |
| 3 | +import {Queue, QueueEncryption} from "aws-cdk-lib/aws-sqs" |
| 4 | +import {Key} from "aws-cdk-lib/aws-kms" |
| 5 | +import {SqsEventSource} from "aws-cdk-lib/aws-lambda-event-sources" |
| 6 | +import {LambdaFunction} from "./LambdaFunction" |
| 7 | + |
| 8 | +export interface SimpleQueueServiceProps { |
| 9 | + readonly stackName: string |
| 10 | + readonly queueName: string |
| 11 | + readonly batchDelay: number |
| 12 | + readonly functions: Array<LambdaFunction> |
| 13 | +} |
| 14 | + |
| 15 | +export class SimpleQueueService extends Construct { |
| 16 | + public queue: Queue |
| 17 | + public deadLetterQueue: Queue |
| 18 | + public kmsKey: Key |
| 19 | + |
| 20 | + constructor(scope: Construct, id: string, props: SimpleQueueServiceProps) { |
| 21 | + super(scope, id) |
| 22 | + |
| 23 | + const name = `${props.stackName}-${props.queueName}`.toLocaleLowerCase() |
| 24 | + |
| 25 | + const kmsKey = new Key(this, `${name}-queue-key`, { |
| 26 | + enableKeyRotation: true, |
| 27 | + description: `KMS key for ${props.queueName} queue and dead-letter queue encryption`, |
| 28 | + removalPolicy: RemovalPolicy.DESTROY |
| 29 | + }) |
| 30 | + |
| 31 | + // Create a Dead-Letter Queue (DLQ) for handling failed messages, to help with debugging |
| 32 | + const deadLetterQueue = new Queue(this, `${name}-dlq`, { |
| 33 | + queueName: `${name}-dlq`, |
| 34 | + retentionPeriod: Duration.days(14), // Max 14 |
| 35 | + encryption: QueueEncryption.KMS, |
| 36 | + encryptionMasterKey: kmsKey, |
| 37 | + visibilityTimeout: Duration.seconds(60), |
| 38 | + enforceSSL: true |
| 39 | + }) |
| 40 | + |
| 41 | + // Create the main SQS Queue with DLQ configured |
| 42 | + const queue = new Queue(this, name, |
| 43 | + { |
| 44 | + queueName: name, |
| 45 | + encryption: QueueEncryption.KMS, |
| 46 | + encryptionMasterKey: kmsKey, |
| 47 | + deadLetterQueue: { |
| 48 | + queue: deadLetterQueue, |
| 49 | + maxReceiveCount: 3 // Move to DLQ after 3 failed attempts |
| 50 | + }, |
| 51 | + deliveryDelay: Duration.seconds(0), |
| 52 | + visibilityTimeout: Duration.seconds(60), |
| 53 | + enforceSSL: true |
| 54 | + } |
| 55 | + ) |
| 56 | + |
| 57 | + // Add queues as event source for the notify function and sync knowledge base function |
| 58 | + // While batching, the messages will be sent if maxBatchingWindow is reached or batchSize is reached |
| 59 | + // Set (very) large batch size to improve wait efficiency of batching window |
| 60 | + const eventSource = new SqsEventSource(queue, { |
| 61 | + maxBatchingWindow: Duration.seconds(props.batchDelay), |
| 62 | + batchSize: 1000, |
| 63 | + reportBatchItemFailures: true |
| 64 | + }) |
| 65 | + |
| 66 | + props.functions.forEach(fn => { |
| 67 | + fn.function.addEventSource(eventSource) |
| 68 | + queue.grantConsumeMessages(fn.function) |
| 69 | + }) |
| 70 | + |
| 71 | + // Grant the Lambda function permissions to consume messages from the queue |
| 72 | + |
| 73 | + this.kmsKey = kmsKey |
| 74 | + this.queue = queue |
| 75 | + this.deadLetterQueue = deadLetterQueue |
| 76 | + } |
| 77 | +} |
0 commit comments