Search code examples
javaaws-lambdaamazon-sqs

AWS Lambda and SQS: failure reporting


It's more of a "best practice" or "am I doing it right" question, but I can't find the answer anywhere on the Internet. I'm preparing an AWS Lambda (Java), which will be triggered by SQS events. It will receive up to 10 events and process them. If some of the events will not be processed correctly (dependent on external services), it will remove processed events from the SQS queue. Nothing very fancy really :) Question is - how should I finish the execution of the lambda in case of failure in the processing of the batch (either partial or full).

Code for Lambda handler looks like this:

public class LambdaHandler implements RequestHandler<SQSEvent, Void> {

    private final CopierComponent copierComponent;

    public LambdaHandler() {
        this.copierComponent = DaggerCopierComponent.builder().build();
    }

    @Override
    public Void handleRequest(SQSEvent sqsEvent, Context context) {
        context.getLogger().log("entering the function");
        copierComponent.ldpDynamoToEsCopier().processMessages(sqsEvent);
        return null;
    }
}

Questions:

  1. In case of success, LambdaHandler will just return null. Should it return anything else? Some kind of LambdaResponse with code 200? Some examples propose String "200 OK", but does it really matter?

  2. In case of failure, processMessages will first remove processed messages from SQS, then raise an exception (BatchProcessingFailure). SQS will not receive any response, so after visibility timeout, it will return not processed messages back to the queue (there is a DLQ configured as well).

I don't like this method though. Is there some other method of returning some value or finishing lambda with a failed state? Some kind of LambdaResponse with code 500, that would inform SQS to not to remove messages and return them to queue?


Solution

  • There seems to be support now for partial failures when consuming SQS messages from Lambda: https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting