You can use the following bash script to refeed AWS events from a dead letter queue (DLQ) to an SQS queue in batches of 5 events with a 1-minute wait between batches. Replace DLQ_URL and TARGET_SQS_URL with the appropriate URLs for your dead letter and target queues.
#!/bin/bash
DLQ_URL="https://sqs.region.amazonaws.com/your-account-id/dead-letter-queue-name"
TARGET_SQS_URL="https://sqs.region.amazonaws.com/your-account-id/target-queue-name"
BATCH_SIZE=5
WAIT_TIME=60
while true; do
RECEIPT_HANDLES=""
MESSAGES=$(aws sqs receive-message --queue-url "$DLQ_URL" --max-number-of-messages "$BATCH_SIZE")
if [ -z "$MESSAGES" ]; then
echo "No more messages in the dead letter queue. Exiting."
exit 0
fi
echo "Processing messages:"
echo "$MESSAGES"
while read -r receipt_handle; do
RECEIPT_HANDLES="${RECEIPT_HANDLES} ${receipt_handle}"
done < <(echo "$MESSAGES" | jq -r '.Messages[] | .ReceiptHandle')
for receipt_handle in $RECEIPT_HANDLES; do
MESSAGE=$(echo "$MESSAGES" | jq -r --arg RECEIPT_HANDLE "$receipt_handle" '.Messages[] | select(.ReceiptHandle==$RECEIPT_HANDLE)')
BODY=$(echo "$MESSAGE" | jq -r '.Body')
aws sqs send-message --queue-url "$TARGET_SQS_URL" --message-body "$BODY"
aws sqs delete-message --queue-url "$DLQ_URL" --receipt-handle "$receipt_handle"
done
echo "Batch processed. Waiting for $WAIT_TIME seconds before processing the next batch."
sleep $WAIT_TIME
done
This script assumes that you have the AWS CLI and jq installed, and your AWS credentials are properly configured. If not, please follow the AWS CLI installation and configuration instructions here: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html