View a markdown version of this page

Amazon SQS examples using AWS CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Amazon SQS examples using AWS CLI

The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with Amazon SQS.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use add-permission.

AWS CLI

To add a permission to a queue

The following add-permission example enables the specified AWS account to send messages to the specified queue.

aws sqs add-permission \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --label SendMessagesFromMyQueue \ --aws-account-ids 12345EXAMPLE \ --actions SendMessage

This command produces no output.

For more information, see Overview of managing access in Amazon SQS in the Amazon Simple Queue Service Developer Guide.

  • For API details, see AddPermission in AWS CLI Command Reference.

The following code example shows how to use cancel-message-move-task.

AWS CLI

To cancel a message move task

The following cancel-message-move-task example cancels the specified message move task.

aws sqs cancel-message-move-task \ --task-handle AQEB6nR4...HzlvZQ==

Output:

{ "ApproximateNumberOfMessagesMoved": 102 }

For more information, see Amazon SQS API permissions: Actions and resource reference in the Developer Guide.

The following code example shows how to use change-message-visibility-batch.

AWS CLI

To change multiple messages' timeout visibilities as a batch

The following change-message-visibility-batch example changes the 2 specified messages' timeout visibilities to 10 hours (10 hours * 60 minutes * 60 seconds).

aws sqs change-message-visibility-batch \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --entries file://change-message-visibility-batch.json

Contents of change-message-visibility-batch.json:

[ { "Id": "FirstMessage", "ReceiptHandle": "AQEBhz2q...Jf3kaw==", "VisibilityTimeout": 36000 }, { "Id": "SecondMessage", "ReceiptHandle": "AQEBkTUH...HifSnw==", "VisibilityTimeout": 36000 } ]

Output:

{ "Successful": [ { "Id": "SecondMessage" }, { "Id": "FirstMessage" } ] }

For more information, see Amazon SQS visibility timeout in the Amazon Simple Queue Service Developer Guide.

The following code example shows how to use change-message-visibility.

AWS CLI

To change a message's timeout visibility

The following change-message-visibility example changes the specified message's timeout visibility to 10 hours (10 hours * 60 minutes * 60 seconds).

aws sqs change-message-visibility \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --receipt-handle AQEBTpyI...t6HyQg== \ --visibility-timeout 36000

This command produces no output.

For more information, see Amazon SQS visibility timeout in the Amazon Simple Queue Service Developer Guide.

The following code example shows how to use create-queue.

AWS CLI

To create a queue

The following create-queue example creates a queue with the specified name, sets the message retention period to 3 days (3 days * 24 hours * 60 minutes * 60 seconds), and sets the queue's dead letter queue to the specified queue with a maximum receive count of 1,000 messages.

aws sqs create-queue \ --queue-name MyQueue \ --attributes file://create-queue.json

Contents of create-queue.json:

{ "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-west-2:80398EXAMPLE:MyDeadLetterQueue\",\"maxReceiveCount\":\"1000\"}", "MessageRetentionPeriod": "259200" }

Output:

{ "QueueUrl": "https://queue.amazonaws.com/80398EXAMPLE/MyQueue" }

For more information, see Using dead-letter queues in Amazon SQS in the Amazon Simple Queue Service Developer Guide.

  • For API details, see CreateQueue in AWS CLI Command Reference.

The following code example shows how to use delete-message-batch.

AWS CLI

To delete multiple messages as a batch

The following delete-message-batch example deletes the specified messages.

aws sqs delete-message-batch \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --entries file://delete-message-batch.json

Contents of delete-message-batch.json:

[ { "Id": "FirstMessage", "ReceiptHandle": "AQEB1mgl...Z4GuLw==" }, { "Id": "SecondMessage", "ReceiptHandle": "AQEBLsYM...VQubAA==" } ]

Output:

{ "Successful": [ { "Id": "FirstMessage" }, { "Id": "SecondMessage" } ] }

For more information, see Amazon SQS batch actions in the Amazon Simple Queue Service Developer Guide.

The following code example shows how to use delete-message.

AWS CLI

To delete a message

The following delete-message example deletes the specified message.

aws sqs delete-message \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --receipt-handle AQEBRXTo...q2doVA==

This command produces no output.

For more information, see Receiving and deleting a message in Amazon SQS in the Amazon Simple Queue Service Developer Guide.

  • For API details, see DeleteMessage in AWS CLI Command Reference.

The following code example shows how to use delete-queue.

AWS CLI

To delete a queue

The following delete-queue example deletes the specified queue.

aws sqs delete-queue \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyNewerQueue

This command produces no output.

For more information, see Deleting an Amazon SQS queue in the Amazon Simple Queue Service Developer Guide.

  • For API details, see DeleteQueue in AWS CLI Command Reference.

The following code example shows how to use get-queue-attributes.

AWS CLI

To get a queue's attributes

The following get-queue-attributes example gets all of the specified queue's attributes.

aws sqs get-queue-attributes \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --attribute-names All

Output:

{ "Attributes": { "ApproximateNumberOfMessagesNotVisible": "0", "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-west-2:80398EXAMPLE:MyDeadLetterQueue\",\"maxReceiveCount\":1000}", "MessageRetentionPeriod": "345600", "ApproximateNumberOfMessagesDelayed": "0", "MaximumMessageSize": "262144", "CreatedTimestamp": "1442426968", "ApproximateNumberOfMessages": "0", "ReceiveMessageWaitTimeSeconds": "0", "DelaySeconds": "0", "VisibilityTimeout": "30", "LastModifiedTimestamp": "1442426968", "QueueArn": "arn:aws:sqs:us-west-2:80398EXAMPLE:MyNewQueue" } }

For more information, see Configuring queue parameters using the Amazon SQS console in the Amazon Simple Queue Service Developer Guide.

The following get-queue-attributes example gets only the specified queue's maximum message size and visibility timeout attributes.

aws sqs get-queue-attributes \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyNewQueue \ --attribute-names MaximumMessageSize VisibilityTimeout

Output:

{ "Attributes": { "VisibilityTimeout": "30", "MaximumMessageSize": "262144" } }

For more information, see Configuring queue parameters using the Amazon SQS console in the Amazon Simple Queue Service Developer Guide.

The following code example shows how to use get-queue-url.

AWS CLI

To get a queue URL

The following get-queue-url example gets the specified queue's URL.

aws sqs get-queue-url \ --queue-name MyQueue

Output:

{ "QueueUrl": "https://queue.amazonaws.com/80398EXAMPLE/MyQueue" }

For more information, see Amazon SQS queue and message identifiers in the Amazon Simple Queue Service Developer Guide.

  • For API details, see GetQueueUrl in AWS CLI Command Reference.

The following code example shows how to use list-dead-letter-source-queues.

AWS CLI

To list dead letter source queues

The following list-dead-letter-source-queues example lists the queues that are associated with the specified dead letter source queue.

aws sqs list-dead-letter-source-queues \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyDeadLetterQueue

Output:

{ "queueUrls": [ "https://queue.amazonaws.com/80398EXAMPLE/MyQueue", "https://queue.amazonaws.com/80398EXAMPLE/MyOtherQueue" ] }

For more information, see Using dead-letter queues in Amazon SQS in the Amazon Simple Queue Service Developer Guide.

The following code example shows how to use list-message-move-tasks.

AWS CLI

To list the message move tasks

The following list-message-move-tasks example lists the 2 most recent message move tasks in the specified queue.

aws sqs list-message-move-tasks \ --source-arn arn:aws:sqs:us-west-2:80398EXAMPLE:MyQueue \ --max-results 2

Output:

{ "Results": [ { "TaskHandle": "AQEB6nR4...HzlvZQ==", "Status": "RUNNING", "SourceArn": "arn:aws:sqs:us-west-2:80398EXAMPLE:MyQueue1", "DestinationArn": "arn:aws:sqs:us-west-2:80398EXAMPLE:MyQueue2", "MaxNumberOfMessagesPerSecond": 50, "ApproximateNumberOfMessagesMoved": 203, "ApproximateNumberOfMessagesToMove": 30, "StartedTimestamp": 1442428276921 }, { "Status": "COMPLETED", "SourceArn": "arn:aws:sqs:us-west-2:80398EXAMPLE:MyQueue1", "DestinationArn": "arn:aws:sqs:us-west-2:80398EXAMPLE:MyQueue2", "ApproximateNumberOfMessagesMoved": 29, "ApproximateNumberOfMessagesToMove": 0, "StartedTimestamp": 1342428272093 } ] }

For more information, see Amazon SQS API permissions: Actions and resource reference in the Developer Guide.

The following code example shows how to use list-queue-tags.

AWS CLI

To list all cost allocation tags for a queue

The following list-queue-tags example displays all of the cost allocation tags associated with the specified queue.

aws sqs list-queue-tags \ --queue-url https://sqs.us-west-2.amazonaws.com/123456789012/MyQueue

Output:

{ "Tags": { "Team": "Alpha" } }

For more information, see Listing Cost Allocation Tags in the Amazon Simple Queue Service Developer Guide.

  • For API details, see ListQueueTags in AWS CLI Command Reference.

The following code example shows how to use list-queues.

AWS CLI

To list queues

The following list-queues example lists all queues.

aws sqs list-queues

Output:

{ "QueueUrls": [ "https://queue.amazonaws.com/80398EXAMPLE/MyDeadLetterQueue", "https://queue.amazonaws.com/80398EXAMPLE/MyQueue", "https://queue.amazonaws.com/80398EXAMPLE/MyOtherQueue", "https://queue.amazonaws.com/80398EXAMPLE/TestQueue1", "https://queue.amazonaws.com/80398EXAMPLE/TestQueue2" ] }

For more information, see Amazon SQS list queue pagination in the Amazon Simple Queue Service Developer Guide.

The following list-queues example lists only queues that start with "My".

aws sqs list-queues \ --queue-name-prefix My

Output:

{ "QueueUrls": [ "https://queue.amazonaws.com/80398EXAMPLE/MyDeadLetterQueue", "https://queue.amazonaws.com/80398EXAMPLE/MyQueue", "https://queue.amazonaws.com/80398EXAMPLE/MyOtherQueue" ] }

For more information, see Amazon SQS list queue pagination in the Amazon Simple Queue Service Developer Guide.

  • For API details, see ListQueues in AWS CLI Command Reference.

The following code example shows how to use purge-queue.

AWS CLI

To purge a queue

The following purge-queue example deletes all messages in the specified queue.

aws sqs purge-queue \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyNewQueue

This command produces no output.

For more information, see Purging messages from an queue using the Amazon SQS console in the Amazon Simple Queue Service Developer Guide.

  • For API details, see PurgeQueue in AWS CLI Command Reference.

The following code example shows how to use receive-message.

AWS CLI

To receive a message

The following receive-message example receives up to 10 available messages, returning all available attributes.

aws sqs receive-message \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --attribute-names All \ --message-attribute-names All \ --max-number-of-messages 10

Output:

{ "Messages": [ { "Body": "My first message.", "ReceiptHandle": "AQEBzbVv...fqNzFw==", "MD5OfBody": "1000f835...a35411fa", "MD5OfMessageAttributes": "9424c491...26bc3ae7", "MessageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "Attributes": { "ApproximateFirstReceiveTimestamp": "1442428276921", "SenderId": "AIDAIAZKMSNQ7TEXAMPLE", "ApproximateReceiveCount": "5", "SentTimestamp": "1442428276921" }, "MessageAttributes": { "PostalCode": { "DataType": "String", "StringValue": "ABC123" }, "City": { "DataType": "String", "StringValue": "Any City" } } } ] }

The following receive-message example receives the next available message, returning only the SenderId and SentTimestamp attributes as well as the PostalCode message attribute.

aws sqs receive-message \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --attribute-names SenderId SentTimestamp \ --message-attribute-names PostalCode

Output:

{ "Messages": [ { "Body": "My first message.", "ReceiptHandle": "AQEB6nR4...HzlvZQ==", "MD5OfBody": "1000f835...a35411fa", "MD5OfMessageAttributes": "b8e89563...e088e74f", "MessageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "Attributes": { "SenderId": "AIDAIAZKMSNQ7TEXAMPLE", "SentTimestamp": "1442428276921" }, "MessageAttributes": { "PostalCode": { "DataType": "String", "StringValue": "ABC123" } } } ] }

For more information, see Receiving and deleting a message in Amazon SQS in the Amazon Simple Queue Service Developer Guide.

For more information, see Receiving and deleting a message in Amazon SQS in the Amazon Simple Queue Service Developer Guide.

The following code example shows how to use remove-permission.

AWS CLI

To remove a permission

The following remove-permission example removes the permission with the specified label from the specified queue.

aws sqs remove-permission \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --label SendMessagesFromMyQueue

This command produces no output.

For more information, see Overview of managing access in Amazon SQS in the Amazon Simple Queue Service Developer Guide.

The following code example shows how to use send-message-batch.

AWS CLI

To send multiple messages as a batch

The following send-message-batch example sends 2 messages with the specified message bodies, delay periods, and message attributes, to the specified queue.

aws sqs send-message-batch \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --entries file://send-message-batch.json

Contents of send-message-batch.json:

[ { "Id": "FuelReport-0001-2015-09-16T140731Z", "MessageBody": "Fuel report for account 0001 on 2015-09-16 at 02:07:31 PM.", "DelaySeconds": 10, "MessageAttributes": { "SellerName": { "DataType": "String", "StringValue": "Example Store" }, "City": { "DataType": "String", "StringValue": "Any City" }, "Region": { "DataType": "String", "StringValue": "WA" }, "PostalCode": { "DataType": "String", "StringValue": "99065" }, "PricePerGallon": { "DataType": "Number", "StringValue": "1.99" } } }, { "Id": "FuelReport-0002-2015-09-16T140930Z", "MessageBody": "Fuel report for account 0002 on 2015-09-16 at 02:09:30 PM.", "DelaySeconds": 10, "MessageAttributes": { "SellerName": { "DataType": "String", "StringValue": "Example Fuels" }, "City": { "DataType": "String", "StringValue": "North Town" }, "Region": { "DataType": "String", "StringValue": "WA" }, "PostalCode": { "DataType": "String", "StringValue": "99123" }, "PricePerGallon": { "DataType": "Number", "StringValue": "1.87" } } } ]

Output:

{ "Successful": [ { "MD5OfMessageBody": "203c4a38...7943237e", "MD5OfMessageAttributes": "10809b55...baf283ef", "Id": "FuelReport-0001-2015-09-16T140731Z", "MessageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" }, { "MD5OfMessageBody": "2cf0159a...c1980595", "MD5OfMessageAttributes": "55623928...ae354a25", "Id": "FuelReport-0002-2015-09-16T140930Z", "MessageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222" } ] }

For more information, see Amazon SQS batch actions in the Amazon Simple Queue Service Developer Guide.

The following code example shows how to use send-message.

AWS CLI

To send a message

The following send-message example sends a message with the specified message body, delay period, and message attributes, to the specified queue.

aws sqs send-message \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyQueue \ --message-body "Information about the largest city in Any Region." \ --delay-seconds 10 \ --message-attributes file://send-message.json

Contents of send-message.json:

{ "City": { "DataType": "String", "StringValue": "Any City" }, "Greeting": { "DataType": "Binary", "BinaryValue": "Hello, World!" }, "Population": { "DataType": "Number", "StringValue": "1250800" } }

Output:

{ "MD5OfMessageBody": "51b0a325...39163aa0", "MD5OfMessageAttributes": "00484c68...59e48f06", "MessageId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" }

For more information, see Sending a message with attributes using Amazon SQS in the Amazon Simple Queue Service Developer Guide.

  • For API details, see SendMessage in AWS CLI Command Reference.

The following code example shows how to use set-queue-attributes.

AWS CLI

To set queue attributes

The following set-queue-attributes example sets the specified queue to a delivery delay of 10 seconds, a maximum message size of 128 KB (128 KB * 1,024 bytes), a message retention period of 3 days (3 days * 24 hours * 60 minutes * 60 seconds), a receive message wait time of 20 seconds, and a default visibility timeout of 60 seconds. This example also associates the specified dead letter queue with a maximum receive count of 1,000 messages.

aws sqs set-queue-attributes \ --queue-url https://sqs.us-west-2.amazonaws.com/80398EXAMPLE/MyNewQueue \ --attributes file://set-queue-attributes.json

Contents of set-queue-attributes.json:

{ "DelaySeconds": "10", "MaximumMessageSize": "131072", "MessageRetentionPeriod": "259200", "ReceiveMessageWaitTimeSeconds": "20", "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-west-2:80398EXAMPLE:MyDeadLetterQueue\",\"maxReceiveCount\":\"1000\"}", "VisibilityTimeout": "60" }

This command produces no output.

For more information, see Configuring queue parameters using the Amazon SQS console in the Amazon Simple Queue Service Developer Guide.

The following code example shows how to use start-message-move-task.

AWS CLI

Example 1: To start a message move task

The following start-message-move-task example starts a message move task to redrive messages from the specified dead-letter queue to the source queue.

aws sqs start-message-move-task \ --source-arn arn:aws:sqs:us-west-2:80398EXAMPLE:MyQueue

Output:

{ "TaskHandle": "AQEB6nR4...HzlvZQ==" }

For more information, see Learn how to configure a dead-letter queue redrive in Amazon SQS in the Amazon SQS Developer Guide.

Example 2: To start a message move task with a maximum rate

The following start-message-move-task example starts a message move task to redrive messages from the specified dead-letter queue to the specified destination queue at a maximum rate of 50 messages per second.

aws sqs start-message-move-task \ --source-arn arn:aws:sqs:us-west-2:80398EXAMPLE:MyQueue1 \ --destination-arn arn:aws:sqs:us-west-2:80398EXAMPLE:MyQueue2 \ --max-number-of-messages-per-second 50

Output:

{ "TaskHandle": "AQEB6nR4...HzlvZQ==" }

For more information, see Amazon SQS API permissions: Actions and resource reference in the Developer Guide.

The following code example shows how to use tag-queue.

AWS CLI

To add cost allocation tags to a queue

The following tag-queue example adds a cost allocation tag to the specified Amazon SQS queue.

aws sqs tag-queue \ --queue-url https://sqs.us-west-2.amazonaws.com/123456789012/MyQueue \ --tags Priority=Highest

This command produces no output.

For more information, see Adding Cost Allocation Tags in the Amazon Simple Queue Service Developer Guide.

  • For API details, see TagQueue in AWS CLI Command Reference.

The following code example shows how to use untag-queue.

AWS CLI

To remove cost allocation tags from a queue

The following untag-queue example removes a cost allocation tag from the specified Amazon SQS queue.

aws sqs untag-queue \ --queue-url https://sqs.us-west-2.amazonaws.com/123456789012/MyQueue \ --tag-keys "Priority"

This command produces no output.

For more information, see Adding Cost Allocation Tags in the Amazon Simple Queue Service Developer Guide.

  • For API details, see UntagQueue in AWS CLI Command Reference.