This tiny guide assumes that you already know what is AWS SQS and how it really works! Here we will demonstrate how we can use AWS CLI to see how SQS it works.
Step1: Create an SQS Queue
Please note that I am keeping the queue very simple in order keep the demonstration super quick and elementary. But, I am making the message retention period 3 minutes, deliberately to not to waste our time in showing messages actually getting erased from the queue.
Create a file q-attributes.json with this content.
Now, assuming that you have AWS CLI installed and configured, do this.
aws sqs create-queue --queue-name sreeQ --attributes file://q-attributes.json
You should see an output like this. Preserve the Q url, we will need it for the reaming part of this demo.
If you wish to see the SQS queue attributes, you can do this.
aws sqs get-queue-attributes --queue-url https://ap-south-1.queue.amazonaws.com/nnn/sreeQ --attribute-names All
You should see something like this.
Step-2: Sending Messages to our AWS SQS Queue
Do this to send one messages to the queue. Repeat this command to send more messages. In reality message producers will be sending such messages using a programming language and it’s AWS SDK.
aws sqs send-message --queue-url https://ap-south-1.queue.amazonaws.com/nnn/sreeQ --message-body "IOT-1 Temp: 51C"
The output can look something like this.
Step-3 Reading Messages from the AWS SQS Queue
It is time to fetch our messages (for further processing by client applications).
aws sqs --region ap-south-1 receive-message --queue-url https://ap-south-1.queue.amazonaws.com/nnn/sreeQ
Output can look like this. Our message along with some more identifiers like MessageId, ReceiptHandle etc. comes as seen below. We also get the MD5 of the message as well. Ensure that our client applications validate the messages using this.
"Body": "IOT-1 Temp: 51C"
Step-3: What Should Happen Now!
- If you repeat the above “receive-message” command immediately or with the visibility timeout of 30 seconds— same message will not (re)appear
- If you repeat the above “receive-message” command after the 30 seconds (visibility timeout)— same message will reappear.
- If you repeat the above “receive-message” command after the 3 minutes of retention period — message will never again appear and would have been permanently deleted.
Please repeat the “receive-message” command to see the above scenarios.
Step-4: Deletion of a Processed Message from the AWS SQS Queue
OK, now that our receiver application has received a message and acted on it (within the visibility timeout period), we can delete the message (need the message handle from above). Remember to fine tune the visibility timeout to ensure that we have sufficient time to process a message, or else the same message could get processed multiple times! But, even i that happens your system should stay safe.
aws sqs --region ap-south-1 delete-message --queue-url https://ap-south-1.queue.amazonaws.com/nnn/sreeQ --receipt-handle "AQEB9P5rdqGshIuHIlxBrrfc8GFyDweOh6kuHoWXmskg7AZLjkDxorkZTfBe10AdHxADAVSKcE5ovnM/g3cddowWtv4EuZNqZ3b0efT/im76eR5mfihPPJEg0csTSTjkr0Wi9qOF85dw7xoO8MakbG/LFcFp0LOoHRCp6z7BtR8+24C3s2kVVLyBU8RhQri5kmzxTk1UNqDPULXbDGAqb8BC3WhE9C+LQjTQDtHUWdEjJWPPa4TMhRclQzPhacchGrEGqWm8fVQDsuCY7Qm5hE40IclNtZxNTSbueAuJMOAiQPCnepExpLkoMXEFfsfshg5+hJe86pGMGzI8R3ntpuFNvjqvj7wIy5MZMDGrNFho23oA1A8t/Ct9MOZQLeniC3Lu"
Step-4 Cleaning Up
OK, time to delete our SQS queue.
aws sqs --region ap-south-1 delete-queue --queue-url https://ap-south-1.queue.amazonaws.com/nnn/sreeQ
Thank you for your time, do follow me for more such tiny demos!