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:
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
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?
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:
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.