4
votes

I have a lambda trigger on an SQS queue which is configured with a DLQ.

When my lambda failed the original message from the queue will be redirected to the DLQ. Now I want to add more information to this original message (like why there was an error etc). I know that I can't modify the original message but I saw that a message can have additional message attributes RequestID, ErrorCode, ErrorMessage.

How can I use/ setup them from my lambda function (NodeJS) ?

1
How you pass message to DLQ from NodeJs Code or AWS DLQ settings? - Chandani Patel
I have a lambda trigger listening to the DLQ. And the message are sent to the DLQ with SQS redrive policies. - Marc Sirisak
okay. In this case AWS automatically adds the attributes so you can debug why it is not processed. You can check the message from logs and cross check in cloudwatch. - Chandani Patel
When I check the DLQ I can see the message body of the original message, but in the message attribute tab, the fields are empty. I am throwing the error like this in the lambda function : throw new Error((err.message) ? err.message : JSON.stringify(err)). Is there a way to specify the attributes manually ? - Marc Sirisak

1 Answers

0
votes

Although lambda does not let you edit retried messages in any way before they get sent to DLQ, we can indirectly add a few attributes like below to the message that can explain why it failed.

enter image description here

This only works for specific cases, mainly for asynchronous, non-stream-based invocations which basically means lambda’s native async retries or SNS triggers work but SQS based retries, for instance, don’t. The other condition is that the exception returned/thrown must be an Error or an extension of the Error prototype for node lambdas.

Something like

exports.handler = async (event, context, cb) => {
   class CustomError extends Error {
       constructor(message) {
           super(message);
           this.name = "Some Lambda Error";
           this.message = message;
       }
   };
   let error = new CustomError("Something went wrong")
   cb(error);

   // or just simply

   cb(new Error("Something went wrong"));
};