Skip to content

Instantly share code, notes, and snippets.

@ivawzh
Created March 16, 2023 23:34
Show Gist options
  • Save ivawzh/6df0dafcbca4f02147b2e18fd55f004e to your computer and use it in GitHub Desktop.
Save ivawzh/6df0dafcbca4f02147b2e18fd55f004e to your computer and use it in GitHub Desktop.
refeed from dead letter queue

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment