【问题标题】:Why does debugging break access to an AWS::Lamba::LayerVersion when using "sam invoke local"?为什么在使用“sam invoke local”时调试会中断对 AWS::Lambda::LayerVersion 的访问?
【发布时间】: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


    【解决方案1】:

    我让我的一个 lambda 函数从process.env 中转储所有环境变量,并发现启用调试(通过-d 5858)和未启用调试之间存在一些差异,最值得注意的是NODE_PATH 是'在启用调试时未定义。

    我想我曾经尝试过修复 NODE_PATH 一次,基于我在其他地方读到的类似问题,但它似乎没有帮助。

    现在我正在转储环境变量,但是,我可以看到我在 env.json 文件中定义 NODE_PATH 的尝试没有任何效果。那时我发现 SAM 忽略了任何未在我的模板中明确定义的环境变量,无论是全局变量还是每个 lambda。

    这解决了这个问题:

    Globals:
      Function:
        Timeout: 30
        Environment:
          Variables:
            NODE_PATH: /opt/nodejs/node12/node_modules:/opt/nodejs/node_modules:/var/runtime/node_modules
    

    一旦我解决了这个问题,然后它发现我需要调整我的 launch.json 以使调试器断点适用于 TypeScript:

    {
      "version": "0.2.0",
      "configurations": [{
          "name": "Debug get-timezone",
          "type": "node",
          "request": "attach",
          "address": "localhost",
          "port": 5858,
          "localRoot": "${workspaceRoot}/dist/get-timezone", // <- More specific path needed
          "remoteRoot": "/var/task",
          "protocol": "inspector",
          "stopOnEntry": false,
          "preLaunchTask": "local-tz-debug",
          "outFiles": [ // <- Added this
            "${workspaceRoot}/dist/get-timezone/**/*.js"
          ],
          "sourceMaps": true // <- Added this
        },
        ...
      ]
    }
    

    我只想要为本地测试而不是部署定义的 NODE_PATH 变量,所以我有一个单独的 template.yaml 文件,没有该定义用于部署。 template.yaml 还将LayerVersionContent 定义为nodejs.zip,而不是dependencies。我从template.yaml 自动生成template.local.yaml,这样我就不必维护两个几乎相同的独立模板。

    我希望将来有更好的方法来做到这一点,但现在它可以完成工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 2022-12-08
      • 2020-04-07
      • 2020-02-06
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      相关资源
      最近更新 更多