DEV Community

Cover image for How to setup AWS Lambda Layers (Nodejs)

How to setup AWS Lambda Layers (Nodejs)

Afraz Khan on July 03, 2021

Lambda layers were introduced in 2018 for flexibel code/data sharing within same or different AWS accounts. AWS Lambda supports multiple environ...
Collapse
 
craigfreeman profile image
Craig 🏔️🏳️‍🌈

Am I able to bundle all but one of my dependencies in with the lambda handler code using webpack but then put that one dependency in a layer? I'd prefer to only use a layer for a hefty dep that is unlikely to change and keep the rest bundled with the handler code.

Collapse
 
afrazkhan profile image
Afraz Khan

Agreed! Common use cases for layers involve heavy dependencies that are shared across multiple Lambda functions.

Collapse
 
colinbr96 profile image
Colin

In case anyone is wondering about this step: "index.js file that exports all node modules", here's what I did:

export * from "dependency-a";
export * from "dependency-b";
Enter fullscreen mode Exit fullscreen mode
Collapse
 
xxdannilinxx profile image
Danilo Machado

bro, you save my life! hahah

Collapse
 
afrazkhan profile image
Afraz Khan

glad to hear this :)

Collapse
 
weaponizedlego profile image
WeaponizedLego

This uploads en entire node_modules which is fine for smaller projects, but when the project size is 300 Lambdas, with a lot of dependencies, it exceeds the 250mb limit even without development dependencies. Do you have any suggestions on if it's possible to utilise tree shading, and some sort of import method to only put the methods you use instead of entire libraries into the layer?

Collapse
 
afrazkhan profile image
Afraz Khan

This looks possible only with your own custom packages, I don't know any technique to pick and choose the methods for 3rd party deps you are using in your code.

About the lambda deployment-package limit, I have two suggestions:

  • Consider distributing the packages across multiple layers, especially if you're dealing with 300 lambdas. There's a good likelihood of breaking down the packages into multiple layers and using a single layer for a particular set of lambdas based on their requirements.
  • Have you thought about utilizing Lambda Container images? They offer a deployment package capacity of up to 1GB. So, if you genuinely require a single package that surpasses the 250MB limit, you can spin-up containers to execute your lambda functions. here
Collapse
 
gauravdubey56 profile image
Gaurav Dubey

Thanks for the info, if I need to use models for my database and export my modules containing the query functions, should I keep it in my node_modules folder or outside of it, inside the nodejs folder, making the structure look like this-
nodejs
-node_modules
-custom-module1
-custom-module2

Collapse
 
afrazkhan profile image
Afraz Khan • Edited

You need to put your custom moduels into the node_modles folder. Nothing else should be there in nodejs folder like below
_ nodejs
__ node_modules
___ module0
___ module1
___ module2
.
.
.

Collapse
 
nermin99 profile image
Nermin Skenderovic

Are we supposed to replace file-name in the build command?

Collapse
 
afrazkhan profile image
Afraz Khan

yes :)