netlify / netlify-lambda

Helps building and serving lambda functions locally and in CI environments
MIT License
594 stars 117 forks source link

Time out issue #87

Closed axhamre closed 5 years ago

axhamre commented 5 years ago

I don't run into any issues with my built function locally. It runs immediately.

But when deployed on Netlify, I'm getting a time out error:

{"errorMessage":"2018-12-17T02:49:11.602Z 4f290cff-01a6-11e9-934d-9f4eca498414 Task timed out after 10.01 seconds"}

Any clues what could be wrong? Or else - how do I debug this?

Information:

  1. Nothing in the deployment logs indicates that something's wrong in the build.

  2. Here is my netlify.toml:

    [build]
    command = "yarn lambda:build"
    functions = "functions"
  3. Here is my package.json:


{
  "name": "netlify-backend-starter-kit",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "license": "UNLICENSED",
  "scripts": {
    "lambda:build": "netlify-lambda build src/lambda",
    "lambda:serve": "netlify-lambda serve src/lambda"
  },
  "dependencies": {
    "graphql-shield": "^4.1.2",
    "graphql-yoga": "^1.16.7"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.0",
    "@babel/core": "^7.2.0",
    "@babel/node": "^7.2.0",
    "@babel/plugin-transform-runtime": "^7.2.0",
    "@babel/preset-env": "^7.2.0",
    "netlify-lambda": "^1.1.1",
  }
}
swyxio commented 5 years ago

to be clear are you getting timeout on function call or during the build? ive never seen this kind of timeout but im new here

also check https://github.com/stubailo/apollo-netlify-lambda-app for working example

axhamre commented 5 years ago

On function call. Build doesn't report any errors.

Thanks for the link, I've gotten other similar lambda projects working on Netlify though.

swyxio commented 5 years ago

its likely something wrong in the function code then. are you calling the callback? or returning a promise? sorry i know it sounds dumb but pple miss this stuff sometimes

swyxio commented 5 years ago

to be clear tho, i do regard this difference between local emulation and real deploy a bug. if we’re letting you run something we shouldn’t, we should try to fix that. however i need to know how to repro

axhamre commented 5 years ago

Sorry for not having provided more info on this, I'm unfortunately way too busy at the moment. Closing this for now.

swyxio commented 5 years ago

still appreciate the report. we shouldnt let you locally do stuff you cant do in the prod enviroment. at the same time i wish our functions lived longer than 5 mins. good luck

naeem-gitonga commented 5 years ago

@sw-yx can we reopen this issue? My function works perfectly locally but times out in prod. How do we debug these issues.

1.Here's my function.

require('dotenv').config();
const mongoose = require('mongoose');
const DEV__DB__URL = process.env.DEV__DB__URL;
const Schema = mongoose.Schema;

const storySchema = new Schema({
    "name": { type: String, required:true },
    "content": { type: String, required:true },
    "author": { type: String, required:true },
    "likes" :{ type: Number, default: 0 },
});

const Story = mongoose.model('Story', storySchema, 'stories');

exports.handler = function (event, context, callback) {
  const method = event.httpMethod;

  mongoose.connect(DEV__DB__URL, {useNewUrlParser: true});
  switch(method) {
    case 'GET':
      return Story.find({}).then(stories => {
        return callback(null, {
          statusCode: 200,
          body: JSON.stringify({ msg: 'Peace, World!', event, context, stories })
        });
      });
    case 'POST':
       console.log(context)
       break;
     default:
        return callback(null,{
          statusCode: 200,
          body: JSON.stringify('nothing')
      });
  }
  mongoose.disconnect();
};

Everything else is identical to what is in create-react-app-lambda repo, with the exception of different file names.

I cannot think of any other information that would be pertinent. Any help you could provide would be great! Thanks beforehand.

swyxio commented 5 years ago

just to be sure - DEVDBURL is a remote mongo server right? cos you're not running a mongodb server in our functions.

what you can do is show your deploy and/or function logs. if your site is open source, you can link it like this: https://app.netlify.com/sites/jamstack-hackathon-starter/deploys and you can look specficlly at your function logs https://app.netlify.com/sites/jamstack-hackathon-starter/functions/hello

if you need further support on Netlify Functions, i recommend contacting https://netlify.com/support

naeem-gitonga commented 5 years ago

@sw-yx Thank you for the quick response! Yes DEV__DB__URL is the url to a database instance hosted by mLab. I get the following Message client side:

errorMessage: "2019-01-26T19:02:12.247Z 430d0f60-1678-4113-b4d1-bf645c1bc5b4 Task timed out after 10.01 seconds"

Here is a link to latest deploy logs click here.

The function logs only have the following:

10:34:08 AM: lambda invoked
10:34:18 AM: Task timed out after 10.01 seconds
10:36:35 AM: lambda invoked
10:36:45 AM: Task timed out after 10.01 seconds
swyxio commented 5 years ago

ok it looks like you're doing things right so far. please contact Netlify Support to figure out why your function is timing out. e.g. make sure basic stuff like auth isnt blocking you and env vars are set up correctly for Netlify Functions. comment out code, console.log and figure which exact line of code is causing you to time out.

naeem-gitonga commented 5 years ago

@sw-yx Thanks for your help!

JiiB commented 5 years ago

@JNaeemGitonga Whats the status for your issue? I'm gettings the same error message, ist works fine locally, but I get the Timeout message when I deploy to netlify. Was the support able to help you out?

Thanks in advance

naeem-gitonga commented 5 years ago

@JiiB No, unfortunately there wasn't much in the forum that helped me. What did help though was for me to deploy the function to AWS so that I could get better logging and see the results of my changes quicker. For example, just yesterday I was at a hackathon and my team decided to go with my suggestion of using create-react-app-lambda to bootstrap our project. Well, the same thing happened again after modifying the function with our own code; it worked locally but not in prod. I took the function and deployed it using my personal AWS account and was able to get it running there and then updated my code in our netlify + lambda app.

My error was that something in my function wasn't resolving or closing causing the function to timeout. Check around the callback() method and make sure that you are doing that correctly. Our function used a switch...case to handle our various CRUD operations and I suspect that the problem was somewhere between the switch...case and callback()--still not exactly sure what I did to make it work. Here is a link to our hackathon project check it out if you think it could be of some use.

Hope this helps!

JiiB commented 5 years ago

@JNaeemGitonga Thanks for your info. I'll try to deploy my function to AWS directly.

I use my function to send an email with an attachment. I used my Gmail Account with OAuth2 and everything worked fine (also when I deployed to Netlify). Today I switched to an SMTP email account instead, It works locally, I'm getting the correct status codes that I've definded. When I deploy to Netlify, the email will still be sent correctly, but i don't receive my configured status codes, instead I get the timeout message.

Hopefully it will work with AWS

swyxio commented 5 years ago

if y'all need help in future always keep in mind our support team at https://www.netlify.com/support/

also we have a related PR for this that will help replicate the timeout locally https://github.com/netlify/netlify-lambda/pull/116

Collins1738 commented 4 years ago

@JNaeemGitonga Thanks for your info. I'll try to deploy my function to AWS directly.

I use my function to send an email with an attachment. I used my Gmail Account with OAuth2 and everything worked fine (also when I deployed to Netlify). Today I switched to an SMTP email account instead, It works locally, I'm getting the correct status codes that I've definded. When I deploy to Netlify, the email will still be sent correctly, but i don't receive my configured status codes, instead I get the timeout message.

Hopefully it will work with AWS

Hey, I was having the same issue and I was wondering, for the SMTP you switched to, where you putting in the password straight in ‘auth.pass’ or did you do some OAuth2 like you did for google. I think that might be the problem. I tried for google and it worked locally but didn’t in netlify, and the same thing happened with the new SMTP I tried, and I never used OAuth2 for neither