0

Been debugging all night, but I can't figure out why my http request is not being called

return getWordDefinition(queryUrl).then(function(responseMsg) {
    //Perform some other business logic
},
  function(err) {
      console.log('Error Getting Definition: ' + err);
});     

The function I'm trying to call:

function getWordDefinition(queryUrl){
    
    const options = {
    method: "GET"
  };

    return new Promise(function(resolve, reject) {
        const request = https.request(queryUrl, options, function(response) {

        console.log("STATUS CODE: " + response.statusCode);  // <------Not being called

        var data;
        response.on("data", function(chunk) {
          if (!data) {
            data = chunk;
          } else {
            data += chunk;
          }
        });
    
        response.on("end", function() {
          console.log("Data: " + JSON.stringify(data));
          resolve("Finished getting data");
        });
        
        request.on('error', (e) => {
          reject("ERROR ON REQUEST: " + e.message);
        });
        
        request.end();
        
      });
    });
  
}

This code is inside my AWS lambda function which seems to be timing out. I'm logging the status code for the http request, but it's never being called in my function. What am I doing incorrectly?

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83

1 Answers1

1

Your Lambda is making an HTTP request and it appears to be timing out?

Here are 3 possible reasons, in descending order of likelihood:

  1. it's running in VPC and you've given the Lambda no route to the internet
  2. the HTTP request takes longer than the Lambda timeout (default is 3 seconds)
  3. your async/promise code has a bug

See Internet and service access for VPC-connected functions for #1.

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • It says "No VPC configuration" under my lambda function, also I've tried changing the time out to 1 min, but it's still timing out. The request to the endpoint takes 1 second in the address bar – DIRTY DAVE Nov 16 '20 at 13:27
  • I've also tried attaching ```AWSLambdaVPCAccessExecutionRole``` instead of ```AWSLambdaBasicExecutionRole``` in my policies for that lambda function – DIRTY DAVE Nov 16 '20 at 13:32
  • Run the same HTTPS request code on your laptop. Does it work? – jarmod Nov 16 '20 at 13:33
  • no, I don't think it's a lambda issue. The ```return new Promise(function . . .``` status code log is still not called. So I think I'm making the request incorrectly – DIRTY DAVE Nov 16 '20 at 13:43
  • See [wrapping https.request in promise](https://stackoverflow.com/questions/38533580/nodejs-how-to-promisify-http-request-reject-got-called-two-times) or switch to something promise-native like Axios (also has nicer interface). – jarmod Nov 16 '20 at 13:47
  • 1
    Thanks for helping me debug the issue and also recommending axios...so much better than using https.request, Once I switched I got it done in 3 lines with no issues. – DIRTY DAVE Nov 16 '20 at 14:16