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.
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.
I believe what you are looking for is get-queue-attributes, perhaps interrogating the ApproximateNumberOfMessages attribute.
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.
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.
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;
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": {
....
}
}
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]"
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());
}