Skip to content

Instantly share code, notes, and snippets.

@luads
Last active March 26, 2023 20:42
Show Gist options
  • Save luads/adef2b78a42b6d4234d4a62792f26e3c to your computer and use it in GitHub Desktop.
Save luads/adef2b78a42b6d4234d4a62792f26e3c to your computer and use it in GitHub Desktop.
Replay lambda from DLQ message
function replay_lambda() {
local profile=$1
local fn=$2
sqs_message="initialising..."
dlq_arn=$(aws --profile=$profile lambda get-function --function-name "$fn" | jq -r '.Configuration.DeadLetterConfig.TargetArn')
dlq=$(aws --profile=$profile sqs get-queue-url --queue-name "$(echo ${dlq_arn##*:})" --output=text)
while [ ! -z "$sqs_message" ]; do
sqs_message=$(aws --profile=$profile sqs receive-message --queue-url $dlq | jq -c '.')
if [ -z "$sqs_message" ]; then echo "No messages left on queue"; kill -INT $$; fi
payload=$(jq -c '.Messages[].Body' <<< "$sqs_message" | sed 's/\\"/"/g' | sed 's/\\\\"/\\"/g' | sed -e 's/^"//' -e 's/"$//')
receipt_handle=$(jq -r '.Messages[].ReceiptHandle' <<< $sqs_message)
message_id=$(jq -r '.Messages[].MessageId' <<< $sqs_message)
echo $payload > /tmp/lambda-request.json
lambda_result=$(aws --profile=$profile lambda invoke --function-name $fn --payload fileb:///tmp/lambda-request.json /dev/null | jq -r '.StatusCode')
aws --profile=$profile sqs delete-message --queue-url $dlq --receipt-handle $receipt_handle > /dev/null
echo "Replayed $message_id, status: $lambda_result"
done
rm /tmp/lambda-request.json
echo "All done!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment