2
votes

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!

2
did you try All instead of QueueArn, what does it return ?Ersoy
I tried 'All', ['All'], ['QueueArn'], and 'QueueArn'. All of those return undefined.Ertai87
did you try to call getQueueAttributes on an existing queue- i mean if aws is creating the queue async then you may not get it back even the createQueue is completed.Ersoy
Yup, I tried creating the queue and then commenting out the create queue part and hardocding in the queue URL from the AWS console. That didn't work either. By the way, because it's probably going to be your next question, I did try using AWS CLI from console and it worked perfectly.Ertai87
yes :) - what about the execution role of this lambda - does this lambda's role allowed to access SQS ?Ersoy

2 Answers

5
votes
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'
    }
}
1
votes

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).