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
sam local is part of SAM CLI, I'll transfer this issue over to the right repo so they can help
I just ran into this myself. The issue appears to be due to the fact that the layer builder does not actually extract out the layer archives as it does the function code. This means that create_tar adds the zipfile to the tar as LAYERNAME, and when _generate_dockerfile adds it to the image the still-compressed zipfile is just dropped into /opt/ as-is.
It appears that the layer CodeUri is expected to be a directory, not a zip file - which is unexpected, since the lambda function CodeUri can be either and is extracted out on demand.
+1
+1
I just run in to this when I tried to debug lambda with layers locally.
It's really disappointing that this is already open for 18 months...
The minimum what should be done asap is to add this issue reference to this page:
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging-python.html
And clearly state that debugging with layers doesn't work locally because of this issue.
It would save me few hours on getting to the bottom of this on my own.
This is extremely frustrating... The only workaround available is manually copying the Layer code into the Function directory, or is there something better? This approach complete defeats the purpose of using a Layer in the first place. I can't undertand why AWS would publish a Layer service with a half-baked developer tool that doesn't work and things stay that way for ~2 years!
Most helpful comment
I just run in to this when I tried to debug lambda with layers locally.
It's really disappointing that this is already open for 18 months...
The minimum what should be done asap is to add this issue reference to this page:
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging-python.html
And clearly state that debugging with layers doesn't work locally because of this issue.
It would save me few hours on getting to the bottom of this on my own.