Get number of messages in an Amazon SQS Queue
Asked Answered
R

7

38

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.

Rant answered 17/8, 2014 at 16:22 Comment(1)
For Java Loot At: docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/…Skricki
E
22

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.

Einberger answered 24/9, 2020 at 11:35 Comment(0)
S
18

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

Saddlebacked answered 17/8, 2014 at 21:9 Comment(3)
Also available in the AWS SDK for PHP: docs.aws.amazon.com/aws-sdk-php/latest/…Unstopped
"ApproximateNumberOfMessages" is a good way. But this might be inaccurate if multiple consumers are consuming from the queue.Ellisellison
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?Taima
E
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.

Ellisellison answered 19/4, 2018 at 4:19 Comment(1)
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?Taima
B
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;
Betrothed answered 1/2, 2021 at 7:1 Comment(0)
A
1

Use this command to get the desired result aws sqs get-queue-attributes --queue-url <queue url> --region <region> --attribute-names ApproximateNumberOfMessages

Acidulate answered 8/4 at 11:6 Comment(0)
M
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": {
       ....
    }
}
Marisolmarissa answered 21/4, 2020 at 14:51 Comment(0)
M
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]"
Mortgage answered 26/10, 2022 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.