How to send error to DLQ if Lambda function throws error (not timedOut)?
Asked Answered
T

3

7

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?

  • example error from CloudWatch:

TypeError: commandArray is not iterable

Tobey answered 18/10, 2018 at 11:45 Comment(0)
F
3

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

  • Lambda function name can be found by S3 API, S3 bucket detail can be found by received event object in the error message.

Solution - 2

Convention: SNS topic name contains lambda function name in it

  • Configure SNS topic to lambda function
  • Add a lambda function to SNS topic subscriber list
  • Subscribed lambda function can get lambda function name from SNS topic name and can add any custom detail in the received error message
Forgiven answered 18/10, 2018 at 21:31 Comment(2)
Lambda retry will happen only if you use callbacks, you can find more details here theantway.com/2017/06/…Niche
How do you specify the SQS queue or SNS topic for it to publish error message detail to?Intercalate
A
2

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.

Aaron answered 27/2 at 0:7 Comment(0)
T
1

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

Towne answered 18/10, 2018 at 14:15 Comment(1)
How do you specify the SQS queue or SNS topic for it to publish error message detail to?Intercalate

© 2022 - 2024 — McMap. All rights reserved.