首页 > 解决方案 > AWS:使用“sam invoke local”时未找到层代码

问题描述

我正在开发一个创建两个 lambda 函数的示例 AWS 项目。这些函数共享公共代码,这些代码node_modules已放置在单独的层中(具体来说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工作。但是我不想使用我必须先部署的东西,我想使用最新的本地尚未部署的代码。

标签: javascriptnode.jstypescriptaws-lambdaaws-lambda-layers

解决方案


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

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


推荐阅读