AWS:使用“ sam invoke local”时找不到层代码

时间:2019-12-13 15:40:51

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

我正在研究一个示例AWS项目,该项目创建了两个lambda函数。这些函数共享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起作用。但是我不想使用必须先部署的东西,我想使用最新的尚未部署的本地代码。

1 个答案:

答案 0 :(得分:2)

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

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