Skip to content

Layer not working locally due to not supporting zip file #947

Description

@GeekOnGadgets

Description:

I have a lambda layer which takes care of common code and node_modules, which works perfectly fine when deployed. But for local development its not working. Not sure if, I am doing something wrong or this feature is not supported yet.

Below is dir structure

-src/
--test
---index.js
--shared
---nodejs
----package.json
----response.js       
-template.yaml
-.gitignore

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Globals:
  Function:
    Runtime: nodejs8.10

Parameters:
  StageName:
    Type: String

Resources:
  TestApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev

  #Lambda Layer for sharing code
  TestLambdaLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: test-layer
      Description: Lambda layer with all node module dependency and shared code between lambda
      ContentUri: src/shared/nodejs.zip
      CompatibleRuntimes:
        - nodejs8.10
      RetentionPolicy: Retain

  Test:
    Type: 'AWS::Serverless::Function'
    Properties:
      Layers:
        - !Ref TestLambdaLayer
      FunctionName: test
      CodeUri: src/test
      Handler: index.test
      Events:
        EndPoint:
          Type: Api
          Properties:
            RestApiId: !Ref TestApi
            Path: /test
            Method: get

test api

const sharedPath = process.env.LAYER_PATH || '/opt/nodejs/';

const boom = require('boom');

const { successResponse, errorResponse } = require(sharedPath + 'response');

async function test(event) {
  const data = {
    buildNumber: 1.0
  };
  try {
    return successResponse(data);
  } catch (error) {
    return errorResponse(boom.serverUnavailable());
  }
}

module.exports = {
  test
};

when I run sam local start-api i get Unable to import module 'index': Error . When I put node_module and shared code inside test folder path everything works as expected locally.

How can I make lambda layer work locally without changing the code? Thanks for the help.

P.S: Also if you have some time I have another issue open on StackOverflow
https://stackoverflow.com/questions/54261625/api-gateway-options-method-throwing-403/54262046#54262046

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions