Handling partial batch failures with the SQS Lambda Poller
A description of how the lambda sqs poller handles both full and partial failures.
copy_object
operation, the object didn't seem to exist.where we could signal which messages had failed to be processed, rather than having to process all messages even if they had been successful. To do this, we could return an object called
batchItemFailures
.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
batch_item_failures = []
For message in event["Messages"]
try:
s3_client.copy_object(
CopySource=copy_object,
Bucket=s3access_bucket,
Key=target_key)
s3_client.delete_object(Bucket=s3access_bucket, Key=key)
except botocore.exceptions.ClientError as exception:
batch_item_failures.append(
{"itemIdentifier": message['messageId’]})
sqs_batch_response["batchItemFailures"] = batch_item_failures
return sqs_batch_response
batch_item_failures
. As we loop over the messages we're receiving from SQS in the event["Messages"]
data, we check each iteration to see if our copy works. If, however, we see a failure, we add an itemIdentifier
object to the list, along with the id of the message we were processing that failed.batchItemFailures
containing our list of failed messages - an example structure might look something like1
2
3
4
5
6
7
8
9
10
{
"batchItemFailures": [
{
"itemIdentifier": "msg-id-1234"
},
{
"itemIdentifier": "msg-id-7890"
}
]
}