DEV Community

Cover image for Easy-Peazy Node Dependencies in AWS Lambda Using Layers
Katherine
Katherine

Posted on

Easy-Peazy Node Dependencies in AWS Lambda Using Layers

Do "Cannot find module Runtime.ImportModuleError" errors have you down?

I honestly thought one of the big selling points of AWS Lambdas were that they were super fast to spin up. But what is a NodeJs method without some npm/yarn dependencies? Kidding, but I was disappointed to find out that there wasn't an easy way to run npm install and keep editing my handler function in the AWS Lambda code editor.

There is a way: layers. Using layers in Lambda functions allows for importing NodeJs dependencies via the AWS code editor UI. These are the steps:

  1. Compress Dependencies
  2. Create a Layer
  3. Attach Layer & Deploy Lambda

1. Compress Dependencies

The goal of this step is to end up with a zipped archive of the node_modules folder with just the dependencies you'll need for your lambda.

Start by creating an empty directory named nodejs and then run npm init inside of it by running these commands line-by-line:

mkdir nodejs

cd nodejs

touch index.js

npm init -y
Enter fullscreen mode Exit fullscreen mode

Once you have the directory, install dependencies that your Lambda will need using npm i --save ... (I'm using haikunator as an example):

npm i --save haikunator
Enter fullscreen mode Exit fullscreen mode

Then require/export the dependencies in the main file of your project. Do this by writing exports.exampleDependency = require('example-dependency') for each dependency you have in the project.

You can just write the export statement in the index.js file directly, or run the below statement via CLI (of course substituting haikunator for your own dependency).

cat > index.js
exports.haikunator = require('haikunator');
Enter fullscreen mode Exit fullscreen mode

Final index.js file:

exports.haikunator = require('haikunator ');
Enter fullscreen mode Exit fullscreen mode

That's the hard part! Now you need to zip up the folder with index.js/ package.json/ node_modules. This is what your directory structure should look like before it is zipped up:

Index, package.json and node_modules in a folder


2. Create a Layer

Once you've zipped the files, go to Lambda > Layers in the AWS UI. Create a new layer (independent of your Lambda function - that part comes later) named whatever you want with Node.js as the compatible runtime. Upload your nodejs.zip archive as the layer code.

Creating a layer in AWS UI


3. Attach Layer & Deploy Lambda

This is the last step and is the part that links the dependencies to your Lambda. Either create a new Lambda function or navigate to an existing one. Click on "layers" in the designer section of the UI and go to "Add a Layer."

Editing layers for your Lambda

The Add Layer screen will show something like the below. Choose Custom Layers and the layer created in the previous step (the one where nodejs.zip was uploaded) will appear in the dropdown. Click "add" and you're good to go!

Add your created layer to your Lambda handler

Now the npm/yarn dependencies are available to use in the Lambda code editor.

Quick tip: if you followed the steps and named the zip file as directed, dependencies can be accessed via require('dependencyName').
Otherwise, access your dependencies via the /opt/nameOfZip path. For example: if you uploaded myDir.zip, use require('/opt/myDir')).

Here is my example code living in the Lambda function:

const haikunator = require('haikunator');
// if layer has custom zip name use the below
// const { haikunator } = require('/opt/nodejs');

const handler = async (event) => {

    const h = new haikunator();
    const response = {
        statusCode: 200,
        body: h.haikunate({
            tokenLength: 0,
            delimiter: '-'
        })
    };
    return response;
};

exports.handler = handler;
Enter fullscreen mode Exit fullscreen mode

That's it! Update Press the Deploy button to your heart's content and update code in the AWS editor. Happy layering!

Top comments (0)