Search code examples
amazon-web-servicesamazon-sqsdead-letter

Force a message from SQS Queue to its dead letter queue?


Trying to write some tests for my AWS SQS Queue and its associated Dead letter queue. I want to somehow in my tests force the message from the queue to its DLQ, then read from the dlq to see if the message is there.

Reading from the DLQ is no problem. But does anyone know a quick and easy way I can programmatically force an sqs queue to send a message to its associated DLQ?


Solution

  • The Dead Letter Queue (DLQ) is simply an SQS Queue, so you can send a message to it like you would any other queue.

    The DLQ is configured when you create your normal queue, and you need to pass the ARN of a queue that will be used as the DLQ.

    When you configure your DLQ, you set the maxReceiveCount (referred to as Maximum receives on the console). This is the number of times a message is delivered to the source queue before being moved to the DLQ. When the ReceiveCount for a message exceeds the maxReceiveCount for a queue, Amazon SQS moves the message to the DLQ.

    enter image description here

    If you want to test the process of sending messages to DLQs, you need to force an error in your tests on the queue messages' processing. This will send a message to the DLQ, which is the best way to understand if the errors are being routed to the queue correctly.

    The process to send messages to the DLQ can be done in the following ways:

    • You explicitly send a message to the DLQ if you find some error and do not want to process or delete the message at that time.

    • If you read the messages more times than the maxReceiveCount and do not process the message (read and delete from the queue), the AWS SQS service will understand that you are having problems with that message and will automatically send it to the DLQ for you (e.g., if maxReceiveCount equals 1 and you read the message twice without deleting it).

    To understand more about DLQs, take a look here: Amazon SQS dead-letter queues.