Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda function timing out

In my local mocha tests the following handler function works just fine. However, when I upload to AWS (using Serverless framework) it times out (unless you don't provide a uid parameter where it then correctly responds immediately).

What's particularly odd is that in less than 3 seconds (timeout is set at 5 seconds), the job completes and even the "post-facto" log message is output but it somehow calling the callback and that is not completing the Lambda function

Here's the cloudwatch log:

![enter image description here]1

And here's the handler function:

export const handler = (event: IRequestInput, context: IContext, cb: IGatewayCallback) => {
  console.log('EVENT:\n', JSON.stringify(event, null, 2));
  const uid = _.get(event, 'queryStringParameters.uid', undefined);
  if(!uid) {
    cb(null, {
      statusCode: 412,
      body: 'no User ID was provided by frontend'
    });
    return;
  }

  oauth.getRequestToken()
    .then(token => {
      console.log('Token is:\n', JSON.stringify(token, null, 2));
      console.log('User ID: ', uid);
      token.uid = uid;
      return Promise.resolve(token);
    })
    .then((token) => {
      console.log('URL: ', token.url);
      cb(null, {
        statusCode: 200,
        body: token.url
      });
      console.log('post-facto');
    })
    .catch((err: PromiseError) => {
      console.log('Problem in getting promise token: ', err);
      cb(err.message);
    });

};
like image 535
ken Avatar asked Dec 06 '25 19:12

ken


1 Answers

Add the following as the first line of your handler function:

context.callbackWaitsForEmptyEventLoop = false
like image 173
idbehold Avatar answered Dec 08 '25 12:12

idbehold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!