Creating a FIFO queue using the AWS CLI

Working with the AWS Management Console is easy enough, but the AWS CLI makes things even simpler! In this section, we will look at a few simple AWS CLI commands that you can use to create and work on your first FIFO queue:

  1. To get started, we require a server or instance with the latest version of the AWS CLI installed and configured. If you don't already have this working, you might want to have a quick look at the detailed steps provided at https://docs.aws.amazon.com/cli/latest/userguide/installing.html.
  2. With the AWS CLI installed and prepped, you can now use the following command to create your first FIFO queue. First, create a simple JSON file that will store the necessary list of attributes that we wish to pass to our new FIFO queue:
# vi fifo-queue.json 
#### PASTE THE FOLLOWING CONTENTS ##### 
{"VisibilityTimeout" : "30", 
"MaximumMessageSize" : "262144", 
"MessageRetentionPeriod" : "345600", 
"DelaySeconds" : "10", 
"ReceiveMessageWaitTimeSeconds" : "0", 
"FifoQueue" : "true", 
"ContentBasedDeduplication" : "true" 
} 

Here, most of the values are probably known to you already, such as the VisibilityTimeout, the MaximumMessageSize, DelaySeconds, and so on. The two new attributes listed here specifically for the FIFO queue are:

    • FifoQueue: Used to designate a queue as a FIFO queue. Note that you cannot change an existing standard queue to a FIFO queue. You will have to create a new FIFO queue altogether. Additionally, when you set this attribute for your queue, you must also provide the MessageGroupId for your messages explicitly.
    • ContentBasedDeduplication: It enables each message to be processed exactly one time from the queue. Once ContentBasedDeduplication is enabled, messages with identical content sent within the deduplication interval are treated as duplicates and only one copy of the message is actually delivered.
  1. Once the JSON file is created, run the following command to create your FIFO queue:
# aws sqs create-queue --queue-name myQueue.fifo
--attributes file://fifo-queue.json 

You should receive the new FIFO queues endpoint URL in the output, as shown in the following screenshot:

  1. With the queue created, you can additionally use the CLI to pass messages to the queue as well. This is also accomplished by using the following command:
# aws sqs send-message  
--queue-url https://queue.amazonaws.com/012345678910/myQueue.fifo  
--message-body "Well this is far easier than I expected."  
--message-group-id "R@nD0M" 

The send-message command accepts the queue URL as one of the input parameters, along with the actual message that has to be sent. The message can be raw, JSON, or XML formatted. In addition to this, the send-message command also uses the --message-group-id parameter that essentially tags the message to belong to a specific message group. Messages that belong to the same message group are processed in a FIFO manner:

The --message-id-group parameter is mandatory when working with FIFO queues.
  1. With the message now sent to the queue, you can use the AWS CLI to receive the message as well. Use the following command to fetch the messages from your FIFO queue:
# aws sqs receive-message  
--queue-url https://queue.amazonaws.com/012345678910/myQueue.fifo 

You can also additionally use the --max-number-of-messages attribute to list up to 10 messages that are currently available in the queue. Here is a snippet of the output that you may get with the previous command:

{ 
    "Messages": [ 
        { 
            "Body": "Well this is far easier than I expected.", 
            "ReceiptHandle": "AQnmzJjGNrI9cl7ZyZ2NyVDDDy==", 
            "MD5OfBody": "d733b7da2656ffc18d99bea3613e24d7", 
            "MessageId": "a075bd88-4942-416d-b632-0258ac8" 
        } 
    ] 
} 

You can similarly use the AWS CLI to list the available queues in your environment, modify their parameters, push and poll for new messages, delete messages, and much more! Remember, the messages will persist in the queue unless you manually delete them or the validity of the queue's MessageRetentionPeriod has expired.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset