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

ความคิดเห็น • 39

  • @BiteSizeAcademy
    @BiteSizeAcademy  3 ปีที่แล้ว

    ⚡️ NEW VIDEO: How to use versioning and aliases to deploy lambda functions? (/w API Gateway) th-cam.com/video/OGMaE63YgEU/w-d-xo.html

    • @rohitkrishnan5456
      @rohitkrishnan5456 3 ปีที่แล้ว

      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 :)

  • @nallibtala3800
    @nallibtala3800 2 ปีที่แล้ว +6

    I will never understand...
    why every time I have doubts, the official documentation is useless.
    But forums and users explain really nice

  • @niravarma
    @niravarma 3 ปีที่แล้ว +3

    Simple and straightforward. Really helpful.

  • @nichenjie
    @nichenjie 4 ปีที่แล้ว +13

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

    • @cristhianvalencia5793
      @cristhianvalencia5793 4 ปีที่แล้ว

      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)

    • @rohitkrishnan5456
      @rohitkrishnan5456 3 ปีที่แล้ว

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

  • @McMurdoStation
    @McMurdoStation 2 ปีที่แล้ว

    Thanks. I was hoping such a thing was possible but hadn't learned how until this video.

  • @colin-vandervoort
    @colin-vandervoort 2 ปีที่แล้ว

    Just the right amount of information, and great pacing, thanks!

  • @eduardboiko7219
    @eduardboiko7219 ปีที่แล้ว

    Was instantly useful, thanks for the video!

  • @adrianarizpe7397
    @adrianarizpe7397 2 ปีที่แล้ว

    This was extremely helpful and simple. Thank you!

  • @luvodlulisa7883
    @luvodlulisa7883 2 ปีที่แล้ว

    This really helped me out, thanks bro.

  • @AnhNguyen-vu7mc
    @AnhNguyen-vu7mc 3 ปีที่แล้ว +5

    How do you do local testing for layer import? /Opt I assume is not the right path in local machine

    • @deyvisonborges
      @deyvisonborges 2 ปีที่แล้ว

      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"

  • @GuillermoCervantes-v5v
    @GuillermoCervantes-v5v 14 วันที่ผ่านมา

    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.

  • @testatestb
    @testatestb ปีที่แล้ว

    Thanks! Its helpful :)

  • @matthewhill3556
    @matthewhill3556 3 ปีที่แล้ว

    Perfect explanation! Thanks a lot :)

  • @RC-vr7fl
    @RC-vr7fl 3 ปีที่แล้ว

    Great succinct presentation. Many thanks.

  • @in.hardikwts
    @in.hardikwts ปีที่แล้ว

    How youm put this file in function e.g. node_modules, package.json etc file which you remove at 6:35

  • @ashil816
    @ashil816 3 ปีที่แล้ว +1

    Really appreciate if you can make a video in kinesis-data-stream and lambda function to consume the stream

  • @lonewolf2547
    @lonewolf2547 4 ปีที่แล้ว

    simple and straight to point

  • @kfliden
    @kfliden 2 ปีที่แล้ว

    How would this work with CloudFormation or SAML can you specify a layer there as well? Great video!

  • @chrismuh5337
    @chrismuh5337 3 ปีที่แล้ว

    thank you very good video and well explained

  • @ZacLow
    @ZacLow 3 ปีที่แล้ว

    you mean, after creating the layer, the codes in the layer don't run anymore? which makes the bundle much faster?

  • @karimfayed3050
    @karimfayed3050 2 ปีที่แล้ว

    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?

  • @guillaume5623
    @guillaume5623 2 ปีที่แล้ว

    really usefull, thank you

  • @farooquitaha
    @farooquitaha 4 ปีที่แล้ว +1

    Thanks Sir!!

  • @andrewcbuensalida
    @andrewcbuensalida 4 หลายเดือนก่อน

    @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?

  • @paqliam
    @paqliam 4 ปีที่แล้ว

    so your utils file also import a node package (axios). How does it know to get that package from the nodejs folder

    • @paqliam
      @paqliam 4 ปีที่แล้ว

      Actually I see captain jack comment, and I think that answers my question.

    • @BiteSizeAcademy
      @BiteSizeAcademy  4 ปีที่แล้ว

      This video explains it in more detail: th-cam.com/video/RnFowJ130pc/w-d-xo.html (How to install npm modules in AWS Lambda?)

    • @karlang2637
      @karlang2637 3 ปีที่แล้ว

      At time 5:13, line 1 shows the import statement in utils.js. `const axios = require('./nodejs/node_modules/axios')`

  • @kaushalendrapandey2662
    @kaushalendrapandey2662 3 ปีที่แล้ว

    I followed the same steps but "/opt/secrets" is showing "Error: Cannot find module '/opt/secret'\

  • @hgiangphan
    @hgiangphan 2 ปีที่แล้ว

    Silly question: Why using variable type "var" in "var sizeOf" and not "const"?

  • @lautarovaleiras3623
    @lautarovaleiras3623 ปีที่แล้ว

    awesome

  • @peterlau2076
    @peterlau2076 4 ปีที่แล้ว

    my life saver

  • @sdbyrd56
    @sdbyrd56 ปีที่แล้ว

    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.