
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"
}
}
Here is the minimal boilerplate code without any error handling to list your AWS Simple Queue Service (SQS) Queues using CommonJS.
import { SQSClient, ListQueuesCommand } from "@aws-sdk/client-sqs";
const client = new SQSClient({ region: "us-east-1" });
const params = {}
const command = new ListQueuesCommand(params);
const response = await client.send(command);
console.log(`response = ${JSON.stringify(response)}`)
If your app is an ES module, package.json will have "type": "module".
{
"type":"module",
"dependencies": {
"@aws-sdk/client-sqs": "^3.693.0"
}
}
In this scenario, you will use import instead of require.
import { SQSClient, ListQueuesCommand } from "@aws-sdk/client-sqs";
Something like this should be returned.
{"$metadata":{"httpStatusCode":200,"requestId":"ad6aad8a-eee5-5e93-a813-f24e620a718e","attempts":1,"totalRetryDelay":0},"QueueUrls":["https://sqs.us-east-1.amazonaws.com/123456789012/my-first-queue.fifo","https://sqs.us-east-1.amazonaws.com/123456789012/my-second-queue.fifo"]}
Here is an example using an async function.
const { SQSClient, ListQueuesCommand } = require("@aws-sdk/client-sqs");
const client = new SQSClient({ region: "us-east-1" });
async function getQueueURLs() {
const params = {
};
const command = new ListQueuesCommand(params);
return await client.send(command);
}
getQueueURLs()
.then(
response => {
//response is really only needed for testing/debugging purposes, to see the full response
//console.log(response)
var queue_urls = response.QueueUrls
console.log(`queue URLs => ${queue_urls}`);
}
)
.catch(
err => {
console.error(err)
process.exit(1)
}
)
Something like this should be returned.
{
'$metadata': {
httpStatusCode: 200,
requestId: 'a5a5e228-3ea8-586f-b9f8-bd9d25d1aad2',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
QueueUrls: [
'https://sqs.us-east-1.amazonaws.com/123456789012/my-first-queue.fifo',
'https://sqs.us-east-1.amazonaws.com/123456789012/my-second-queue.fifo'
]
}
queue URLs => [ 'https://sqs.us-east-1.amazonaws.com/123456789012/my-first-queue.fifo', 'https://sqs.us-east-1.amazonaws.com/123456789012/my-second-queue.fifo' ]
QueueNamePrefix can be used if you only want to return queues matching a certain queue name that begins with
import { SQSClient, ListQueuesCommand } from "@aws-sdk/client-sqs";
const client = new SQSClient({ region: "us-east-1" });
async function getQueueURLs() {
const params = {
QueueNamePrefix: "my-first-queue.fifo"
};
const command = new ListQueuesCommand(params);
return await client.send(command);
}
getQueueURLs()
.then(
response => {
//response is really only needed for testing/debugging purposes, to see the full response
//console.log(response)
var queue_urls = response.QueueUrls
console.log(`queue URLs => ${queue_urls}`);
}
)
.catch(
err => {
console.error(err)
process.exit(1)
}
)
If you want one and only one queue URL to be returned, you could determine if the length of the QueueUrls response is 1 (meaning there is one element in the list) and then create a variable named queue_url that contains the Queue URL.
import { SQSClient, ListQueuesCommand, ReceiveMessageCommand } from "@aws-sdk/client-sqs";
const client = new SQSClient({ region: "us-east-1" });
async function getQueueURL() {
const params = {
QueueNamePrefix: "my-first-queue.fifo"
};
const command = new ListQueuesCommand(params);
return await client.send(command);
}
getQueueURL()
.then(
response => {
//response is really only needed for testing/debugging purposes, to see the full response
//console.log(response)
if (response.QueueUrls.length != 1) {
console.error(`response.QueueUrls.length does NOT equal 1`);
process.exit(1)
} else {
var queue_url = response.QueueUrls.toString()
console.log(`queue URL => ${queue_url}`);
}
}
)
.catch(
err => {
console.error(err)
process.exit(1)
}
)
Something like this should be returned.
]$ node app.js
queue URL => 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