Search code examples
amazon-web-servicesaws-lambdaamazon-sqs

how to get single record from SQS trigger?


We are getting all the records sqs event trigger at once how to get or limit the trigger to get only one at a time?

public class sqslambda implements RequestHandler<SQSEvent, Void> {
  @Override
  public Void handleRequest(SQSEvent event, Context context) {
    try {
      for (SQSEvent.SQSMessage message : event.getRecords()) {

       String input = message.getBody();

      }
    } catch (Exception ex) {
      logger.error("Exception handling batch seed request.", ex);
      throw ex;
    }
    return null;
  }
  ...
}

Solution

  • You use an event source mapping to tell Lambda to send items from an Amazon SQS queue to an AWS Lambda function.

    As part of this configuration, you can define the batch size, which specifies "the largest number of records that will be read from your queue at once".

    Therefore, you should go to your Lambda function, click on the SQS trigger and set the Batch Size to one.

    See also: Using AWS Lambda with Amazon SQS