SQS batch middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda
Middleware for handling partially failed SQS batches.
To install this middleware you can use NPM:
npm install --save @middy/sqs-partial-batch-failure
sqs
: anAWS.SQS
instance for deleting successfully processed messages from the queue.
const middy = require('@middy/core')
const sqsBatch = require('@middy/sqs-partial-batch-failure')
const originalHandler = (event, context, cb) => {
const recordPromises = event.Records.map(async (record, index) => { /* Custom message processing logic */ })
return Promise.allSettled(recordPromises)
}
const handler = middy(originalHandler)
.use(sqsBatch())
Your Lambda function requires permission to delete the message from the queue.
{
"Effect": "Allow",
"Action": [
"sqs:DeleteMessage"
],
"Resource": "arn:aws:sqs..."
}
This middleware expects a Promise.allSettled
resolved value to be returned by the Lambda handler, which is only available in Node.js 12+.
To use this middleware with earlier versions of Node.js, use a polyfill. We recommend installing promise.allsettled and adding require('promise.allsettled').shim()
to your handler.
For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.
Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.
Licensed under MIT License. Copyright (c) 2017-2018 Luciano Mammino and the Middy team.