最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AWS Lambda: Error: getaddrinfo ENOTFOUND from API - Stack Overflow

programmeradmin0浏览0评论

I have created the following code which grabs a seralizedXmlFile object from an S3 bucket and pushes it to an API service. This returns FAIL with the logs showing

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;

I followed a Q and A I found on "Error: getaddrinfo ENOTFOUND" error when making an HTTPs request

and changed the code to

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

but again firing the same error....does anyone have any idea what the issue is?

I have also tested the web service api through POSTMAN, so I can confirm it is working

Thank You

I have created the following code which grabs a seralizedXmlFile object from an S3 bucket and pushes it to an API service. This returns FAIL with the logs showing

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;

I followed a Q and A I found on "Error: getaddrinfo ENOTFOUND" error when making an HTTPs request

and changed the code to

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

but again firing the same error....does anyone have any idea what the issue is?

I have also tested the web service api through POSTMAN, so I can confirm it is working

Thank You

Share Improve this question edited Nov 12, 2024 at 9:30 Yves M. 31k24 gold badges109 silver badges149 bronze badges asked Feb 15, 2018 at 3:56 JohnJohn 3,94522 gold badges84 silver badges173 bronze badges 3
  • 1 Is this external site on the internet or in some VPC? Because your lambda needs to have access to the same. Please see aws.amazon.com/premiumsupport/knowledge-center/… – Tarun Lalwani Commented Mar 22, 2018 at 4:10
  • Its a published API solution on IIS, I will try what you have suggested – John Commented Mar 22, 2018 at 5:17
  • Where do you change the placeholder string http://URL to something real? The URL part is clearly a placeholder, not a valid host name. – Chris Johnson Commented Mar 28, 2018 at 18:50
Add a comment  | 

2 Answers 2

Reset to default 11

Try

host: "URL",

instead of

host: "http://URL",

Also, you're using the https library and your URL prefix is http:// but I think you can/should omit it altogether.

Error: getaddrinfo ENOTFOUND http://url

means client was not able to connect to given address. Please try specifying host without http:

var params = {
    host: "URL",
    path: "/api/SyncPersonnelViaAwsApi/Get/5"
};
发布评论

评论列表(0)

  1. 暂无评论