Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda layers node_modules

I am creating a lambda layer, bundling up some dependencies, including node_modules. I am successfully creating a layer but when i try to require a module from my code, the console is telling me that the module cannot be found. Here is the code

var Promise = require('promise');
module.exports.handler = function(event, context, callback) {   
  new Promise(function (resolve, reject) {
    setTimeout(function() {
      callback(null, "helloWorld2");
    }, 9000);
  });
};

How can I reference node modules from a layer???

like image 263
James Gunn Avatar asked Dec 15 '18 01:12

James Gunn


3 Answers

How are you running your lambda? If via sam cli, something like the below has worked for me as my template.yaml ...

example template

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: example of node.js lambda function using layer for dependencies

Resources:

  ExampleFunction:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: nodejs8.10
      CodeUri: nodejs/
      Handler: src/event.handler
      Layers:
        - !Ref NodeModulesLayer

  NodeModulesLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      Description: full set of function dependencies
      ContentUri: ./
      CompatibleRuntimes:
        - nodejs6.10
        - nodejs8.10
      LicenseInfo: 'Available under the MIT-0 license.'
      RetentionPolicy: Retain

pointing to local layer

The SAM developer guide includes a page on Working with Layers. At the time I'm writing this, they don't really get into how to reference layers at local file paths, and instead focus on references to remotely hosted layers.

The aspect I found tricky is that the directory structure of a node.js layer is expected to be ...

nodejs/
  node_modules/

... which means that in order for your locally installed node_modules directory to work as a layer, your package.json file must be nested inside a folder named nodejs.

Note the paths in the above example template.yaml:

  1. ExampleFunction.Properties.CodeUri is set to nodejs/
  2. ExampleFunction.Properties.Handler should be set to the path to your handler file, relative to nodejs/.
  3. NodeModulesLayer.Properties.ContentUri is set to the folder that contains both the template.yaml file and the nodejs dir.

Which means my example is assuming the following structure ...

nodejs/
  node_modules/
  src/
    event.js
  package.json
template.yaml

preserve sam build support

One additional gotcha to be wary of ...

With respect to defining your function resource in template.yaml, there's some "flexibility" in terms of which parts of the path you put in CodeUri vs Handler. In some cases, doing ...

    Properties:
      CodeUri: nodejs/src/
      Handler: event.handler

... works just as well as doing ...

    Properties:
      CodeUri: nodejs/
      Handler: src/event.handler

BUT, if you're using the sam build command, the former will NOT work. That command expects to find package.json inside of the CodeUri directory. So, stick with CodeUri: nodejs/ and use the Handler value to navigate through any additional folder hierarchy necessary to reaching your handler.

like image 123
RH Becker Avatar answered Oct 21 '22 20:10

RH Becker


Try this, simple example how to set up lambda layer in nodejs:

https://medium.com/@anjanava.biswas/nodejs-runtime-environment-with-aws-lambda-layers-f3914613e20e

like image 2
Levi Avatar answered Oct 21 '22 21:10

Levi


Dependencies in 2022...

It was quite hard for me to figure out, whether this is still state-of-the-art or we can simplify dependencies now. Turns out it's possible now to include dependencies without hacks or complicated setup.

For me, creating the lambda with new lambda.NodeJsFunction() instead of new lambda.Function() did the trick. Yet, it was super hard for me to find a working sample. I decided to share a sample repo with you.

Sample repository

https://github.com/AntoniusGolly/cdk-lambda-typescript-boilerplate

What it does:

  • it has a lot of stuff you don't need to demonstrate the purpose (e.g. it produces a API Gateway based on a config.ts, so ignore that part if you don't need it)
  • it allows you to use TS and ES6
  • it allows you to use a dependency: i.e. I use node-fetch here
  • I just do cdk deploy and no other bundling
  • according to the docu by default, all node modules are bundled except for aws-sdk.
  • I don't know exactly how it works, but it does ;)

I hope this helps someone as I would have appreciated it.

like image 1
agoldev Avatar answered Oct 21 '22 20:10

agoldev