Skip to content

Commit

Permalink
Add support for reporting batch item failures
Browse files Browse the repository at this point in the history
  • Loading branch information
lhgomes committed May 7, 2023
1 parent 4c7f791 commit c398a7a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
8 changes: 7 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ Batch Mode
----------
If the event source send multiple records to be processed, the layer will execute your handler one time for each record.
To disable this behaviour, just create a variable called ``BATCH_REQUEST`` with value ``TRUE``. This option only works
if the ``THREADING_ENABLED`` was not set to ``TRUE``
if the ``THREADING_ENABLED`` was not set to ``TRUE``.

Reporting batch item failures (https://aws.amazon.com/about-aws/whats-new/2021/11/aws-lambda-partial-batch-response-sqs-event-source/)
----------
If the event source send multiple records to be processed, the layer can handle the partial batch item failures.
To enable this feature, just create a variable called ``PARTIAL_BATCH_RESPONSE`` with value ``TRUE``. This option only works
if the ``THREADING_ENABLED`` was not set to ``TRUE``.

Logging
-------
Expand Down
28 changes: 24 additions & 4 deletions phhelper/aws_lambda_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ def __records_handler(f, event, context):

return True

def __partial_batch_records_handler(f, event, context):
batch_item_failures = []
batch_response = {}

for record in event['Records']:
try:
f(record, context)
except Exception as error:
batch_item_failures.append({"itemIdentifier": record['messageId']})

logging.error('lambda_handler error: %s' % (error))
logging.error('lambda_handler trace: %s' % traceback.format_exc())

batch_response["batchItemFailures"] = batch_item_failures
return batch_response

def __cognito_handler(f, event, context):
result = None
try:
Expand Down Expand Up @@ -223,11 +239,15 @@ def generic_handler(event, context):
if (thread_enabled == 'TRUE'):
__thread_records_handler(f, event, context)
else:
batch_request = str(os.environ.get('BATCH_REQUEST','FALSE')).upper()
if (batch_request == 'TRUE'):
return __batch_handler(f, event, context)
partial_batch_response = str(os.environ.get('PARTIAL_BATCH_RESPONSE','FALSE')).upper()
if (partial_batch_response == 'TRUE'):
return __partial_batch_records_handler(f, event, context)
else:
__records_handler(f, event, context)
batch_request = str(os.environ.get('BATCH_REQUEST','FALSE')).upper()
if (batch_request == 'TRUE'):
return __batch_handler(f, event, context)
else:
__records_handler(f, event, context)
elif 'requestContext' in event:
return __apigateway_handler(f, event, context)
elif 'source' in event and event['source'] == 'aws.events':
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name='phhelper',
version='0.9.23',
version='0.9.24',
author='Luiz Henrique Gomes',
author_email="lhgnet@gmail.com",
description='Python Handler Helper for Lambda',
Expand Down

0 comments on commit c398a7a

Please sign in to comment.