SQS Message Retrieval (File Based)

20 May 2021, Posted by BoB in All Posts, Computer Talk

First, thank you to Tamás Sallai for getting me on the right track. I originally started building this script with “for msg in `aws sqs receive-message –queue-url $QUEUE_URL –max-number-of-messages 10`” but then came across his post. For those struggling with this right now hope it helps.


#script runs on amazon linux os
#script requires IAM user and zone to be configured (aws configure)
#script requires jq (JSON processor) to be installed

#set queue url

#polls every second upon completion of the loop
while sleep 1
MSG=$(aws sqs receive-message –queue-url $QUEUE_URL –max-number-of-messages 5)

[ ! -z “$MSG” ]

echo “$MSG” | jq -r ‘.Messages[] | .ReceiptHandle’ | (xargs -I {} aws sqs delete-message –queue-url $QUEUE_URL –receipt-handle {})

echo “$MSG” | jq -r ‘.Messages[] | .Body’ >> sqs_`date +%s`.json

Post a comment