Search code examples
node.jsamazon-web-servicesaws-lambdaamazon-sqs

AWS SQS+Lambda, should I delete messages? NodeJS


I recently started working with AWS, my first job is to consume a SQS queue using a lambda function.

I know I don't need to call receiveMessage because the lambda function already receives messages here:

exports.handler = async(event, context) => {
let i = 0;

for (const record of event.Records) {
...
}
};

My question is: should I call sqs.deleteMessage for every message received? I know the lambda function automatically deletes processed messages, but a friend told me I still have to call deleteMessage manually because IF an error occurs, all messages go back to the queue If I don't delete each message manually.

Thank you!


Solution

  • If your Lambda function returns a success code, then the message will be automatically deleted. If an error occurs, and your function returns an error response, then the message will be queued up again (depending on the settings of the queue). You should never delete the message yourself. Why would you want to bypass the error handling and retry features built into SQS?

    If you never want a message to be retried, then simply configure the queue appropriately. In particular, the Maximum receives and, optionally the DLQ settings on your SQS queue should be used to define the error handling behavior.