3

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

1
  • If you will bundle the node modules with the code, you won't be able to read/edit the code directly from the aws-cli because of large size. If you are ok to not to have that option go ahead and bundle everything together. AS even if you create a layer for module you still may endup with large project size later. Commented Jan 31, 2019 at 9:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. Let's say I have a lambda that was written a year ago and did not change and I have a new lambda that I'm going to write today. I need to decide if I stay with the old package version of a lib (e.g. redis client) or update the older lambda to use the newer version of the client. Is that really the best practice here? Isn't one of the benefits of lambda is isolation? Isn't it better to create a layer per lambda in that situation?
Creating the layer allows you to update the redis client in all functions at once and reduce all package sizes, that is the bright side, but you gotta test that all functions are compatible with the new lib/new layer. Keeping libraries up to date is important for security and layers help with that, but you can do it with your package manager too. For 10-15 functions, i'd say just updating the packages is easier, unless the libs are really huge. If it gets to more, say 50 functions, harder to manage libs and a lot of duplicate libs inside packages, then layers help a lot.
That's correct assuming the new client has no breaking changes. It seemed like a dirty solution for me. So, let me get to your point. One layer for all lambdas created somehow automatically prior to the deploy. The lambdas in that case will be small and the cold loads will be faster. BTW, do you have any idea how does the layer load on cold loads?
Yeah, testing with the updated libs is the hardest part, but that got nothing to do with lambda. When your lambda function is loaded for execution, it has to download the package from S3. With layers, the libs and files will be already in the filesystem, so less to load. Also, if you have different configurations, keep in mind you can use up to 5 layers at once.
And to better understand cold starts, take a look at ¨Understanding Container Reuse in AWS Lambda¨: aws.amazon.com/blogs/compute/container-reuse-in-lambda
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.