Amir Popovich
Amir Popovich

Reputation: 29836

How should I handle node_modules on AWS Lambda using NodeJS?

I've started using AWS Lambda for a small site. I will have around 10-15 lambdas connected to different triggers (API's, SQS etc.)

We are going to use AWS SAM for creating our stacks and NodeJS for most of the lambda methods.

I've searched a lot to find the best practices for the node_modules and couldn't really get to a conclusion. Most of the proposed solutions included layers and I couldn't really understand how I should build those layers and how they work side by side with local testing.

Questions:

  1. Whats the difference between using the node_modules "as is" and converting the node_modules directory to a layer? Will it benefit performance wise? Will it benefit only when a cold lambda is loaded?

  2. If layers is the solution, should I use one layer for all lambdas (e.g. some kind of CI script that will "merge" them pre deploy)? Or create a mono-repo solution using all the same node_module versions across all lambdas? This seems a bit not clean but I have seen recommendations like these while searching for the best solution.

I'm looking for a decent solution that will not hurt performance wise and an option to work locally and test our code before deploying it..

Upvotes: 3

Views: 2354

Answers (1)

Julio Faerman
Julio Faerman

Reputation: 13501

Separating the libraries in a layer would share them across your functions, reducing package size and cold start time. How much depends on your application, but i'd say that for few functions, unless you use a lot of libraries, it is simpler to just bundle them in the deployment package. If it grows to more functions and library configurations, it can help to create a lambda layer, but i'd not do it from the start.

Upvotes: 1

Related Questions