SQS + Lambda: break the flow, recover the message
Make Lambda fail, trace SQS retries, and recover a message from the DLQ.
A message keeps failing and ends up in a dead-letter queue (DLQ). Reproduce that failure with AWS Emulator, find the cause in the logs, and recover the message after fixing the consumer.
Set up your environment
-
Clone the repository
Run the rest of the commands from this lab directory.
git clone https://github.com/leodc/devops-in-practice-labs.git cd devops-in-practice-labs/sqs-lambda-retries
Linux
-
Start the emulator
Install AWS Emulator 1.0.322 and Python with pip and venv. Then prepare the Python environment and start the emulator.
Install AWS Emulator 1.0.322 ↗python3 -m venv .venv .venv/bin/pip install -r requirements.txt aws-emulator --version bash start-emulator.sh- Leave this terminal running.
-
Open your working terminal
In a second terminal, enter the lab directory and activate its Python environment. Adjust the path if you cloned the repository elsewhere.
cd devops-in-practice-labs/sqs-lambda-retries source .venv/bin/activate
Docker
-
Enter the lab environment
Start the container from the lab directory. It includes AWS Emulator 1.0.322, Python, and nano. Logs are saved to .lab on your computer.
bash docker-lab.sh- Use Bash and Docker with Linux containers; on Windows, run these commands in WSL.
Stage 1 / 8
Create the queues and function
Create the source queue, DLQ, and Lambda function. Leave the event source mapping disabled so you can inspect them before processing messages.
python lab.py prepare
python lab.py inspect Stop and inspect
- Check that the mapping is Disabled, fail_poison is true, and both queues are empty.
- Open handler.py and find what makes order-poison fail. The lab uses one record per invocation, a 1-second function timeout, a 6-second visibility timeout, and maxReceiveCount 3.
What does the disabled mapping let you inspect?
See explanation
You can check the function and queues before polling starts. Sending messages will not trigger processing until you enable the mapping.
These settings keep the exercise short. AWS recommends a visibility timeout at least six times the function timeout and maxReceiveCount of at least five; this lab uses three to reach the DLQ sooner.
Stage 2 / 8
Send two messages
Send order-ok and order-poison while consumption is still disabled.
python lab.py send
python lab.py inspect Stop and inspect
- Find the two sent events and save the message_id for order-poison.
- Check for two visible messages in the source queue and an empty DLQ.
Why are there no handler logs yet?
See explanation
The event source mapping polls SQS and invokes Lambda. While it is disabled, both messages remain in the queue.
Stage 3 / 8
Start processing
Enable the mapping. Lambda will process the messages and retries will continue in the background.
python lab.py consume
python lab.py inspect Stop and inspect
- Look for processed for order-ok and deliberate_failure for order-poison. Run inspect again if the logs have not arrived.
- Follow message_id and receive_count across attempts.
What lets the failed message become available again?
See explanation
The handler raises an error for order-poison, so the message stays in SQS. When its visibility timeout expires, it becomes available for another attempt.
With one record per invocation, the successful message is not retried alongside the failing one.
Stage 4 / 8
Inspect retries and the DLQ
Wait for the message to reach the DLQ and inspect its logs and metadata.
python lab.py diagnose
python lab.py inspect Stop and inspect
- Find three deliberate_failure events for the same message, with receive_count 1, 2, and 3: one initial attempt and two retries.
- Find the diagnosis event for order-poison. Compare its dlq_receive_count with the failure logs.
Does a receive count of 4 prove that Lambda failed four times?
See explanation
Lambda failed three times. The fourth receive was the diagnostic read from the DLQ. The script restores the message’s visibility immediately; repeating diagnose returns the saved result without receiving it again.
The source queue’s redrive policy sends the message to this DLQ. This is separate from Lambda’s DLQ configuration for asynchronous invocations.
Reference logs
Your IDs and timestamps will differ.
{"body": "order-poison", "event": "deliberate_failure", "message_id": "193c9481-2fae-42ae-a35a-e77fe77c20e5", "receive_count": "1"}
{"body": "order-poison", "event": "deliberate_failure", "message_id": "193c9481-2fae-42ae-a35a-e77fe77c20e5", "receive_count": "2"}
{"body": "order-poison", "event": "deliberate_failure", "message_id": "193c9481-2fae-42ae-a35a-e77fe77c20e5", "receive_count": "3"}
{"body": "order-poison", "dlq_receive_count": "4", "event": "diagnosis", "failed_deliveries": 3, "message_id": "193c9481-2fae-42ae-a35a-e77fe77c20e5", "time": "2026-11-12T02:37:23.550458+00:00"} Stage 5 / 8
Fix the consumer
In consumer.json, change fail_poison to false. Save in nano with Ctrl+O, Enter, then exit with Ctrl+X. Run repair to apply the change to Lambda.
nano consumer.json
python lab.py repair
python lab.py inspect Stop and inspect
- Check that the function reports fail_poison as false.
- Confirm the message is still in the DLQ.
Why has the fixed function not processed that message yet?
See explanation
The mapping polls the source queue, but the message is in the DLQ. Fixing the function does not return it to the source queue.
Stage 6 / 8
Return the message to the source queue
Start a redrive task. The mapping is still enabled, so Lambda can process the message as soon as it returns.
python lab.py redrive
python lab.py inspect Stop and inspect
- Find redrive_started and its task handle. This confirms that the task started; verify checks whether recovery completed.
What would happen if you redrove the message before fixing the failure?
See explanation
The message would fail again. Repair the consumer before returning messages to a queue it is still polling.
Stage 7 / 8
Verify recovery
Check that redrive completed, Lambda processed order-poison, and both queues are empty.
python lab.py verify
python lab.py inspect Stop and inspect
- Find verification with result PASS and a processed event for order-poison.
- Check redrive_identity_observed. Emulator 1.0.322 preserves the message ID and receive count; AWS assigns a new ID and enqueue time after redrive.
Why would an empty DLQ be insufficient evidence?
See explanation
An empty DLQ only tells you the message left it. Successful recovery also needs a completed redrive task, a processed log, and zero visible, in-flight, or delayed messages in both queues.
Reference logs
Your IDs and timestamps will differ.
{"body": "order-poison", "event": "processed", "message_id": "193c9481-2fae-42ae-a35a-e77fe77c20e5", "receive_count": "5"}
{"dlq": {"ApproximateNumberOfMessages": 0, "ApproximateNumberOfMessagesDelayed": 0, "ApproximateNumberOfMessagesNotVisible": 0}, "event": "verification", "result": "PASS", "source": {"ApproximateNumberOfMessages": 0, "ApproximateNumberOfMessagesDelayed": 0, "ApproximateNumberOfMessagesNotVisible": 0}, "time": "2026-11-12T02:37:29.200757+00:00"} Stage 8 / 8
Save the logs and clean up
Delete the emulated resources. The full sequence remains in .lab/events.jsonl on your computer, even after the container is removed.
python lab.py cleanup
cat .lab/events.jsonl Stop and inspect
- Check for cleanup_completed.
- Linux: stop terminal A with Ctrl+C.
- Docker: exit the container shell. From the lab directory on your computer, open .lab/events.jsonl in your editor or run cat .lab/events.jsonl.
- If you restarted the emulator mid-lab, run python lab.py reset, then python lab.py prepare. Existing logs are kept; use timestamps to distinguish runs.
Which log entries explain the failure and recovery?
See explanation
Follow the original message ID through the three failures, the DLQ read, the repair, and redrive. Finish with the processed event and PASS result.
Share events.jsonl rather than the raw emulator startup log, which can contain generated local credentials.
Read the whole sequence
Use your logs to explain when the message became available again, why it reached the DLQ, and what confirms that recovery succeeded.
References
Send it to someone who might find it useful.