# 502 errors on lambda function when deployed

**URL:** <https://answers.netlify.com/t/502-errors-on-lambda-function-when-deployed/32780>\
**Category:** Support\
**Tags:** lambda-functions\
**Created:** [February 19, 2021, 1:17am UTC](https://answers.netlify.com/t/502-errors-on-lambda-function-when-deployed/32780 "2021-02-19T01:17:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![StudioMASS](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/studiomass/32/22236_2.png) [@StudioMASS](https://answers.netlify.com/u/StudioMASS)\
**Post date:** [February 19, 2021, 1:17am UTC](https://answers.netlify.com/t/502-errors-on-lambda-function-when-deployed/32780/1 "2021-02-19T01:17:02Z")

</div>

I’m trying to add a lambda function to my Gatsby site, which is deployed to Netlify via Gatsby Cloud. The function automatically sends the user a formatted email when they submit a form - I’m using a custom callback in the form response handler rather than the `submission-created.js` approach. In `netlify dev` it works, however when I try it on the deployed site, I get a 502 error:

 ![Screen Shot 2021-02-19 at 10.47.04 am](https://global.discourse-cdn.com/netlify/original/3X/4/f/4f5d326976a09780d34731196262bfca2056b530.png)

Here’s the function code:

```
const sendgrid = require('@sendgrid/mail');

const getFirstName = (name) => {
  const names = name.split(' ');
  return names[0];
};

function sendEmail(client, applicant, email, senderEmail, senderName) {
  return new Promise((fulfill, reject) => {

    const data = {
      from: {
        email: senderEmail,
        name: senderName,
      },
      subject: 'Application received',
      to: `${email}`,
      html: `Hi ${getFirstName(applicant)},
        <br><br>
       Thanks for your interest, we'll be in touch!
      `,
    };

    client
      .send(data)
      .then(([response]) => {
        fulfill(response);
      })
      .catch((error) => reject(error));
  });
}

exports.handler = function (event, callback) {
  const applicant = event.queryStringParameters.applicant;
  const email = event.queryStringParameters.email;

  const { SENDGRID_API_KEY } = process.env;

  sendgrid.setApiKey(SENDGRID_API_KEY);

  sendEmail(sendgrid, applicant, email, 'our-email@email.com', 
'Our Name')
    .then((response) => callback(null, { statusCode: response.statusCode }))
    .catch((err) => callback(err, null));
};

```

Thanks!

---

<div class="post-metadata">

**Author:** ![StudioMASS](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/studiomass/32/22236_2.png) [@StudioMASS](https://answers.netlify.com/u/StudioMASS)\
**Post date:** [February 19, 2021, 6:08am UTC](https://answers.netlify.com/t/502-errors-on-lambda-function-when-deployed/32780/2 "2021-02-19T06:08:44Z")

</div>

Realised I forgot to make the `exports.handler` an async function. Have fixed that now, but still getting the 502, and this error when I log the response:

`Error: Cannot find module '/var/task/src/node_modules/@sendgrid/mail/index.js'. Please verify that the package.json has a valid "main" entry`

Seems to be something to do with installing the function dependencies, however I have setup a `package.json` in the `/functions` folder with `@sendgrid/mail` listed as a dependency and `emailApplicant.js` (the name of the lambda function) as the `main`, and added an `onPostBuild` function in `gatsby-node.js` to `npm install` the dependencies as stated here: [https://support.gatsbyjs.com/hc/en-us/articles/360054529274-Deploying-Netlify-Functions-from-Gatsby-Cloud](https://support.gatsbyjs.com/hc/en-us/articles/360054529274-Deploying-Netlify-Functions-from-Gatsby-Cloud)

(This is Gatsby specific but might be helpful)

```
const util = require("util");
const child_process = require("child_process");
const exec = util.promisify(child_process.exec);

exports.onPostBuild = async (gatsbyNodeHelpers) => {
  const { reporter } = gatsbyNodeHelpers;

  const reportOut = (report) => {
    const { stderr, stdout } = report;
    if (stderr) reporter.error(stderr);
    if (stdout) reporter.info(stdout);
  };

  reportOut(await exec("cd public/functions && npm install"));

```

---

<div class="post-metadata">

**Author:** ![StudioMASS](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/studiomass/32/22236_2.png) [@StudioMASS](https://answers.netlify.com/u/StudioMASS)\
**Post date:** [February 19, 2021, 7:28am UTC](https://answers.netlify.com/t/502-errors-on-lambda-function-when-deployed/32780/3 "2021-02-19T07:28:40Z")

</div>

PS Here is the site: [https://602f541336377987ffe38b0d--studiomass.netlify.app/careers](https://602f541336377987ffe38b0d--studiomass.netlify.app/careers)

Submitting one of the forms triggers the function

---

<div class="post-metadata">

**Author:** ![hillary](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hillary/32/21132_2.png) [@hillary](https://answers.netlify.com/u/hillary)\
**Post date:** [February 24, 2021, 6:51pm UTC](https://answers.netlify.com/t/502-errors-on-lambda-function-when-deployed/32780/4 "2021-02-24T18:51:15Z")

</div>

Hey there, @StudioMASS 👋

Thanks so much for reaching out! Sorry for the delay in response. This is a bit outside the scope of my expertise, but I am going to loop in some folks who may be able to support you here!

@jonsully , I know that you have experience with lambda functions as well as @AaronP . Can you two take a look here and see if you can identify some resources or next steps?

Thanks!

---

<div class="post-metadata">

**Author:** ![jonsully](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jonsully/32/15053_2.png) [@jonsully](https://answers.netlify.com/u/jonsully)\
**Post date:** [February 24, 2021, 7:41pm UTC](https://answers.netlify.com/t/502-errors-on-lambda-function-when-deployed/32780/5 "2021-02-24T19:41:05Z")

</div>

Hey @StudioMASS! 👋🏻

And welcome to The Forums :netliheart:

Two things I’m seeing off the bat here — first, within your `handler` function (which you did need to make `async` so +1 for that) you’ll need to use `await` before calling `sendEmail()`. Otherwise the call to `sendEmail()` will be considered an async call that gets put onto the call stack but never called once the execution of the `handler` function / stack ends. That’s one of the realities of using AWS Lambda so not much we can do there — Lambda does not continue to process function calls on the call stack beyond `handler` itself once `handler` ends. You _must_ use `await` for any async function you want to run during your function. 👍🏻

Second, in terms of build / dependency stuff, can you try the approach of a second nested folder for each function name? This looks like this:

```auto
.
└── project-name/
    ├── src (your actual static site)/
    │ ├── js
    │ ├── css
    │ └── html/
    │ ├── index.html
    │ └── etc
    └── functions/
        ├── my-function-name/
        │ ├── index.js
        │ └── package.json
        └── my-second-function/
            ├── index.js
            └── package.json

```

It’s just an extra nested folder (folder name == function name, js file is just “index”) so that each function gets their own `package.json` for more isolated dependencies. Netlify should automatically resolve and package those dependencies if you follow this pattern.

Let me know if that helps!

–  
Jon
