Skip to main content

sqs-partial-batch-failure

Middleware for handling partially failed SQS batches.

Install

To install this middleware you can use NPM:

npm install --save @middy/sqs-partial-batch-failure
# Required for types only
npm install --save-dev @aws-sdk/client-sqs

Options

  • logger (function) (optional): A function that will be called when a record fails to be processed. Default: console.error

Sample usage

Standrad Queue (All records handled in parallel):

import middy from '@middy/core'
import sqsBatch from '@middy/sqs-partial-batch-failure'

const lambdaHandler = (event, context) => {
const recordPromises = event.Records.map(async (record, index) => {
/* Custom message processing logic */
return record
})
return Promise.allSettled(recordPromises)
}

export const handler = middy().use(sqsBatch()).handler(lambdaHandler)

FIFO Queue (Records handled sequentially):

import middy from '@middy/core'
import sqsBatch from '@middy/sqs-partial-batch-failure'

const lambdaHandler = (event, context) => {
const statusPromises = [];
for (const [idx, record] of Object.entries(Records)) {
try {
/* Custom message processing logic */
statusPromises.push(Promise.resolve());
} catch (error) {
statusPromises.push(Promise.reject(error));
}
}
return Promise.allSettled(statusPromises)
}

export const handler = middy().use(sqsBatch()).handler(lambdaHandler)

Important

The value ReportBatchItemFailures must be added to your Lambda's FunctionResponseTypes in the EventSourceMapping. See Reporting batch item failures and Lambda EventSourceMapping