【发布时间】:2020-04-11 06:17:19
【问题描述】:
这行得通:
sam local invoke -t template.local.yaml -e events/event-timezone.json GetTimezoneFunction
这在尝试使用 Visual Studio Code 进行调试时不起作用:
sam local invoke -t template.local.yaml -e events/event-timezone.json -d 5858 GetTimezoneFunction
不同之处在于,在第二个中,我收到一个错误,即未找到在我的 lambda 函数使用的单独层中定义的 npm 'axios' 模块。
Error: Cannot find module 'axios'
Require stack:
- /var/task/get-timezone.js
- /var/runtime/UserFunction.js
- /var/runtime/index.js
我所能想到的可能是尝试使用调试器,要么图层的资源没有复制到 Docker 运行时环境,要么某些文件搜索路径被破坏。
我有另一个 lambda 函数,它不需要层中的任何东西,无论有没有调试器都可以正常工作。
带有getTimeZone 函数的文件以:
import axios from 'axios';
这是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
这是我的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"
},
...
]
}
到目前为止,在寻找此问题的答案时,我只发现了很多未解决的问题和一些不太相同的问题。
【问题讨论】:
标签: typescript amazon-web-services visual-studio-code aws-lambda vscode-debugger