AWS lambda function stops working after timed out error

后端 未结 6 1489
生来不讨喜
生来不讨喜 2021-02-05 00:41

I have a simple lambda function that asynchronously makes an API calls and then returns data. 99% of the time this works great. When ever the API takes longer then the lambda co

6条回答
  •  遥遥无期
    2021-02-05 01:32

    You should look for how your function handle works with a specific context.callbackWaitsForEmptyEventLoop

    If that boolean-type is false, the setTimeout won't be ever fired, because you might've answered/handled the lambda invocation earlier. But if the value of callbackWaitsForEmptyEventLoop is true - then your code will do what you are looking for.

    Also - it's probably easier to handle everything via callbacks directly, without the need for "hand-written" timeouts, changing configuration timeouts and so on...

    E.g.

    function doneFactory(cb) { // closure factory returning a callback function which knows about res (response)
      return function(err, res) {
        if (err) {
          return cb(JSON.stringify(err));
        }
        return cb(null, res);
      };
    }
    
    // you're going to call this Lambda function from your code
    exports.handle = function(event, context, handleCallback) {
    
      // allows for using callbacks as finish/error-handlers
      context.callbackWaitsForEmptyEventLoop = false;
    
      doSomeAsyncWork(event, context, doneFactory(handleCallback));
    };
    

提交回复
热议问题