Skip to content

BatchProcessor.response() crashes with KeyError when failed SQS record lacks messageId #8273

@KirtiRamchandani

Description

@KirtiRamchandani

Bug

When raise_on_entire_batch_failure=False and a record handler throws, BatchProcessor.response() can crash with:

KeyError: 'messageId'

in _collect_sqs_failures() via msg.message_id.

This happens if the inbound SQS record is missing messageId (malformed/custom event injection).

Repro

from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType

processor = BatchProcessor(event_type=EventType.SQS, raise_on_entire_batch_failure=False)

def record_handler(record):
    raise RuntimeError("boom")

bad_record = {
    "body": "{}",
    "receiptHandle": "rh",
    "attributes": {"ApproximateReceiveCount": "1"},
    "messageAttributes": {},
    "md5OfBody": "abc",
    "eventSource": "aws:sqs",
    "eventSourceARN": "arn:aws:sqs:us-east-1:123:q",
    "awsRegion": "us-east-1",
}

with processor(records=[bad_record], handler=record_handler):
    processor.process()

processor.response()  # KeyError: 'messageId'

Expected

Return partial batch response (empty itemIdentifier or explicit error), not crash the Lambda.

Proof after fix

tests/functional/batch/required_dependencies/test_utilities_batch.py::test_sqs_batch_processor_missing_message_id_does_not_crash_on_handler_failure PASSED

PR incoming.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions