amazon-web-servicesaws-lambdaamazon-sqsaws-sqs-fifo

SQS Messages not deleted after DLQ Implementation


I recently implemented DLQ(Dead Letter Queue) for SQS. I have done the following three configurations.

  1. Default visibility timeout in SQS.
  2. In the Dead Letter Queue configuration, enable and Maximum receives are configured as 3.
  3. In Lambda configuration, Report batch item failures are enabled.

But the problem, all success/failure messages are processed three times and moved to DLQ.

For success cases, the correct JSON response is returned.

Once I disable the "Report batch item failures is enabled", message will deleted for both success/failure cases.


Solution

  • Once we enable the "Report batch item failures is enabled", we should change the response type of the function.

    Old Code : public async Task<String> FunctionHandlerAsync(SQSEvent sqsEvent)

    New Code : public async Task<SQSBatchResponse> FunctionHandlerAsync(SQSEvent sqsEvent)

    Due to response type changes, we should change the code in function implementation.

    create the object,

     List<SQSBatchResponse.BatchItemFailure> batchItemFailures = new List<SQSBatchResponse.BatchItemFailure>();
    

    For exception cases,

    batchItemFailures.Add(new SQSBatchResponse.BatchItemFailure { ItemIdentifier = record.MessageId });
    

    Finally, return the batch response to function,

    return new SQSBatchResponse(batchItemFailures);
    

    After the above changes, success messages are correctly deleted.

    Reference: https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting