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.
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.
I believe what you are looking for is get-queue-attributes, perhaps interrogating the ApproximateNumberOfMessages attribute.
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;
Use this command to get the desired result
aws sqs get-queue-attributes --queue-url <queue url> --region <region> --attribute-names ApproximateNumberOfMessages
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]"
© 2022 - 2024 — McMap. All rights reserved.