Reputation: 621
I am having Lambda function that I expect to return some result. So if I send wrong parameters it fails for example in the middle of the function.
Is there a way I can handle if any error occurs to be sent in my DLQ, print the error in the message, then retry, then delete the message?
TypeError: commandArray is not iterable
Upvotes: 7
Views: 27142
Reputation: 1864
PROBLEM
I had SQS trigger a Lambda and if it failed I expected it to send the message to the DLQ I had setup, however, this wasn't happening.
The problem was that when the Lambda function failed I was returning an object with a status code (with the appropriate error code) and body key. However, the Lambda service considered that a successful invocation of the function.
SOLUTION
To fix this problem I had to throw
an error instead of returning a response. When I did this the message was sent to the DLQ after it retried the number of times as per the max receive count value.
I couldn't find anywhere in the AWS documentation where this was clearly stated.
Upvotes: 3
Reputation: 41
AWS Lambda function has a retry mechanism on Asynchronous invocation, If AWS Lambda is unable to fully process the event, it will automatically retry the invocation twice, with delays between retries.
After retries, AWS Lambda will send ERROR message detail to the specified Amazon SQS queue or Amazon SNS topic.
https://docs.aws.amazon.com/lambda/latest/dg/retries-on-errors.html
The error message does not contain failed Lambda function name due to any reason (exceptions/timeout). To add lambda function name in the error message, you can go for two ideas.
Solution - 1
Solution - 2
Convention: SNS topic name contains lambda function name in it
Upvotes: 3
Reputation: 3496
Lambda has the facility to retry and pump failures into a Dead Letter Queue.
Any Lambda function invoked asynchronously is retried twice before the event is discarded. If the retries fail and you're unsure why, use Dead Letter Queues (DLQ) to direct unprocessed events to an Amazon SQS queue or an Amazon SNS topic to analyze the failure.
You can then have a Lambda Function on the SNS topic or SQS queue that can respond to the error and react in the way you want it to.
For more information, see: https://docs.aws.amazon.com/lambda/latest/dg/dlq.html
Upvotes: 1