Creating an AWS SQS queue

时间:2016-04-25 09:17:11

标签: node.js amazon-web-services aws-sdk amazon-sqs aws-cli

I am trying to create an SQS queue on AWS from an EC2 instance using node.js. From the same instance, I can access S3 and list the buckets, etc. using node.js. The instance has an IAM role granting full access to S3 and SQS.

Executing the below code however fails and I cannot figure out why. Even more interesting is that from my PC the code actually works.

var AWS = require('aws-sdk');
AWS.config.update({region:'eu-central-1'});
//console.log("awsv "+AWS.VERSION); --> 2.3.5
var sqs = new AWS.SQS();
 var params = {
  QueueName: "MyQueue1"
 };
 sqs.createQueue(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else {
        if(data)
                console.log(data);           // successful response
        else
                console.log("Other unknown error");
   }
 });

Error message:

/home/ubuntu/aws-nodejs-sample/node_modules/aws-sdk/lib/http/node.js:121
      callback();
      ^
TypeError: undefined is not a function
    at Writable.writer._write (/home/ubuntu/aws-nodejs-sample/node_modules/aws-sdk/lib/http/node.js:121:7)
    at Writable.write (_stream_writable.js:176:8)
    at write (_stream_readable.js:481:24)
    at Array.forEach (native)
    at flow (_stream_readable.js:493:19)
    at _stream_readable.js:458:7
    at process._tickCallback (node.js:427:13)
    at process._makeCallback (node.js:345:15)

It turns out, that a quick test with a script calling the CLI functions actually works:

var AWS = require('aws-sdk');
AWS.config.region = 'eu-central-1';

var qUrl="https://sqs.eu-central-1.amazonaws.com/......./awsquerytest";

var queue = new AWS.SQS({params: {QueueUrl: qUrl}}); // using url to queue
var receipthandle;
var fs = require('fs');

var child = require('child_process').exec('aws sqs receive-message --queue-url https://sqs.eu-central-1.amazonaws.com/....../awsquerytest');
child.stdout.on('data', function(data) {
   var x= data.toString();
   console.log(data.toString());
   fs.writeFile("/home/ubuntu/aws-nodejs-sample/output",x,function(err){
if(err){
return console.log(err);
}
console.log("oldu");
});
});

child.stderr.on("data", function (data) {
    console.log(data.toString());
});

How can this be explained?

1 个答案:

答案 0 :(得分:0)

看起来它与您在Ubuntu服务器上安装的不同版本的节点与本地计算机有关。

您在服务器上运行的node --version是什么?