14

I'm looking at the the AWS SQS documentation here: https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/ReceiveMessage.html#receive-sqs-message

My understanding is that we need to delete the message using AmazonSQSClient.DeleteMessage() once we're done processing it, but is this necessary when we're working with an SQS triggered Lambda?

I'm testing with a Lambda function that's triggered by an SQSEvent, and unless I'm mistaken, it appears that if the Lambda function runs to completion without throwing any errors, the message does NOT return to the SQS queue. If this is true, the I would rather avoid making that unnecessary call to AmazonSQSClient.DeleteMessage().

Here is a similar question from 2019 with the top answer saying that the SDK does not delete messages automatically and that they need to be explicitly deleted within the code. I'm wondering if anything has changed since then.

Thoughts?

1
  • 4
    Minor note on terminology. In the case of the Lambda function failing to process the SQS messages, those messages aren't returned to the SQS queue. Instead, they become visible again (once their visibility timeout occurs). Those messages never left the SQS queue. Commented Dec 14, 2020 at 18:21

2 Answers 2

41

The key here is that you are using the AWS Lambda integration with SQS. In that instance AWS Lambda handles retrieving the messages from the queue (making them available via the event object), and automatically deletes the message from the queue for you if the Lambda function returns a success status. It will not delete the message from the queue if the Lambda function throws an error.

When using AWS Lambda integration with SQS you should not be using the AWS SDK to interact with the SQS queue at all.


Update: Lambda now supports partial batch failure for SQS whereby the Lambda function can return a list of failed messages and only those will become visible again.

Sign up to request clarification or add additional context in comments.

11 Comments

There's one scenario in which you might want the Lambda to interact directly with SQS, and that's if you're processing a batch of messages and, in preparation for the potential failure to process some subset of those messages, you want to be sure that the successfully-processed messages do not become visible again in SQS. Your Lambda function can explicitly delete each SQS message as it is successfully processed in this case. Or you can make use of middleware, such as middy, to do this for you.
@Mercury it's in the second paragraph on this page: docs.aws.amazon.com/lambda/latest/dg/with-sqs.html
Following the comment @jarmod there is a video on this topic here: youtube.com/watch?v=8zysQqxgj0I&t=1263s
Small update here: Lambda now supports partial batch failure for SQS whereby the Lambda function can return a list of failed messages snd only those will become visible again.
Lambda function returns a success status - what exactly should you return from the lambda to indicate a successful status?
|
-1

Yes, otherwise the next time you ask for a set of messages, you will get the same messages back - maybe not on the next call, but eventually you will. You likely don't want to keep processing the same set of messages over and over.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.