Pmits
Pmits

Reputation: 315

How to properly deploy Lambda layers with Serverless framework

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

Upvotes: 1

Views: 2237

Answers (1)

Pmits
Pmits

Reputation: 315

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');

Upvotes: 2

Related Questions