AWS CLI and SQS — a Tiny Demonstration

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

Create a file q-attributes.json with this content.

{
"MessageRetentionPeriod": "180"
}

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.

{
"QueueUrl": "https://ap-south-1.queue.amazonaws.com/nnn/sreeQ"
}

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.

{
"Attributes": {
"QueueArn": "arn:aws:sqs:ap-south-1:nnn:sreeQ",
"ApproximateNumberOfMessages": "0",
"ApproximateNumberOfMessagesNotVisible": "0",
"ApproximateNumberOfMessagesDelayed": "0",
"CreatedTimestamp": "1540707132",
"LastModifiedTimestamp": "1540707132",
"VisibilityTimeout": "30",
"MaximumMessageSize": "262144",
"MessageRetentionPeriod": "180",
"DelaySeconds": "0",
"ReceiveMessageWaitTimeSeconds": "0"
}
}

Step-2: Sending Messages to our AWS SQS Queue

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.

{
"MD5OfMessageBody": "8947cd39e126a7a7985cc884c05e8bfa",
"MessageId": "a5fb9702-ecfb-4ad3-8791-31b08dba5ae0"
}

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.

{
"Messages": [
{
"MessageId": "e7c287de-c4a6-4158-a0a8-2b986e346518",
"ReceiptHandle": "AQEB9P5rdqGshIuHIlxBrrfc8GFyDweOh6kuHoWXmskg7AZLjkDxorkZTfBe10AdHxADAVSKcE5ovnM/g3cddowWtv4EuZNqZ3b0efT/im76eR5mfihPPJEg0csTSTjkr0Wi9qOF85dw7xoO8MakbG/LFcFp0LOoHRCp6z7BtR8+24C3s2kVVLyBU8RhQri5kmzxTk1UNqDPULXbDGAqb8BC3WhE9C+LQjTQDtHUWdEjJWPPa4TMhRclQzPhacchGrEGqWm8fVQDsuCY7Qm5hE40IclNtZxNTSbueAuJMOAiQPCnepExpLkoMXEFfsfshg5+hJe86pGMGzI8R3ntpuFNvjqvj7wIy5MZMDGrNFho23oA1A8t/Ct9MOZQLeniC3Lu",
"MD5OfBody": "e317cf0ea6dee8619f0c734d8dc4ee8a",
"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!

AWS Certified DevOps Engineer & Solutions Architect Professional — Docker | Kubernetes | DevOps — Trainer | Running | Swimming | Cycling

AWS Certified DevOps Engineer & Solutions Architect Professional — Docker | Kubernetes | DevOps — Trainer | Running | Swimming | Cycling