Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serverless Offline undefined module when loaded from lambda layer

I have the following project tree

enter image description here

Where nodejs folder is a lambda layer defined in the following serverless.yaml

service: aws-nodejs # NOTE: update this with your service name


provider:
  name: aws
  runtime: nodejs8.10
  stage: dev

plugins:
  - serverless-offline
layers:
  layer1:
    path: nodejs # required, path to layer contents on disk
    name: ${self:provider.stage}-layerName # optional, Deployed Lambda layer name

functions:
  hello:
    handler: handler.hello
    layers:
      - {Ref: Layer1LambdaLayer}
    events:
      - http:
          path: /dev
          method: get

The layer1 only contains UUID package.

So when I try to run the lambda locally using serverless offline plugin, it says can't find module UUID.

But when I deploy the code to AWS, it run like a charm.

Any way we can get lambda layers running locally for testing purpose? and for speeding up the development?

Or is there any way where I can dynamically set the node_module path to point to the layer folder during the development and once I need to push to production, it change the path to the proper one

like image 449
Allloush Avatar asked Apr 09 '19 10:04

Allloush


2 Answers

Ok after many trials, I figure out a working solution

I added a npm run command which export a temporary node_module path to the list of paths

"scripts": {
    "offline": "export NODE_PATH=\"${PWD}/nodejs/node_modules\" && serverless offline"
  },

So, node can lookup for the node modules inside the sub folders

like image 157
Allloush Avatar answered Nov 09 '22 22:11

Allloush


I got around this by running serverless-offline in a container and copying my layers into the /opt/ directory with gulp. I set a gulp watch to monitor any layer changes and to copy them to the /opt/ directory.

like image 26
Sam Chung Avatar answered Nov 09 '22 22:11

Sam Chung