在 Visual Studio Code 中调试 AWS SAM Lambda TypeScript 项目时,可能会发现无法连接调试器。 这可能是因为启动 Lambda 函数时,调试器无法正常启动或连接。
要解决这个问题,可以按照以下步骤操作:
确保已经安装了 AWS Toolkit for Visual Studio Code 扩展。
打开项目根目录下的 launch.json 文件,并确保它采用以下配置:
{
"type": "node",
"request": "attach",
"name": "Attach to SAM Lambda Functions",
"port": 5858,
"address": "localhost",
"localRoot": "${workspaceFolder}/.aws-sam/build",
"remoteRoot": "/var/task",
"protocol": "legacy",
"stopOnEntry": false,
"restart": true
}
请注意,此处“name”为“Attach to SAM Lambda Functions”,“port”为“5858”,“localRoot”位于“${workspaceFolder}/.aws-sam/build”下。
例如,如果您的 Lambda 函数名为“hello-world”,您的 launch.json 文件应如下所示:
{
"version": "0.2.0",
"configurations": [
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "Invoke hello-world with event.json as input",
"invokeTarget": {
"target": "code",
"projectRoot": "${workspaceFolder}/hello-world",
"lambdaHandler": "handler",
"eventFile": "${workspaceFolder}/events/event.json"
}
},
{
"name": "Attach to SAM Lambda Functions",
"request": "attach",
"type": "node",
"address": "localhost",
"port": 5858,
"localRoot": "${workspaceFolder}/.aws-sam/build",
"