How To Configure SQS Using AWS CLI

How To Configure SQS Using AWS CLI

SQS stands for Amazon Simple Queue Service.

It is flexible, reliable, secure, highly-scalable, easy & fully managed message queuing service for storing unlimited messages.

Using SQS we can sends multiple message at same times.

It support two types of queue, Standard and FIFO (First In First Out) queues.

"Standard" queues are available in all regions & "FIFO" queues are available only in the US East (N. Virginia), US East (Ohio), US West (Oregon), and EU (Ireland) regions.

If you don't specify the FifoQueue attribute while creating queue then by default it will create a Standard queue.

Important Notes:
  • Once you created a queue you can't update queue type.
  • If you forget to provide a value for an attribute then queue will use default value for that attribute.
  • If you delete a queue then you must wait at least 60 seconds before creating a queue with the same name.
  • If you provide the name of an existing queue along with the exact name and values of all the queue's attributes then create-queue returns the queue URL for the existing queue.
  • If the queue name, attribute names, or attribute values don't match an existing queue then create-queue returns an error.
  • A queue name can have up to 80 characters.
  • A queue name can be of alphanumeric characters, hyphens (- ), and underscores (_ ).
  • A FIFO queue name must end with the .fifo suffix.
  • Queue names are case-sensitive.
  • A message can include only XML, JSON, and unformatted text. The Unicode characters are allowed: For example: #x9 | #xA | #xD | #x20 to #xD7FF | #xE000 to #xFFFD | #x10000 to #x10FFFF etc
  • AWS SQS does not delete message once it has been received by receiver because of distributed system and it has no guarantee that receiver received that message or not due to some technical difficulties. Therefore it is receiver responsibility to delete message from queue. 
  • SQS provides 'visibility period' is a period for which message become unavailable for specific period after being received by consumer / receiver.

Use Case:
You can use Amazon SQS when you want to move data between distributed application components.

Lets see one by one...

How to create a queue:

It require queue name and attributes like DelaySeconds, MaximumMessageSize etc. we can also save these attribute values in a JSON file and pass it instead of passing attributes directly.

Create a JSON file name as attributejson.json and write below attribute.

{"MessageRetentionPeriod":"259200"} 

This parameter tells AWS SQS to persist the message in queue for that particular time in seconds.

In above example we set it for 3 days (3 days * 24 hours * 60 minutes * 60 seconds).

>> aws sqs create-queue --queue-name pgqueue --attributes file://attributejson.json

Once you execute command above it will output the URL of newly created queue. Save this URL for future utilization.

Output:
{
  "QueueUrl": "https://queue.amazonaws.com/099998EXAMPLE/pgqueue"
}

To verify we can use command below.

>> aws sqs list-queues 


How to send messages in new SQS Queue:

>> aws sqs send-message --queue-url --message-body "This is my first queue message"

AWS provides 'send-message' command to send message to any specified queue and parameter we need to pass --queue-url and --messgae-body.

Once we execute above command it will send that message into that queue. You can also verify this from AWS management console.


How to receive/delete queue message:

AWS provides a command that is --receive-message and only one parameter need to pass which is --queue-url from which want to receive messages.

>> aws sqs receive-message --queue-url 

From the output you can note receipt-handle to delete this message in future.

>> aws sqs delete-message --queue-url --receipt-handle


How to send message with delay in queue:

We can send message in queue with specific time delay that means the message will be available for processing after that delayed time finished.

>>aws sqs send-message --queue-url --message-body "Message with 10 second delay" --delay-seconds 10

Here we set delay of 10 seconds that means the message will be delivered to queue after 10 seconds. in this command we just need to pass extra parameter i.e --delay-seconds with value in seconds.


How to delete SQS queue:

SQS provides a command delete-queue with parameter queue-url to delete queue.

>>aws sqs delete-queue --queue-url

We can verify deleted queue by command below.

>> aws sqs list-queues



Happy Learning AWS Services!!!! :) Still Doubts? lets put your questions in below comment box! Thanks!

Watch Video:


Reference: https://docs.aws.amazon.com/cli/latest/reference/sqs/index.html


How To Configure SQS Using AWS CLI How To Configure SQS Using AWS CLI Reviewed by Web Technology Funda on 7:06:00 AM Rating: 5

No comments

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!