【问题标题】:How to actually step by step debug in VSCode AWS Sam Local Lambda Functions如何在 VSCode AWS Sam 本地 Lambda 函数中实际逐步调试
【发布时间】:2021-06-16 02:19:40
【问题描述】:

大家好,这里有人成功断点了使用 Visual Studio 代码和 nodejs8.10 在本地运行的 lambda 函数吗?我想知道是不是因为我的项目是打字稿。 我已经关注https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging-nodejs.html 无济于事。

函数运行,如果我在某个文件上执行stopOnEntry: true vscode 停止,但不在我实际函数的断点上。

输出:

$ sam local invoke ConsumeSQSFunction --no-event --region us-west-2 -d 5858
2019-04-30 11:19:16 Found credentials in shared credentials file: ~/.aws/credentials
2019-04-30 11:19:16 Invoking index.processPublisherServicesQueue (nodejs8.10)

Fetching lambci/lambda:nodejs8.10 Docker container image......
2019-04-30 11:19:17 Mounting /home/dev/Documents/xxxx/main-dir/dist/lambda-section/consume-sqs as /var/task:ro,delegated inside runtime container
Debugger listening on ws://0.0.0.0:5858/74f34edb-cdcd-4da0-82c0-950f5d809fd9
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
START RequestId: 1a4198ca-cceb-1b38-f251-386a239dad19 Version: $LATEST
2019-04-30T18:19:19.800Z        1a4198ca-cceb-1b38-f251-386a239dad19    Hello World!
2019-04-30T18:19:19.800Z        1a4198ca-cceb-1b38-f251-386a239dad19    Processing queue {} [] undefined
END RequestId: 1a4198ca-cceb-1b38-f251-386a239dad19
REPORT RequestId: 1a4198ca-cceb-1b38-f251-386a239dad19  Duration: 199.46 ms     Billed Duration: 200 ms Memory Size: 128 MB     Max Memory Used: 45 MB

{"statusCode":200,"body":"{\"message\":\"hello world\"}"}
Waiting for the debugger to disconnect...

项目结构:

launch.json
/main-dir
   /app
        /lambda-section (where I did sam init)
               /ConsumeSQS
                   index.ts
              template.yaml
              event.json
        /etc
   /dist
       /lambda-section
         /ConsumeSQS
            index.js
            index.js.map

template.yaml 中的相关部分

Resources:
  ConsumeSQSFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      FunctionName: ConsumeSQS
      Description: 'Consumes messages from SQS queue'
      CodeUri: ../../dist/lambda-section/consume-sqs/
      Handler: index.processPublisherServicesQueue
      Runtime: nodejs8.10

launch.json:

,
        {
            "name": "Attach to SAM CLI",
            "type": "node",
            "request": "attach",
            "address": "localhost",
            "port": 5858,
            // From the sam init example, it would be "${workspaceRoot}/hello_world"
            "localRoot": "${workspaceRoot}/main-dir/app/lambda-section",
            "remoteRoot": "/var/task",
            "protocol": "inspector",
            "stopOnEntry": false
        }

【问题讨论】:

    标签: aws-lambda aws-sam aws-sam-cli


    【解决方案1】:

    我认为问题在于您没有配置源映射。

    当您运行 SAM 时,实际调用的代码是 dist/lambda-section/ConsumeSQS/index.js,甚至需要通过包装器(这是您在 stopOnEntry 时看到的)。

    我建议您将"sourceMaps": true 添加到您的launch.json。如果这很好用,但您可能还需要使用 sourceMapPathOverrides 键。

    如果您检查 dist/.../index.js 和匹配映射的内容,您应该能够看到这些文件中引用的源映射路径与实际打字稿文件的路径之间的差异。然后,您可以相应地以地图的形式提供覆盖,例如:

    "sourceMapPathOverrides": {
      "file:///lambda-section/ConsumeSQS/*": "${workspaceRoot}/main-dir/app/lambda-section/*"
    }
    

    (n.b. 有关这些属性的更多信息,请参阅VSCode documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 2020-11-06
      • 2020-05-27
      • 2021-05-26
      • 2016-02-26
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多