30

Just a simple question, but I can't seen to find the answer.

Is it possible to use the API, to get the queue size (the number of messages/jobs waiting to be processed) of an AWS SQS queue?

Preferably using cURL or the PHP SDK.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Martijn Thomas
  • 861
  • 3
  • 13
  • 24
  • For Java Loot At: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_GetQueueAttributes.html – ImtiazeA Oct 01 '21 at 10:17

7 Answers7

18

I believe what you are looking for is get-queue-attributes, perhaps interrogating the ApproximateNumberOfMessages attribute.

dangerousdave
  • 6,331
  • 8
  • 45
  • 62
  • 1
    Also available in the AWS SDK for PHP: http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.Sqs.SqsClient.html#_getQueueAttributes – Jeremy Lindblom Aug 18 '14 at 07:40
  • "ApproximateNumberOfMessages" is a good way. But this might be inaccurate if multiple consumers are consuming from the queue. – Keet Sugathadasa Apr 19 '18 at 04:05
  • Is this value updated relatively quickly or in real-time? Any idea on the delay for this value to be updated after an enqueue/dequeue? – Kubie Oct 18 '21 at 13:27
17

And with some code examples:

aws sqs get-queue-attributes --queue-url https://sqs.<region>.amazonaws.com/<accountId>/<SQS name> --attribute-names All


{
    "Attributes": {
        "QueueArn": "arn:aws:sqs:<region>:<accountId>:<SQS name>",
        "ApproximateNumberOfMessages": "0",
        "ApproximateNumberOfMessagesNotVisible": "3",
        "ApproximateNumberOfMessagesDelayed": "0",
        "CreatedTimestamp": "1594729555",
        "LastModifiedTimestamp": "1595845586",
        "VisibilityTimeout": "60",
        "MaximumMessageSize": "262144",
        "MessageRetentionPeriod": "900",
        "DelaySeconds": "0",
        "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:<region>:<accountId>:<DLQ name>\",\"maxReceiveCount\":3}",
        "ReceiveMessageWaitTimeSeconds": "0"
    }
}

Get values of defined attributes:

 aws sqs get-queue-attributes --queue-url https://sqs.<region>.amazonaws.com/<accountId>/<SQS name> --attribute-names VisibilityTimeout ApproximateNumberOfMessages ApproximateNumberOfMessagesNotVisible ApproximateNumberOfMessagesDelayed

{
    "Attributes": {
        "VisibilityTimeout": "60",
        "ApproximateNumberOfMessages": "0",
        "ApproximateNumberOfMessagesNotVisible": "3",
        "ApproximateNumberOfMessagesDelayed": "0"
    }
}

Warning

The ApproximateNumberOfMessagesDelayed , ApproximateNumberOfMessagesNotVisible , and ApproximateNumberOfMessagesVisible metrics may not achieve consistency until at least 1 minute after the producers stop sending messages. This period is required for the queue metadata to reach eventual consistency.

elbik
  • 1,749
  • 2
  • 16
  • 21
13

You can retrieve Attributes of the Queue and look for the relevant properties (See this link). You might want to look at both the following attributes.

ApproximateNumberOfMessages - Returns the approximate number of visible messages in a queue

ApproximateNumberOfMessagesNotVisible - Returns the approximate number of messages that have not timed-out and aren't deleted.

If you want to include the messages that are waiting to be added, you can consider the following property as well.

ApproximateNumberOfMessagesDelayed - Returns the approximate number of messages that are waiting to be added to the queue.

Finally do a summation of the values returned by the above properties and get the size of the current queue.

Community
  • 1
  • 1
Keet Sugathadasa
  • 11,595
  • 6
  • 65
  • 80
  • Are these values updated relatively quickly or in real-time? Any idea on the delay for the values to be updated after an enqueue/dequeue? – Kubie Oct 18 '21 at 13:27
1

For PHP, try this,

        $sqsClient = new  SqsClient([
            'region' => env('AWS_REGION'),
            'version' => '2012-11-05',
            'credentials' => [
                'key'    => env('AWS_ACCESS_KEY'),
                'secret' => env('AWS_SECRET_KEY'),
            ],
        ]);

        $sqs = new SqsQueue($sqsClient,null,null);
        $size = $sqs->size(env('AWS_SQS_URL'));

        echo $size;
Thushara Buddhika
  • 1,652
  • 12
  • 14
0

With PHP :

putenv('AWS_ACCESS_TOKEN=xxxx');
putenv('AWS_ACCESS_TOKEN_SECRET=xxxx'); 

$sqs = new \Aws\Sqs\SqsClient([
    'profile' => 'default',
    'region' => 'REGION',
    'version' => 'latest'
]);

$queueUrl = 'https://sqs.REGION.amazonaws.com/xxxxxxxx/queue-name';

$x = $sqs->getQueueAttributes([
    'QueueUrl' => $queueUrl, 
    'AttributeNames' => ['All']
]);

echo json_encode($x->toArray(), JSON_PRETTY_PRINT);

will output something a bit like :

{
    "Attributes": {
        "QueueArn": "arn:aws:sqs:REGION:xxxxxxx:queue-name",
        "ApproximateNumberOfMessages": "0",
        "ApproximateNumberOfMessagesNotVisible": "0",
        "ApproximateNumberOfMessagesDelayed": "0",
        "CreatedTimestamp": "1587472818",
        "LastModifiedTimestamp": "1587473783",
        "VisibilityTimeout": "30",
        "MaximumMessageSize": "262144",
        "MessageRetentionPeriod": "345600",
        "DelaySeconds": "0",
        "ReceiveMessageWaitTimeSeconds": "0",
        "KmsMasterKeyId": "alias\/aws\/sqs",
        "KmsDataKeyReusePeriodSeconds": "300",
        "FifoQueue": "true",
        "ContentBasedDeduplication": "false"
    },
    "@metadata": {
       ....
    }
}
David Goodwin
  • 4,184
  • 1
  • 22
  • 12
0

You can use JQ to get the output only in value:

$AWSProdAccountID = "1234567890"

$ProductionErrorMsgCount = aws sqs get-queue-attributes --queue-url https://sqs.<region>.amazonaws.com/$AWSProdAccountID/<SQS name> --attribute-names ApproximateNumberOfMessages --profile $AWSProfile --region ap-southeast-2 | jq.exe -r ".Attributes.ApproximateNumberOfMessages[:1]"
0

Below code uses AWS PHP SDK V3 to get the Queue URL first and then get the ApproximateNumberOfMessages, ApproximateNumberOfMessagesDelayed, ApproximateNumberOfMessagesNotVisible attributes for the requested Queue:

ApproximateNumberOfMessages – Returns the approximate number of messages available for retrieval from the queue.

ApproximateNumberOfMessagesDelayed – Returns the approximate number of messages in the queue that are delayed and not available for reading immediately. This can happen when the queue is configured as a delay queue or when a message has been sent with a delay parameter.

ApproximateNumberOfMessagesNotVisible – Returns the approximate number of messages that are in flight. Messages are considered to be in flight if they have been sent to a client but have not yet been deleted or have not yet reached the end of their visibility window.

require 'vendor/autoload.php';

use Aws\Sqs\SqsClient; 
use Aws\Exception\AwsException;


$queueName = "SQS_QUEUE_NAME";
 
$client = new SqsClient([
    'profile' => 'default',
    'region' => 'us-west-2',
    'version' => '2012-11-05'
]);

try {
    $result = $client->getQueueUrl([
        'QueueName' => $queueName
    ]);
    
    $queueUrl = $result['QueueUrl'];
    $result = $client->getQueueAttributes([
        'AttributeNames' => ['ApproximateNumberOfMessages', 'ApproximateNumberOfMessagesDelayed', 'ApproximateNumberOfMessagesNotVisible'],
        'QueueUrl' => $queueUrl,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
Arpit Jain
  • 1,599
  • 9
  • 23