sebastiandurandeu
sebastiandurandeu

Reputation: 261

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.

Upvotes: 10

Views: 5903

Answers (1)

Yann
Yann

Reputation: 2532

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.

Upvotes: 13

Related Questions