简体   繁体   中英

Rest-API call fails in lambda function

I'm new about lambda functions in AWS and I need some suggestions to figure out the nature of the problem. AWS Lambda function based on Javascript using node.js 12.x. I did set up local development environment based on SAM (sam cli/aws cli/docker/IntelliJ) on Ubuntu 18.04 and MacOs Catalina and a simple basic lambda function work, on both systems. I can set up logs and see them on via IntelliJ when docker runs. The function was created using sam init command from a terminal and selecting a simple hello world.

I did add a Rest-API call in it. Nothing fancy, using request 2.88.2 (I know is deprecated and I did try to use other ways, all of them fails anyway so I'm stick with request for now).

Basically what is happening is that the call to the API "seems" not happening at all. Logs placed before and after the API call are showing up. Logs inside the API call, like to show the errors or results, are never showing up. So far only in one case I was able to see an error message coming from the API, when I removed the URI. And as expected the API returned an error message saying: invalid URI.

Otherwise NOTHING. Here some code. This function is called from the lambda handler.

function getToken() {
    const request = require('request');
    const qs = require('querystring');

    console.log("getToken function called");

    let bodyData = qs.stringify({
        username: 'test',
        password: 'xxxxx',
        grant_type: 'password'
    });

    console.log("getToken bodyData : " + bodyData);

    let options = {
        url: "https://blahblahblah/function",
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
        },
        form: bodyData
    };

    console.log("getToken options : " + options);

    var request1 = request(options, function(err, response, body) {
        console.log("returned from API call");

        if (err) {
            console.log("Error from getToken : " + err);
            return null;
        } else {
            console.log("Answer from getToken : " + body);
            return body;
        }
    });
}

I did test the connection and API using Postman and is working. All the logs inside the request are NEVER coming up. No matter changing options (did try many many different ways). What am I doing wrong? Any suggestion on how to track this problem?

Thanks

STeve

This is the correct behaviour. Because the function getToken is not waiting for the http request to complete. you should either convert the function to use promise/async-await or simply callback.

Promise

async function getToken() {
  ...
  return new Promise((resolve, reject) => {
    request(options, function (err, response, body) {
      console.log("returned from API call");

      if (err) {
        console.log("Error from getToken : " + err);
        resolve(null);
      } else {
        console.log("Answer from getToken : " + body);
        resolve(body);
      }
    });
  });
}

// Then in the lambda Handler:
// await getToken()

Callback

function getToken(callback) {
  ...
  
  request(options, function (err, response, body) {
    console.log("returned from API call");

    if (err) {
      console.log("Error from getToken : " + err);
      callback(null);
    } else {
      console.log("Answer from getToken : " + body);
      callback(body);
    }
  });
}

// Then in the lambda
getToken(() => {
  // handle the http request response
})

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