1

I am trying to configure and deploy some layers in AWS using the Serverless framework.
The structure of the directory where the files for the layers are, is this:

lambda-layers
    |
    | - test-layers
         |
         | - nodejs
         |      | - index.js
         |      | - test.js
         |
         | - serverless.yml   

My serverless.yml file looks like this

service: test-layers

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: region

layers:
  TestLayer:
    path: nodejs

I run sls deploy and I can see that the layer has been deployed, but when I try accessing functions from the files in the layer in my Lambda like this:
const index = require('/opt/nodejs/index');
My Lambda crashes and complains that the index module does not exist.

When I zip the layer contents and upload them manually to AWS everything works fine.


Things that I have tried:
- Moving serverless.yml inside the nodejs directory and deploying from there, but this was giving me the following error:
No file matches include / exclude patterns

1 Answer 1

2

Finally found out what I was doing wrong. There is nothing wrong with the above configuration, my mistake was on requiring the files from the layers.
Instead of this:
const index = require('/opt/nodejs/index');
I should be doing this:
const index = require('/opt/index');

Sign up to request clarification or add additional context in comments.

1 Comment

Accidentally came across you post after 2 hours of searching with no success. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.