I am fairly new to the SQS Lambda integration and trying to understand the best approach for my use-case.
I have an SQS Lambda integration. My Lambda function is receiving messages from SQS and calls an external API to process the messages. The messages will be received one at a time by Lambda function, with batch size set to 1.
@Override
public Void handleRequest(SQSEvent event, Context context) {
Response response = null;
if (event != null) {
for (SQSMessage msg : event.getRecords()) {
response = callXXXAPI(msg.getBody(), reqId, logger);
}
}
return null;
}
I'm looking for a way to capture any error responses from the external API call and put back the messages which had failed response (non-200 status) on to the queue. These API-failed messages are not currently going back to the queue as they are considered "PROCESSED". Is there a way to do it with my current setup? Is it recommended?
The SQS also has a Dead Letter Queue.
If you modify your function code to throw an Exception
when the API call fails, the Lambda/SQS integration will treat that as a failure and place the message back in the queue.
You will also need to configure Maximum receives
to some number greater than 1
if you want the messages to be retried multiple times before being sent to the DLQ.