This works:
sam local invoke -t template.local.yaml -e events/event-timezone.json GetTimezoneFunction
This does not work when trying to debug with Visual Studio Code:
sam local invoke -t template.local.yaml -e events/event-timezone.json -d 5858 GetTimezoneFunction
The difference is that, in the second, I get an error that the npm 'axios' module, which is defined in a separate layer used by my lambda function, is not found.
Error: Cannot find module 'axios'
Require stack:
- /var/task/get-timezone.js
- /var/runtime/UserFunction.js
- /var/runtime/index.js
All I can figure is that maybe by trying to use the debugger either the resources of the layer aren't copied to the Docker runtime environment, or some file search path gets broken.
I have another lambda function which doesn't need anything from the layer, and works fine with or without the debugger.
The file with the getTimeZone
function starts with:
import axios from 'axios';
This is the relevant part of template.local.yaml
:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Sample
Globals:
Function:
Timeout: 30
Resources:
SampleCommonLayer:
Type: AWS::Lambda::LayerVersion
Properties:
CompatibleRuntimes:
- nodejs12.x
Content: dependencies
Description: Sample Common LayerVersion
LayerName: SampleCommonLayer
GetTimezoneFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: dist/get-timezone
Handler: get-timezone.getTimezone
Runtime: nodejs12.x
Layers:
- !Ref SampleCommonLayer
Events:
GetTimezone:
Type: Api
Properties:
Path: /get-timezone
Method: get
Outputs:
GetTimezoneApi:
Description: "API Gateway endpoint URL for Prod stage for getTimezone function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/get-timezone/"
GetTimezoneFunction:
Description: "getTimezone Lambda Function ARN"
Value: !GetAtt GetTimezoneFunction.Arn
GetTimezoneFunctionIamRole:
Description: "Implicit IAM Role created for getTimezone function"
Value: !GetAtt GetTimezoneFunctionRole.Arn
Here's my launch.json
:
{
"version": "0.2.0",
"configurations": [{
"name": "Debug get-timezone",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 5858,
"localRoot": "${workspaceRoot}/dist",
"remoteRoot": "/var/task",
"protocol": "inspector",
"stopOnEntry": false,
"preLaunchTask": "local-tz-debug"
},
...
]
}
So far I've only found a lot of unanswered questions and a few not-quite-the-same problems while searching for an answer to this problem.