How to use layers with Lambda functions?
ฝัง
- เผยแพร่เมื่อ 10 ก.พ. 2025
- ⚡️ Blog post: www.undefineda...
In this video we look at how to use layers with AWS Lambda functions. (with nodejs)
We look at a demo app which uses two npm modules (axios and image-size) which were installed using npm and are part of the bundle. We then extract these modules into a layer. We attach this layer to our function and import and use the modules we extracted.
It has 2 advantages:
it reduces the size of the bundle
it makes it possible to re-use the extracted code across several functions.
We also look at how to separate local files and extract them into a layer and then import them from the function.
If you would like to see an example of using layers, check out this video • How to use ImageMagick... in which we use a layer to install ImageMagick to resize images within a lambda function.
⭐️ Subscribe for more content: newsletter.und...
#lambda #layers #nodejs
⚡️ NEW VIDEO: How to use versioning and aliases to deploy lambda functions? (/w API Gateway) th-cam.com/video/OGMaE63YgEU/w-d-xo.html
Hey BiteSize Academy, Great work! I have learnt a lot from your videos.. Can you make a video with a monorepo setup for deploying multiple lambdas (using lambda layers for common modules)? I think it a real world use case and might help a lot of people :)
I will never understand...
why every time I have doubts, the official documentation is useless.
But forums and users explain really nice
Simple and straightforward. Really helpful.
[6:48] If you put `util.js` file inside `node_modules/` folder in the layer, you can do `require('utils')` directly without hardcoding the `/opt` path.
I don't think is a good idea to put things inside the node_modules folder. Unless you have implemented a hook after npm install, the file will not be added automagically (usually the node_modules folder is not committed to the git repository)
@@cristhianvalencia5793 it can be done as part of the build step...maybe util.js lives in a lib folder and gets copied when the lambda layer is deployed
Thanks. I was hoping such a thing was possible but hadn't learned how until this video.
Just the right amount of information, and great pacing, thanks!
Was instantly useful, thanks for the video!
This was extremely helpful and simple. Thank you!
This really helped me out, thanks bro.
How do you do local testing for layer import? /Opt I assume is not the right path in local machine
send to local /nodejs/node_modules/your_lib and referece in
tsconfig.json
paths: {
"/nodejs/node_modules/yourpackage": ["/nodejs/node_modules/yourpackage"]
}
import { item } from "/nodejs/node_modules/yourpackage"
Do you know the structure of a layer in ES6 V3 for node22 or node20 with import statements. I follow this tutorial, and AWS guides. Mostly "package.json" has the element "type: "module". The modules are unzip and loaded, but the "opt/directory" or the JS files are not found.
Thanks! Its helpful :)
Perfect explanation! Thanks a lot :)
Great succinct presentation. Many thanks.
How youm put this file in function e.g. node_modules, package.json etc file which you remove at 6:35
Really appreciate if you can make a video in kinesis-data-stream and lambda function to consume the stream
simple and straight to point
How would this work with CloudFormation or SAML can you specify a layer there as well? Great video!
thank you very good video and well explained
you mean, after creating the layer, the codes in the layer don't run anymore? which makes the bundle much faster?
Great Video! It is really helpful. But I do have a question. If I use the layer to just have the node modules, would this be considered preloading dependencies and would make cold starts take less time?
really usefull, thank you
Thanks Sir!!
@BiteSizeAcademy, during development on your local machine, require('/opt/utils') won't work. Would you just do require(process.env.NODE_ENV === 'production' ? '/opt/utils' : './utils') so that it automatically knows where to look for the utils folder?
so your utils file also import a node package (axios). How does it know to get that package from the nodejs folder
Actually I see captain jack comment, and I think that answers my question.
This video explains it in more detail: th-cam.com/video/RnFowJ130pc/w-d-xo.html (How to install npm modules in AWS Lambda?)
At time 5:13, line 1 shows the import statement in utils.js. `const axios = require('./nodejs/node_modules/axios')`
I followed the same steps but "/opt/secrets" is showing "Error: Cannot find module '/opt/secret'\
Silly question: Why using variable type "var" in "var sizeOf" and not "const"?
awesome
my life saver
This would be better if you actually demonstrated calling a layer function from the index file. It's unclear from this that you don't have to add any directory structure; you just refer to the module as you would in a non-layered lambda function, but based on this video I tried prefixing 'opt' and then 'nodejs/node_modules', etc. until I realized you don't have to do anythingl.