I’ve given Gatsby functions a test drive since they work with Netlify. I’ve got the majority of the functionality for my lambda function working, but I’m hung up on one thing…including additional files that the function can use.
My function is designed to accept a POST request from a contact form on a website and then send an HTML email to an email address. To create the body of the email, I’m using EJS so that I can have an HTML template, fill in some variables received from the contact form, and then send it through Mailgun.
As you’ll notice, the included files are not present. However, when I run the same function re-purposed for Netlify, the included files do show up locally:
Here’s the output from the gatsby function on Netlify.
12:29:04 PM: 74e298a1 INFO /var/task/functions/gatsby/functions/sendmail/email_templates/contact.ejs
12:29:04 PM: 74e298a1 ERROR - Error rendering EJS template for a form named 'contact'.
Hi @dmorda
The included_files aren’t copied into .cache/functions, which is generated by Gatsby. The function generated by Netlify won’t be called “sendmail”, but something like __gatsby-handler depending on the version of your build plugin. If you run netlify build locally it will create a zipfile in .netlify/functions called something like __gatsby-handler.zip. If you look inside there, either by running zipinfo or by unzipping it and opening the folder in an IDE or somewhere else that shows hidden folders, and see what files are included. You’ll also see it in functions-serve if you run netlify dev.
Thanks for the clarification @ascorbic, I should have tried the build step myself and explored the generated file. Although I somewhat like the idea of using the “included_files” better, I ended up sorting this out as a Gatsby function.
I simplified my email templates into a single one flexible enough to handle any email that I need to generated. I then used an “import” statement that specifies that the raw-loader should be used.
import emailTemplate from '!raw-loader!./email_templates/default.ejs'
Now the template is bundled into the Gatsby function as a dependency and that seemed to do the trick.
Glad everything is working for you now, @dmorda Thanks for outlining the exact steps you took, this will definitely be beneficial for future Forums members who encounter something similar.