alexst
alexst

Reputation: 11

Node.js aws Lambda SQS functions are not firing

I have a lambda function that tries to call SQS functions from the aws-sdk but they don't fire. For my function, I have two permission policies AmazonSQSFullAccess and AWSLambdaFullAccess. The function is able to be invoked by a SQS message but when I call sqs.listQueues, sqs.createQueue, or sqs.sendMessage, it doesn't do anything.

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

exports.handler = async (event) => {
  console.log(event);
  
  sqs.listQueues({}, function(err, data) {
    if (err) {
      console.log("Error", err);
    } else {
      console.log("Success", data.QueueUrls);
    }
  });
  
  var params = {
    QueueName: 'Test Queue',
    Attributes: {
      'DelaySeconds': '60',
      'MessageRetentionPeriod': '86400'
    }
  };

  sqs.createQueue(params, function(err, data) {
    if (err) {
      console.log("Error", err);
    } else {
      console.log("Success", data.QueueUrl);
    }
  });
};

Here is the log output:

START RequestId: 0e345c02-b5f2-4816-8e58-c9a8cfbc4069 Version: $LATEST<br/>
2020-06-24T21:26:40.697Z    0e345c02-b5f2-4816-8e58-c9a8cfbc4069    INFO    { key1: 'value1', key2: 'value2', key3: 'value3' }<br/>
END RequestId: 0e345c02-b5f2-4816-8e58-c9a8cfbc4069<br/>
REPORT RequestId: 0e345c02-b5f2-4816-8e58-c9a8cfbc4069  Duration: 521.92 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 83 MB  Init Duration: 370.65 ms

I have two queues created already that are not logged and the new 'Test Queue' does not appear. Is this a permissions problem or a configuration problem or something else entirely?

Upvotes: 1

Views: 623

Answers (1)

Ersoy
Ersoy

Reputation: 9576

The following code works (tested for both listing and creating). Checked the created one on SQS page too. I used await with promise.

Also careful about queue names, check the restrictions

A queue name can have up to 80 characters. The following characters are accepted: alphanumeric characters, hyphens (-), and underscores (_).

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

exports.handler = async (event) => {
  console.log(event);
  
  let queues = await sqs.listQueues().promise();
  
  // return queues
  
  var params = {
    QueueName: 'TestQueue', // name doesnt contain space
    Attributes: {
      'DelaySeconds': '60',
      'MessageRetentionPeriod': '86400'
    }
  };

  await sqs.createQueue(params).promise();
};

Upvotes: 1

Related Questions