javascript - AWS : Layer code not found when using "sam invoke local"

标签 javascript node.js typescript aws-lambda aws-lambda-layers

我正在开发一个创建两个 lambda 函数的示例 AWS 项目。这些函数共享来自 node_modules 的公共(public)代码它已放置在单独的层中(特别是 AWS::Lambda::LayerVersion ,而不是 AWS::Serverless::LayerVersion )。我可以部署此代码,并且在我测试部署的版本时它可以正常工作。

但是,当我尝试使用 sam invoke local 在本地测试代码时,未找到通用代码。我收到此错误(我正在尝试使用 npm 包“axios”):

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'axios'\nRequire stack:\n- /var/task/get-timezone.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js"}



这是我的template.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: nodejs.zip
      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

  ReverseFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: dist/reverse
      Handler: reverse.reverse
      Runtime: nodejs12.x
      Layers:
        - !Ref SampleCommonLayer
      Events:
        Reverse:
          Type: Api
          Properties:
            Path: /reverse
            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

  ReverseApi:
    Description: "API Gateway endpoint URL for Prod stage for reverse function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/reverse/"
  ReverseFunction:
    Description: "reverse Lambda Function ARN"
    Value: !GetAtt ReverseFunction.Arn
  ReverseFunctionIamRole:
    Description: "Implicit IAM Role created for reverse function"
    Value: !GetAtt ReverseFunctionRole.Arn

我正在调用 GetTimezone 函数,如下所示:
sam local invoke --layer-cache-basedir layer-cache --force-image-build \"GetTimezoneFunction\" --event events/event-timezone.json -d 5858
没有任何东西被复制到 layer-cache目录,我确定这是问题的一部分,但我不知道如何解决这个问题。

我已经搜索过这个问题的答案,但到目前为止,我只找到了未回答的问题,或者与我的特定情况不匹配的答案。

大多数有点相关的问题涉及AWS::Serverless::LayerVersion ,而不是 AWS::Lambda::LayerVersion .我试过使用 Serverless相反,但这并没有帮助。

更新:

如果我改变...
      Layers:
        - !Ref SampleCommonLayer

...至...
      Layers:
        - arn:aws:lambda:us-east-2:xxxxxxxxxxxx:layer:SampleCommonLayer:y

...使用已经部署的层(其中 xxxxxxxxxxxxy 是特定的 ID 和版本)然后 sam local invoke作品。但是我不想使用我必须先部署的东西,我想使用最新的本地尚未部署的代码。

最佳答案

这是已知问题:https://github.com/awslabs/aws-sam-cli/issues/947

目前,“解决方法”是使用图层的目录而不是 zip 文件。

关于javascript - AWS : Layer code not found when using "sam invoke local",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59325699/

相关文章:

javascript - 关闭 iframe 窗口时重置 iframe

java - 如何在 AWS Lambda 上运行使用 npm 的 Java 应用程序

node.js - 如何在 google chat/hangouts 上使用 nodejs xmpp 客户端发送 emojis/表情符号

javascript - 在 typedoc 中标记/生成方法链接?

javascript - 查找当前 windows.pathname 是否包含 li a href 的位置

javascript - 如何在reactjs中动态加载组件?

javascript - 为什么 Express (Node.js) 请求对象是只读的?

javascript - Typescript 类中的 jQuery 回调

angularjs - 使用 TypeScript 的 Angular 1.x 中的类级可注入(inject)依赖项?

Javascript 不影响所选类的所有元素