Status of 502 for lambda functions - timeout 10s?

Hi there,

when deployed I see my lambda functions return status 502. I know there is a timeout after 10s.
Testing the page local with the cli, I get response times like these most of the time:
Response with status 200 in 454 ms.
after page refresh
Response with status 200 in 58 ms.
and never above 800ms. The function fetches data from fauna db and on the test page I’ve removed all function calls but one.

I am currently stuck and don’t know how to investigate further.

Additional info:

netlify lambda function js:

const {q, client} = require("./utils/fauna")

console.log("Function `getHitsOverall` invoked");

exports.handler = (event, context, callback) => {

  // Get over all hits

  const query = q.Count(q.Documents(q.Collection("hits")));
  
  return client.query(query)
    .then(response => {
      return callback(null, {
        statusCode: 200,
        body: JSON.stringify(response)
      });
    })
    .catch(error => {
      console.log("error", error);
      return callback(null, {
        statusCode: 400,
        body: JSON.stringify(error)
      });
    });
}

utils/fauna.js

const faunadb = require("faunadb")

const client = new faunadb.Client({
  secret: process.env.FAUNA_SECRET

  // Also tried this:
  / /keepAlive: false
});
const q = faunadb.query;

module.exports = {
  client,
  q
}

Any help appreciated

I’ve changed the callback to async => no more timeouts in production:

const {q, client} = require("./utils/fauna")

console.log("Function `getHitsOverall` invoked");

exports.handler = async (event, context) => {

  // Get over all hits

  const query = q.Count(q.Documents(q.Collection("hits")));
  
  try {
      const response = await client.query(query);
      return {
        statusCode: 200,
        body: JSON.stringify(response)
      };
    } catch (error) {
      console.error(error);
      return {
        statusCode: 400,
        body: JSON.stringify(error)
      };
    }
  }

awesome! thanks for sharing!