You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utilities/batch.md
+129-6Lines changed: 129 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -78,9 +78,14 @@ To install this utility, add the following dependency to your project.
78
78
}
79
79
```
80
80
81
-
**IAM Permissions**
81
+
## IAM Permissions
82
82
83
-
This utility requires additional permissions to work as expected. Lambda functions using this utility require the `sqs:GetQueueUrl` and `sqs:DeleteMessageBatch` permission.
83
+
This utility requires additional permissions to work as expected. Lambda functions using this utility require the `sqs:DeleteMessageBatch` permission.
84
+
85
+
If you are also using [nonRetryableExceptions](#move-non-retryable-messages-to-a-dead-letter-queue) attribute, utility will need additional permission of `sqs:GetQueueAttributes` on source SQS.
86
+
It also needs `sqs:SendMessage` and `sqs:SendMessageBatch` on configured dead letter queue.
87
+
88
+
Refer [example project](https://github.com/aws-samples/aws-lambda-powertools-examples/blob/main/java/SqsBatchProcessing/template.yaml#L67) for policy details example.
84
89
85
90
## Processing messages from SQS
86
91
@@ -91,7 +96,8 @@ Both have nearly the same behaviour when it comes to processing messages from th
91
96
***Entire batch has been successfully processed**, where your Lambda handler returned successfully, we will let SQS delete the batch to optimize your cost
92
97
***Entire Batch has been partially processed successfully**, where exceptions were raised within your `SqsMessageHandler` interface implementation, we will:
93
98
-**1)** Delete successfully processed messages from the queue by directly calling `sqs:DeleteMessageBatch`
94
-
-**2)** Raise `SQSBatchProcessingException` to ensure failed messages return to your SQS queue
99
+
-**2)** if, non retryable exceptions occur, messages resulting in configured exceptions during processing will be immediately moved to the dead letter queue associated to the source SQS queue or deleted from the source SQS queue if `deleteNonRetryableMessageFromQueue` is set to `true`.
100
+
-**3)** Raise `SQSBatchProcessingException` to ensure failed messages return to your SQS queue
95
101
96
102
The only difference is that **SqsUtils Utility API** will give you access to return from the processed messages if you need. Exception `SQSBatchProcessingException` thrown from the
97
103
utility will have access to both successful and failed messaged along with failure exceptions.
@@ -110,15 +116,20 @@ When using this annotation, you need provide a class implementation of `SqsMessa
110
116
111
117
All records in the batch will be passed to this handler for processing, even if exceptions are thrown - Here's the behaviour after completing the batch:
112
118
113
-
***Any successfully processed messages**, we will delete them from the queue via `sqs:DeleteMessageBatch`
114
-
***Any unprocessed messages detected**, we will raise `SQSBatchProcessingException` to ensure failed messages return to your SQS queue
119
+
***Any successfully processed messages**, we will delete them from the queue via `sqs:DeleteMessageBatch`.
120
+
***if, nonRetryableExceptions attribute is used**, messages resulting in configured exceptions during processing will be immediately moved to the dead letter queue associated to the source SQS queue or deleted from the source SQS queue if `deleteNonRetryableMessageFromQueue` is set to `true`.
121
+
***Any unprocessed messages detected**, we will raise `SQSBatchProcessingException` to ensure failed messages return to your SQS queue.
115
122
116
123
!!! warning
117
124
You will not have access to the **processed messages** within the Lambda Handler - all processing logic will and should be performed by the implemented `#!java SqsMessageHandler#process()` function.
throw new IllegalArgumentException("Failed business validation. No point of retrying. Move me to DLQ." + message.getMessageId());
177
+
}
178
+
179
+
return returnVal;
180
+
}
181
+
}
182
+
}
183
+
```
184
+
185
+
142
186
### SqsUtils Utility API
143
187
144
188
If you require access to the result of processed messages, you can use this utility. The result from calling **`#!java SqsUtils#batchProcessor()`** on the context manager will be a list of all the return values
@@ -248,3 +292,82 @@ If you want to disable the default behavior where `SQSBatchProcessingException`
248
292
return returnValues;
249
293
}
250
294
```
295
+
296
+
## Move non retryable messages to a dead letter queue
297
+
298
+
If you want certain exceptions to be treated as permanent failures during batch processing, i.e. exceptions where the result of retrying will
299
+
always be a failure and want these can be immediately moved to the dead letter queue associated to the source SQS queue, you can use `SqsBatch#nonRetryableExceptions()`
300
+
to configure such exceptions.
301
+
302
+
If you want such messages to be deleted instead, set `SqsBatch#deleteNonRetryableMessageFromQueue()` to `true`. By default, its value is `false`.
303
+
304
+
Same capability is also provided by [SqsUtils Utility API](#sqsutils-utility-api).
305
+
306
+
!!! info
307
+
Make sure the lambda function has required permissions needed by utility. Refer [this section](#iam-permissions).
0 commit comments