简体   繁体   中英

"Error: getaddrinfo ENOTFOUND" error when making an HTTPs request

here's the code in AWS Lambda function:

var https = require('https');
exports.handler = (event, context, callback) => {
    var params = {
        host: "bittrex.com",
        path: "/api/v1.1/public/getmarketsummaries"
    };
    var req = https.request(params, function(res) {
        var test = res.toString();
        console.log(JSON.parse(test));
        //console.log(JSON.parse(res.toString()));
    });
    req.end();
};

Error: getaddrinfo ENOTFOUND https://bittrex.com https://bittrex.com:443 at errnoException (dns.js:28:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

Other solutions did not work.

Remove the https:// from the host. The require already says you're using https/SSL.

I modified your code to work correctly in AWS Lambda Node.js 6.10. I set the Lambda timeout to be 60 seconds for testing.

The big change is adding "res.on('data', function(chunk) {}:" and "res.on('end', function() {}".

var https = require('https');
exports.handler = (event, context, callback) => {
    var params = {
        host: "bittrex.com",
        path: "/api/v1.1/public/getmarketsummaries"
    };
    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));
        });
    });
    req.end();
};

The issue is with your security groups. Looks like your lambda doesn't have access to resolve DNS. Check if you lambda sec groups have port 53 UDP and TCP enabled.

Also Make sure the lambda attempts to reach a reachable host. in other words make sure that the host doesn't reside in a private network / VPN / or an unreachable network (that can't be reached from the externally)

(Trivial but still worth mentioning)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM