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

1

6

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?

Rosaliarosalie answered 16/2, 2022 at 20:31 Comment(3)
Did you try simply getting it from the original queue and writing it to the associated DLQ (which is just another queue)?Mumps
I could do that, but that would defeat the purpose of a test. What I am trying to do is test that the SQS queue will move messages to its associated DLQ. I am new to AWS though, and I am unsure of when an SQS queue goes about moving its messages to a DLQ. I know it is related to the maxReceiveCount of the queueRosaliarosalie
Ah, I see. Read the section on Understanding SQS dead-letter queues (DLQs): "When the ReceiveCount for a message exceeds the maxReceiveCount for a queue, SQS moves the message to the DLQ." You could trigger a dummy Lambda function from the queue and have your function simply throw an exception (or otherwise indicate error) every time.Mumps
D
9

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.

Drip answered 16/2, 2022 at 20:37 Comment(2)
Any suggestions on how I can force that error to occur? Wouldn't the error be occurring on the AWS servers? I am unsure how I can get a sendMessage request to successfully go through, but also cause an error once it arrives to AWS serversRosaliarosalie
I updated my answer to explain how DLQs works and how you can do what you wantDrip

© 2022 - 2024 — McMap. All rights reserved.