0

According to documentation Lambda should leave message in queue if errors happens. https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html There is sentence "When your function returns an error, Lambda leaves it in the queue." My first idea was that if I throw new Exception that would work. However it seems my messages are processed and deleted. Why and how to return error from Lambda in .NET?

For example, should this leave message in queue:

    public async Task FunctionHandler(SQSEvent evnt, ILambdaContext context)
    {

throw new Exception ("Whole point of this function is that is retriable in case of error. Why I am deleted from the queue if exception is thrown?");
     }
    
6
  • Any demo code you can share please? Especially on how you're handling the exception being thrown. Commented Jun 8, 2022 at 10:48
  • 1
    settings of your SQS queue? visibility timeout? any dead letter queues? Commented Jun 8, 2022 at 10:58
  • Visibility timeout is 45 seconds. I simplified code so that when functions start I throw new Exception(message.Body) and I also added Dead Letter Queue. Actually, it seems that Lambda repeates executions. I don't know how I didn't see it. Maybe because different way of throwing Exceptions was in place but I suppose every exception would need to cause Lambda to return message back to the queue. Commented Jun 8, 2022 at 11:13
  • I don't know how many times lambda "repeteas" execution but when I added another message my previous one ended in Dead Letter Queue. I don't know why but before that it was execution whole time previous message. Commented Jun 8, 2022 at 11:18
  • 1
    It sounds like it's doing what it's supposed to do (retry a couple of times, then move to DLQ). Is your problem now resolved? Commented Jun 8, 2022 at 11:44

1 Answer 1

1

The maximum receives value determines how many times a failure (SQS) message can be resent to the lambda function for processing. Once the failure message's retry count reaches the configured limit, then the message will be forwarded to a Dead-letter Queue.

In the below image, I highlighted the location on where you can see the maximum receives value. enter image description here

p.s All the unhandled exceptions from a lambda function behaves the above-mentioned way because a lambda (consumer) is actually responsible for removing the processed message from the SQS queue, and this usually happens at the end of the lambda execution.

Since you throw the exception before completing the process, the message would be stayed in SQS for the configured visibility timeout limit, and then it will retry based on the maximum receives value.

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

1 Comment

Tnx, didn't expect this setting inside SQS but now is logical to me

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.