AWS Lambda not removing messages from the queue

故事扮演 提交于 2021-01-23 15:21:02

问题


I am triggering a Lambda function from an SQS event with the following code:

@Override
public Void handleRequest(SQSEvent sqsEvent, Context context) {
    for (SQSMessage sqsMessage : sqsEvent.getRecords()) {
        final String body = sqsMessage.getBody();
        try {
            //do stuff here
        } catch (Exception ex) {
            //send to DLQ
        }
    }
    return null;
}

The "do stuff" is calling another Lambda function with the following code:

private final AWSLambda client;
    private final String functionName;

    public LambdaService(AWSLambdaAsync client, String functionName) {
        this.client = client;
        this.functionName = functionName;
    }

    public void runWithPayload(String payload) {
        logger.info("Invoking lambda {} with payload {}", functionName, payload);
        final InvokeRequest request = new InvokeRequest();
        request.withFunctionName(functionName).withPayload(payload);
        final InvokeResult invokeResult = client.invoke(request);
        final Integer statusCode = invokeResult.getStatusCode();
        logger.info("Invoked lambda {} with payload {}. Got status code {} and response payload {}",
                functionName,
                payload,
                statusCode,
                StandardCharsets.UTF_8.decode(invokeResult.getPayload()).toString());
        if(statusCode.equals(200) == false) {
            throw new IllegalStateException(String.format("There was an error executing the lambda function %s with payload %s", functionName, payload));
        }
    }

I am using the following libraries:

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-lambda-java-core</artifactId>
  <version>1.2.0</version>
</dependency>
<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-lambda-java-events</artifactId>
  <version>2.2.6</version>
</dependency>
<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk-sqs</artifactId>
  <version>1.11.505</version>
</dependency>
<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk-lambda</artifactId>
  <version>1.11.505</version>
</dependency>

The problem is that it looks like the SQS message is not removed from the queue and it gets reprocessed over and over. It happens every 30 seconds which is exactly the value of Default Visibility Timeout. Now, as far as I know, if the lambda consuming the sqs messages is terminating properly it should automatically delete the message from the queue, but this is not happening. I don't think there is any error happening in the lambda because I am not getting any message in the DLQ (and I have a catch-all block) and I cannot see any stacktrace in the logs in Cloudwatch. I am bit confused about what's happening here, anyone has some good idea?


回答1:


Unless something changed recently, I don't think the AWS SDK for Java automatically deletes the message from the queue. You need to write the code to do that.

I would love to be proven wrong on that one, please share the doc excerpt I missed.

Code sample :

  • https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-sqs-messages.html
  • https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/standard-queues-getting-started-java.html


来源:https://stackoverflow.com/questions/57003706/aws-lambda-not-removing-messages-from-the-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!