Is it possible to use a lambda function during the build stage?

I’ve written a Netlify function that collates some data from a 3rd party API that I want to call and put the response in my js bundle at build stage but it seems to me that Netlify runs build before processing the functions folder… Is there any way to change this?

@bristoljon, at this time, there is no way to change the build order.

One possible workaround might be to make this function part of a separate site for the same team. Then do this:

  • build the first site with the function so it exists and/or is updated
  • build the second site which will call the function for the first site during the build

Functions were designed to be used as API endpoints for the deploy site - not the build process. While it is possible to use them during the build, this isn’t what they were designed for so a bit of “shoehorning” may be required. It should work though.

Thanks Luke for the suggestion. I hadn’t thought of that approach. One idea I was going to look into is whether I can use netlify-lambda to run the function during build stage as if it was dev mode and make it available to the build process… Any thoughts on that? I’ve not used netlify-lambda directly before but may experiment with it.

Interesting idea! You might be able to do that! Certainly you should be able to connect to localhost where netlify-lambda serve would open a connection to/from.

Let us know how it goes!

So I ran into trouble with netlify-lambda on a few points, environment variables not being pulled in when running locally and then killing the server cleanly after the build process had completed but seemed like it might have worked.

In the end though, I’ve gone for sharing code between the lambda function and the build script. In my case I had separate files for each of the different api requests that simply return the data. The lambda entry file calls them all and puts the responses into a single http response while my build script calls them and writes the data to json for importing into my redux store initial state so this proved an easier solution for me.

1 Like

thanks for sharing your solution, @bristoljon!