title: Netlify lambda function timing out after 10 seconds
body: <p>I am having trouble returning data from my Netlify serverless function in my production environment. It currently is timing out after 10 seconds, responding with a status code of 502 and the following error message:</p>
  
  <pre class="lang-js prettyprint-override"><code>{
    "errorMessage": "2019-... 5f... Task timed out after 10.01 seconds"
  }
  </code></pre>
  
  <p>However, in my development environment, the Netlify serverless function works perfectly, responding with the data from my database.</p>
  
  <p>I have confirmed that the environment variables on my Netlify site and in my <code>.env</code> file are the same. How would I go about debugging this problem and solving the issue?</p>
  
  <hr>
  
  <blockquote>
    <p><strong>Netlify serverless function</strong></p>
  </blockquote>
  
  <pre class="lang-js prettyprint-override"><code>const { connectDatabase, ProjectSchema } = require('../database')
  
  const createHeaders = origin =&gt; {
      // This is a comma-separated list of allowed origins, including my local 
      // environment and production website
      const allowedOrigins = process.env.ALLOWED_ORIGINS
          ? process.env.ALLOWED_ORIGINS.split(',')
          : []
  
      return {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Headers':
              'Origin, X-Requested-With, Content-Type, Accept',
          'Access-Control-Allow-Origin': allowedOrigins.includes(origin)
              ? origin
              : ''
      }
  }
  
  /**
   * This function queries for all `Project` instances in the database and
   * returns it in the response body.
   */
  const getProjects = async origin =&gt; {
      try {
          const projects = await ProjectSchema.find()
  
          return {
              headers: createHeaders(origin),
              statusCode: 200,
              body: JSON.stringify({ projects })
          }
      } catch (err) {
          return {
              headers: createHeaders(origin),
              statusCode: 400,
              body: JSON.stringify({ error: err })
          }
      }
  }
  
  /**
   * This function is the serverless lambda for `/.netlify/functions/get-projects`.
   */
  exports.handler = async event =&gt; {
      try {
          await connectDatabase()
          const response = await getProjects(event.headers.origin)
          return response
      } catch (err) {
          return err
      }
  }
  
  </code></pre>
  
  <blockquote>
    <p><strong>Database functions</strong></p>
  </blockquote>
  
  <pre class="lang-js prettyprint-override"><code>require('dotenv').config()
  const mongoose = require('mongoose')
  const { Schema } = mongoose
  
  /**
   * This function establishes a connection to the MongoDB Atlas database.
   */
  exports.connectDatabase = async () =&gt; {
      await mongoose.connect(process.env.DATABASE_URL, {
          useNewUrlParser: true,
          useUnifiedTopology: true
      })
  }
  
  exports.ProjectSchema = mongoose.model(
      'project',
      new Schema({
          title: {
              type: String,
              required: [true, 'Title field is required']
          },
          description: {
              type: String,
              required: [true, 'Description field is required']
          }
      })
  )
  </code></pre>
  
  <blockquote>
    <p><strong>Fetch request from client</strong> </p>
  </blockquote>
  
  <pre class="lang-js prettyprint-override"><code>const App = () =&gt; {
      const [projects, setProjects] = useState([])
  
      useEffect(() =&gt; {
          fetch(API.GET_PROJECTS, {
              headers: { 'Content-Type': 'application/json' }
          })
              .then(res =&gt; res.json())
              .then(({ projects }) =&gt; setProjects(projects))
      }, [])
  
      return (...)
  }
  </code></pre>
  
  <blockquote>
    <p><strong>netlify.toml</strong></p>
  </blockquote>
  
  <pre><code>[build]
    functions = "functions"
  </code></pre>
  
  <blockquote>
    <p><strong>package.json</strong></p>
  </blockquote>
  
  <pre><code>{
    "scripts": {
      "build": "npm run build:client &amp; npm run build:server",
      "build:client": "webpack --config webpack.client.js",
      "build:server": "netlify-lambda build src/server/functions -c webpack.server.js",
      "start": "netlify-lambda serve src/server/functions",
      "start:dev": "npm run build:client -- --watch"
    }
  }
  </code></pre>