Last active
July 24, 2024 02:01
-
-
Save brianfoody/702303682e4e5e60ea5743aad52641a5 to your computer and use it in GitHub Desktop.
Dead Letter Queue example with CDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Queue } from "@aws-cdk/aws-sqs"; | |
import { App, Duration, Stack, StackProps } from "@aws-cdk/core"; | |
import { Runtime, Code, Function } from "@aws-cdk/aws-lambda"; | |
import { SqsEventSource } from "@aws-cdk/aws-lambda-event-sources"; | |
class DeadLetterQueue extends Stack { | |
constructor(parent: App, name: string, props?: StackProps) { | |
super(parent, name, props); | |
// Send things to me if you don't know what to do with it. | |
const deadLetterQueue = new Queue(this, "eventSequencingDLQueue", { | |
queueName: "dlq.fifo", | |
deliveryDelay: Duration.millis(0), | |
contentBasedDeduplication: true, | |
retentionPeriod: Duration.days(14), | |
fifo: true | |
}); | |
// Send messages to me and I'll make sure I hand them to the processor in order. | |
const fifoQueue = new Queue(this, "eventSequencingFifoQueue", { | |
queueName: "mailbox.fifo", | |
deliveryDelay: Duration.millis(0), | |
contentBasedDeduplication: true, | |
fifo: true, | |
visibilityTimeout: Duration.seconds(30), | |
deadLetterQueue: { | |
maxReceiveCount: 1, | |
queue: deadLetterQueue | |
} | |
}); | |
// I'm going to send messages. | |
const sender = new Function(this, "senderFn", { | |
runtime: Runtime.NODEJS_10_X, | |
code: Code.asset("./lib/src"), | |
handler: "v2/sender/sender.main", | |
timeout: Duration.seconds(6), | |
memorySize: 256, | |
reservedConcurrentExecutions: 1, | |
environment: { | |
FIFO_QUEUE_URL: fifoQueue.queueUrl | |
} | |
}); | |
fifoQueue.grantSendMessages(sender); | |
// I'm going to process messages. If I can't process one, I'll put it in the DLQ for you ;-) | |
const processor = new Function(this, "processorFn", { | |
runtime: Runtime.NODEJS_10_X, | |
code: Code.asset("./lib/src"), | |
handler: "v2/processor/processor.main", | |
timeout: Duration.seconds(6), | |
memorySize: 256, | |
reservedConcurrentExecutions: 1, | |
environment: { | |
DLQ_QUEUE_URL: deadLetterQueue.queueUrl | |
} | |
}); | |
fifoQueue.grantConsumeMessages(processor); | |
deadLetterQueue.grantSendMessages(processor); | |
// Put the processor to work. 365 / 24 / 7. No rights for robots yet! | |
processor.addEventSource( | |
new SqsEventSource(fifoQueue, { | |
batchSize: 10 | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment