From e463705bf816a2fb4b2bedd922c13d1a9afd93bc Mon Sep 17 00:00:00 2001 From: Sharif Mahmoud Date: Sat, 20 Apr 2024 00:00:24 +0400 Subject: [PATCH 1/2] Update sqs-partial-batch-failure.md --- .../middlewares/sqs-partial-batch-failure.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/website/docs/middlewares/sqs-partial-batch-failure.md b/website/docs/middlewares/sqs-partial-batch-failure.md index 13459ebc3..b1d1b1150 100644 --- a/website/docs/middlewares/sqs-partial-batch-failure.md +++ b/website/docs/middlewares/sqs-partial-batch-failure.md @@ -20,6 +20,8 @@ npm install --save-dev @aws-sdk/client-sqs ## Sample usage +Standrad Queue (All records handled in parallel): + ```javascript import middy from '@middy/core' import sqsBatch from '@middy/sqs-partial-batch-failure' @@ -35,6 +37,28 @@ const lambdaHandler = (event, context) => { export const handler = middy().use(sqsBatch()).handler(lambdaHandler) ``` +FIFO Queue (Records handled sequentially): + +```javascript +import middy from '@middy/core' +import sqsBatch from '@middy/sqs-partial-batch-failure' + +const lambdaHandler = (event, context) => { + const promises = []; + for (const [idx, record] of Object.entries(Records)) { + try { + /* Custom message processing logic */ + promises.push(Promise.resolve()); + } catch (error) { + promises.push(Promise.reject(error)); + } + } + return Promise.allSettled(recordPromises) +} + +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](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting) and [Lambda EventSourceMapping](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html) From d7c03f2c93801904f5eb3ed8d828c0a927d66589 Mon Sep 17 00:00:00 2001 From: Sharif Mahmoud Date: Sat, 20 Apr 2024 00:06:17 +0400 Subject: [PATCH 2/2] Update sqs-partial-batch-failure.md --- website/docs/middlewares/sqs-partial-batch-failure.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/website/docs/middlewares/sqs-partial-batch-failure.md b/website/docs/middlewares/sqs-partial-batch-failure.md index b1d1b1150..75bb9aca5 100644 --- a/website/docs/middlewares/sqs-partial-batch-failure.md +++ b/website/docs/middlewares/sqs-partial-batch-failure.md @@ -44,16 +44,16 @@ import middy from '@middy/core' import sqsBatch from '@middy/sqs-partial-batch-failure' const lambdaHandler = (event, context) => { - const promises = []; + const statusPromises = []; for (const [idx, record] of Object.entries(Records)) { try { /* Custom message processing logic */ - promises.push(Promise.resolve()); + statusPromises.push(Promise.resolve()); } catch (error) { - promises.push(Promise.reject(error)); + statusPromises.push(Promise.reject(error)); } } - return Promise.allSettled(recordPromises) + return Promise.allSettled(statusPromises) } export const handler = middy().use(sqsBatch()).handler(lambdaHandler)