Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda: Error: getaddrinfo ENOTFOUND from api

I have created the following code which grabs a seralizedXmlFile object from an S3 bucket and pushes it to a 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 AWS Lambda: Error: getaddrinfo ENOTFOUND

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

like image 822
John Avatar asked Feb 15 '18 03:02

John


1 Answers

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.

like image 120
TLP Avatar answered Sep 28 '22 12:09

TLP