Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using AWS Lambda Layers shall I prefer multiple layers or a single layer with all the dependencies?

We have several AWS Lambdas in Node.js (v10.x) and some of them have some "specific big" dependencies that are not shared across all of them. For example, we have an "Authorizer" function that depends on the firebase-admin NPM package and this is the only Lambda that uses it.

To make it simpler to maintain, at the moment we have a single Layer that contains all the dependencies. Is there any benefit in splitting the dependencies in more than one Layer? My assumption is that a bigger layer increases "cold start" time. However, how big is that penalty in relation to the size of the Layer? I could not find any reference in the AWS Documentation about this.

Similar question, but across different languages. If we use multiple languages for our Lambdas (eg. Node and Python), can I have a single Lambda layer that contains dependencies for both languages? I guess this is more of an easy choice as the Python dependencies will effectively never be used by the Node.js code. However, I would like to read some AWS recommended best practices to manage layers.

like image 299
sebastiandurandeu Avatar asked Feb 24 '20 18:02

sebastiandurandeu


People also ask

Can Lambda have multiple layers?

You can add up to five layers to a Lambda function. The total unzipped size of the function and all layers cannot exceed the unzipped deployment package size quota of 250 MB. For more information, see Lambda quotas.

How many layers can a Lambda function use at a time?

Overview of Lambda layers You can include up to five layers per function, which count towards the standard Lambda deployment size limits.

What is the advantage of using layers in Lambda functions?

Lambda layers provide a convenient way to package libraries and other dependencies that you can use with your Lambda functions. Using layers reduces the size of uploaded deployment archives and makes it faster to deploy your code.

In which of the following use case a Lambda layer should not be used?

Hence my recommendation is to not use Lambda Layers as a way to distribute shared code, except for the following AWS Lambda use cases: Large dependencies that are difficult to distribute otherwise and seldom change. Examples include the FFmpeg and Pandoc executables.


1 Answers

A Lambda with all its layers unpacked should be less than 250 MB, if you are close to this size consider splitting the layers and use only the ones that you need.

You can mix dependencies for different languages in one layer as it's just files that you can reuse in your Lambda runtime. As you said it will increase cold start and each function that uses the common layer will download the layer independently. So if it's easier for you to maintain everything in one layer keep it as is, however, in general, it's not a good practice to load the code you are not going to use.

On the other hand, each function can have only up to 5 layers, so with splitting the common code and dependencies into small layers, you may reach this limit soon.

like image 181
Yann Avatar answered Oct 20 '22 18:10

Yann