Creating a nodejs lambda layer for aws-sdk and other common npm libraries

  1. Create a nodejs.zip file to use in AWS Lambda layer
    • Open up a command prompt (terminal on macs)
    • Type mkdir nodejs
    • Type cd nodejs
    • Type npm install aws-sdk
    • Repeat npm install for any other common packages you use
    • Now we need to zip it up and package it for a lambda layer
      Type cd ..
      Then type zip nodejs or on Windows just right click on the nodejs folder and select Compress to ZIP File
  2. Create an AWS Lambda Layer
    • Open up your AWS Console
    • Navigate to Lambda and select Layers from the left hand menu
    • Click Create Layer
    • Fill in a name
    • Choose Upload a .zip file and add the nodejs.zip file we created above
    • Under Runtimes select a nodejs version that suits your node js code base. For me it was Node.js 18.x
    • Click the Create button
  3. Add the new lambda layer to a lambda
    • Find or create a lambda
    • Click on Add A Layer
    • Then choose custom layer and select your newly created lambda layer
    • Click Add
  4. That’s it! Now you can use require(‘aws-sdk’) in any lambda that includes this newly created lambda layer.

Note: When building lambdas that include layers, you may consider using “npm install -D <packagename>” when you need to include packages while you are developing. This will install the package locally for development only. For instance “npm install -D aws-sdk”. We wouldn’t want our lambda code including files for aws-sdk since they will be included automatically from our lambda layer.

Leave a Reply

Your email address will not be published. Required fields are marked *