
This assumes you are familiar with the basic configurations needed to connect to Amazon Web Services (AWS) using NodeJS. If not, check out my article Getting Started with NodeJS aws-sdk.
The @aws-sdk/client-sqs package is used to interact with AWS Simple Queue Service (SQS) in NodeJS. The npm list command can be used to determine if you have the @aws-sdk/client-sqs package installed. If not, the npm install command can be used to install the package. Once installed, your package.json should include the @aws-sdk/client-sqs package.
{
"dependencies": {
"@aws-sdk/client-sqs": "^3.693.0"
}
}
This assumes you are already familiar with how to list your AWS Simple Queue Service (SQS) Queues using CommonJS or ES Module.
import { SQSClient, ListQueuesCommand, GetQueueAttributesCommand } from "@aws-sdk/client-sqs";
var params
var command
var response
const client = new SQSClient({ region: "us-east-1" })
params = {
QueueNamePrefix: "my-first-queue.fifo"
}
command = new ListQueuesCommand(params)
response = await client.send(command)
params = { QueueUrl: response.QueueUrls.toString(), AttributeNames: [ "All" ] }
command = new GetQueueAttributesCommand(params)
response = await client.send(command)
console.log(`JSON.stringify(response) = ${JSON.stringify(response)}`)
Something like this should be returned.
{
'$metadata': {
httpStatusCode: 200,
requestId: '95209905-0c66-5f21-a27e-c6beb053c962',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
Attributes: {
QueueArn: 'arn:aws:sqs:us-east-1:123456789012:my-first-queue.fifo',
ApproximateNumberOfMessages: '1306',
ApproximateNumberOfMessagesNotVisible: '0',
ApproximateNumberOfMessagesDelayed: '0',
CreatedTimestamp: '1731902618',
LastModifiedTimestamp: '1731906101',
VisibilityTimeout: '30',
MaximumMessageSize: '262144',
MessageRetentionPeriod: '1209600',
DelaySeconds: '0',
Policy: '{"Version":"2012-10-17","Id":"__default_policy_ID","Statement":[{"Sid":"__owner_statement","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::123456789012:user/john_doe"},"Action":"SQS:*","Resource":"arn:aws:sqs:us-east-1:123456789012:my-first-queue.fifo"}]}',
ReceiveMessageWaitTimeSeconds: '1',
SqsManagedSseEnabled: 'true',
FifoQueue: 'true',
DeduplicationScope: 'queue',
FifoThroughputLimit: 'perQueue',
ContentBasedDeduplication: 'false'
}
}
You probably want to just get the ApproximateNumberOfMessages values.
import { SQSClient, ListQueuesCommand, GetQueueAttributesCommand } from "@aws-sdk/client-sqs";
var params
var command
var response
const client = new SQSClient({ region: "us-east-1" })
params = {
QueueNamePrefix: "my-first-queue.fifo"
}
command = new ListQueuesCommand(params)
response = await client.send(command)
params = { QueueUrl: response.QueueUrls.toString(), AttributeNames: [ "ApproximateNumberOfMessages" ] }
command = new GetQueueAttributesCommand(params)
response = await client.send(command)
console.log(`response.Attributes.ApproximateNumberOfMessages = ${response.Attributes.ApproximateNumberOfMessages}`)
And here is an example using an async function.
- Notice in this example that line 1 has "import" which means this is an ES Module, not CommonJS.
In this example, all attributes of my-first-queue.fifo will be fetched.
import { SQSClient, ListQueuesCommand, GetQueueAttributesCommand } from "@aws-sdk/client-sqs";
const client = new SQSClient({ region: "us-east-1" });
var messages_count = 0
async function getQueueURL() {
const params = {
QueueNamePrefix: "my-first-queue.fifo"
};
const command = new ListQueuesCommand(params);
return await client.send(command);
}
async function GetQueueAttributes(queue_url) {
const params = {
QueueUrl: queue_url,
AttributeNames: [ "All" ]
}
const command = new GetQueueAttributesCommand(params);
return await client.send(command);
}
getQueueURL()
.then(
response => {
if (response.QueueUrls.length != 1) {
console.error(`response.QueueUrls.length does NOT equal 1`);
process.exit(1)
} else {
GetQueueAttributes(response.QueueUrls.toString())
.then(
response => {
console.log(response)
}
)
.catch(
err => {
console.error(err)
process.exit(1)
}
)
}
}
)
.catch(
err => {
console.error(err)
process.exit(1)
}
)
Something like this should be returned. Notice in this example that ApproximateNumberOfMessages is 1306.
{
'$metadata': {
httpStatusCode: 200,
requestId: '95209905-0c66-5f21-a27e-c6beb053c962',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
Attributes: {
QueueArn: 'arn:aws:sqs:us-east-1:123456789012:my-first-queue.fifo',
ApproximateNumberOfMessages: '1306',
ApproximateNumberOfMessagesNotVisible: '0',
ApproximateNumberOfMessagesDelayed: '0',
CreatedTimestamp: '1731902618',
LastModifiedTimestamp: '1731906101',
VisibilityTimeout: '30',
MaximumMessageSize: '262144',
MessageRetentionPeriod: '1209600',
DelaySeconds: '0',
Policy: '{"Version":"2012-10-17","Id":"__default_policy_ID","Statement":[{"Sid":"__owner_statement","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::123456789012:user/john_doe"},"Action":"SQS:*","Resource":"arn:aws:sqs:us-east-1:123456789012:my-first-queue.fifo"}]}',
ReceiveMessageWaitTimeSeconds: '1',
SqsManagedSseEnabled: 'true',
FifoQueue: 'true',
DeduplicationScope: 'queue',
FifoThroughputLimit: 'perQueue',
ContentBasedDeduplication: 'false'
}
}
Instead of fetched all attributes, only ApproximateNumberOfMessages can be fetched.
import { SQSClient, ListQueuesCommand, GetQueueAttributesCommand } from "@aws-sdk/client-sqs";
const client = new SQSClient({ region: "us-east-1" });
var messages_count = 0
async function getQueueURL() {
const params = {
QueueNamePrefix: "my-first-queue.fifo"
};
const command = new ListQueuesCommand(params);
return await client.send(command);
}
async function GetQueueAttributes(queue_url) {
const params = {
QueueUrl: queue_url,
AttributeNames: [ "All" ]
}
const command = new GetQueueAttributesCommand(params);
return await client.send(command);
}
getQueueURL()
.then(
response => {
if (response.QueueUrls.length != 1) {
console.error(`response.QueueUrls.length does NOT equal 1`);
process.exit(1)
} else {
GetQueueAttributes(response.QueueUrls.toString())
.then(
response => {
console.log(response)
}
)
.catch(
err => {
console.error(err)
process.exit(1)
}
)
}
}
)
.catch(
err => {
console.error(err)
process.exit(1)
}
)
Something like this should be returned.
{
'$metadata': {
httpStatusCode: 200,
requestId: '6d6e240c-18ed-5131-9530-7bb7a54263a2',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
Attributes: { ApproximateNumberOfMessages: '1306' }
}
You probably want to just get the ApproximateNumberOfMessages values.
import { SQSClient, ListQueuesCommand, GetQueueAttributesCommand } from "@aws-sdk/client-sqs";
const client = new SQSClient({ region: "us-east-1" });
var messages_count = 0
async function getQueueURL() {
const params = {
QueueNamePrefix: "my-first-queue.fifo"
};
const command = new ListQueuesCommand(params);
return await client.send(command);
}
async function GetQueueAttributes(queue_url) {
const params = {
QueueUrl: queue_url,
AttributeNames: [ "All" ]
}
const command = new GetQueueAttributesCommand(params);
return await client.send(command);
}
getQueueURL()
.then(
response => {
if (response.QueueUrls.length != 1) {
console.error(`response.QueueUrls.length does NOT equal 1`);
process.exit(1)
} else {
GetQueueAttributes(response.QueueUrls.toString())
.then(
response => {
var ApproximateNumberOfMessages = response.Attributes.ApproximateNumberOfMessages
console.log(`There are approximately ${ApproximateNumberOfMessages} messages in ${queue_url}`)
}
)
.catch(
err => {
console.error(err)
process.exit(1)
}
)
}
}
)
.catch(
err => {
console.error(err)
process.exit(1)
}
)
Which should return something like this.
There are approximately 1307 messages in https://sqs.us-east-1.amazonaws.com/123456789012/my-first-queue.fifo
Did you find this article helpful?
If so, consider buying me a coffee over at