AWS Lambda:错误:来自api的getaddrinfo ENOTFOUND

时间:2018-02-15 03:56:06

标签: javascript node.js https aws-lambda

我创建了以下代码,它从S3存储桶中获取seralizedXmlFile对象并将其推送到api服务。这将返回FAIL并显示日志

Error: getaddrinfo ENOTFOUND http://url
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

CODE:

const AWS = require('aws-sdk');
const https = require('http');
var s3 = new AWS.S3();
var un;
var pw;
var seralizedXmlFile;

let index = function index(event, context, callback) {
 //for testing I have named the bucket and key   
var params = {
  Bucket: "bucket", //event.bucketName, 
  Key:  "personnelData_50112404_635705766849654385.xml" //event.fileName
};

s3.getObject(params, function(data, err) 
 {
   if (data) 
   {
       let seralizedXmlFile = err.Body.toString('utf-8'); // Use the encoding necessary
       console.log("objectData " + seralizedXmlFile);
   }   
 });

var ssm = new AWS.SSM({region: 'ap-southeast-2'});
var paramsx = {
  'Names' : ['/App/ServiceUsername', '/App/ServicePassword'],
  'WithDecryption' : true
};

ssm.getParameters(paramsx, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     {console.log('data: ' + JSON.stringify(data));           // successful response
  console.log('password: ' + data.Parameters[0].Value); 
  console.log('username: ' + data.Parameters[1].Value); 
  pw = data.Parameters[0].Value;
  un = data.Parameters[1].Value;
  }
   const req = https.request('http:/url/api/SyncPersonnelViaAwsApi/Get/5', (res) => {
                res.headers + 'Authorization: Basic ' + un + ':' + pw;
                let body = seralizedXmlFile;
        console.log('Status:', res.statusCode);
        console.log('Headers:', JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', (chunk) => body += chunk);
        res.on('end', () => {
            console.log('Successfully processed HTTPS response');

           console.log('returned res: ' + res);           
           callback(null, res);                  

        });
    });
    req.end();
});
};
exports.handler = index;

我按照AWS Lambda: Error: getaddrinfo ENOTFOUND

上找到的Q和A.

并将代码更改为

  var params = {
    host: "http://URL",
    path: "/api/SyncPersonnelViaAwsApi/Get/5"
};
var req = https.request(params, function(res) {
    let data = '';
    console.log('STATUS: ' + res.statusCode);
    res.setEncoding('utf8');
    res.on('data', function(chunk) {
        data += chunk;
    });
    res.on('end', function() {
        console.log("DONE");
        console.log(JSON.parse(data));

但再次发出同样的错误....有没有人知道这是什么问题?

我还通过POSTMAN测试了web服务api,所以我可以确认它正在运行

谢谢

2 个答案:

答案 0 :(得分:2)

  

错误:getaddrinfo ENOTFOUND http://url

表示客户端无法连接到指定地址。请尝试指定不带http:

的主机
var params = {
    host: "URL",
    path: "/api/SyncPersonnelViaAwsApi/Get/5"
};

答案 1 :(得分:1)

尝试

host: "URL",

而不是

host: "http://URL",

此外,您使用https库并且您的网址前缀为http://,但我认为您可以/应该完全省略它。