9
votes

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

4
what's the name of your theme? looks nice! - nedo

4 Answers

8
votes

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

0
votes

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.

0
votes

I use layers in serverless offline via installing a layer from local file system as a dev dependency.

npm i <local_path_to_my_layer_package> --save-dev
-2
votes

BTW this issue was fixed in sls 1.49.0. Just run:

sudo npm i serverless

Then you should specify package include in serverless.yml's layer section

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
    package:
    include:
    - node_modules/**
    name: ${self:provider.stage}-layerName # optional, Deployed Lambda layer name

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

Tested on nodejs10.x runtime