How do I fail a specific SQS message in a batch from a Lambda?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Amazon Simple Queue Service (SQS) is a fully managed message queuing service that allows for the decoupling and scaling of microservices, distributed systems, and serverless applications. When using AWS Lambda to process messages from an SQS queue, it is essential to manage the acknowledgment or failure of messages properly. This is particularly critical when dealing with message batches, where the need might arise to fail individual messages within a batch while successfully processing others.
Handling Messages in a Batch
When AWS Lambda functions process SQS messages, they often receive a batch of messages. By default, the batch size can be set up to ten messages. While AWS Lambda takes care of polling the queue, you must ensure each message is handled correctly to avoid data loss or unnecessary retries.
Processing a Batch of Messages
Each message in the batch received by a Lambda function is an individual event that can be processed independently. At the end of the function execution, Lambda acknowledges successful processing to SQS. If the function returns successfully, SQS deletes the processed messages. If an error occurs during the execution of the function, the entire batch of messages is returned to the queue.
Failing a Specific Message
In particular, when one message in a batch fails processing, but you want the successfully processed messages to be deleted, you must handle exceptions in a controlled manner and manage which messages to fail manually:
- Iterate Over Each Message: Each message in the batch should be processed in a loop, where exceptions can be caught on a per-message basis.
- Generate Individual Results: Collect the outcome of each message processing step (success or failure). This could involve creating a custom result object or structure to maintain the results.
- Return Properly Annotated Results: Instead of merely throwing an exception on failure, which would return the entire batch to the queue, generate a response that specifically details which messages failed. This involves implementing logic within your Lambda function to process these results.
Example Lambda Python Code
Here's a simple example illustrating how you might handle different failures in a Lambda function processing an SQS batch in Python:

