Using data from Lambda layers

answered
#1

I have some functions that I’d like to deploy, but they use data from files in a folder in node_modules without directly specifying each path using require, so those data files don’t get bundled with the function.

It feels like Lambda layers might be a good place to store this extra data for use in Netlify functions. Is it possible to configure a function so that it has access to one or more layers?

#2

Hi @aeaton,

Thanks for joining the community. I actually never considered that in the past, but at the moment you can’t use layers with Netlify deployed lambda functions. The current best way to get the data you need is to include it as part of the deploy. You can zip your function and it’s node_modules and deploy that together and then your function will have access to those node_modules. However at some point you’ll have to pull in that data, are you accessing them directly via the file system? Is there a reason you aren’t importing them using require or something similar? I’d love to understand your use-case more so we can see if there’s a better workflow for you.

#3

I’m using the Netlify GitHub app to build and deploy the functions - is it possible to tell the builder that it should include the node_modules folder (or specific parts of it)?

#4

The files are accessed via the file system, yes. The path to an initial file is located using require.resolve(…), then from there other files are loaded via a relative path.

#5

Hi @aeton you’ll need to zip the function and node_modules together and deploy that zipped directory. Then you’ll have access to anything in your module the same way you would any other dependency.

1 Like