简体   繁体   中英

How to get AWS SQS queue ARN in nodeJS?

I'm trying to build an application with a basic client-server infrastructure. The server infrastructure is hosted on AWS, and when a client logs on, it sends a message to the server to set up various infrastructure considerations. One of the pieces of infrastructure is an SQS Queue that the client can poll from to get updates from the server (eventually I'd like to build a push service but I don't know how for right now).

I'm building this application in NodeJS using the Node AWS SDK. The problem I'm having is I need the queue ARN to do various things like subscribe the SQS queue to an SNS topic that the application uses, but the create queue API returns the queue URL, not ARN. So I can get the ARN from the URL using the getQueueAttributes API, but it doesn't seem to be working. Whenever I call it, I get undefined as the response. Here's my code, please tell me what I'm doing wrong:

exports.handler = (event, context, callback) => {

    new aws.SQS({apiVersion: '2012-11-05'}).createQueue({
            QueueName: event.userId
        }).promise()
    )
    .then(data => { /* This has the Queue URL */
        new aws.SQS({apiVersion: '2012-11-05'}).getQueueAttributes({
            QueueUrl: data.QueueUrl,
            AttributeNames: ['QueueArn']
        }).promise()
    })
    .then(data => {
        console.log(JSON.stringify(data)); /* prints "undefined" */
    })
/* Some more code down here that's irrelevant */
}

Thanks!

const AWS = require('aws-sdk');
const sqs = new AWS.SQS();

exports.handler = async(event, context, callback) => {

    var params = {
        QueueUrl: 'my-queue-url',
        AttributeNames: ['QueueArn']
    };

    let fo = await sqs.getQueueAttributes(params).promise();
    console.log(fo);
};

and it printed

{
    ResponseMetadata: { RequestId: '123456-1234-1234-1234-12345' },
    Attributes: {
        QueueArn: 'arn:aws:sqs:eu-west-1:12345:my-queue-name'
    }
}

With the help of Ersoy, I realized that I was using block-formatting (with {}) to write my Promises, but I was never returning anything from those blocks. I had thought that the last value in the Promise block was the return value by default, but it seems that was not the case. When I added return before the SQS API command, then it worked (without using async/await).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM