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

7
我正在处理一个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

...to...

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

如果使用已部署层(其中xxxxxxxxxxxxy是特定的ID和版本),那么sam local invoke可以工作。但我不想使用需要先部署的东西,我想使用最新的本地未部署的代码。


我遇到了完全相同的问题!你找到解决问题的方法了吗?我浪费了一整天的时间来弄清楚我做错了什么。我认为这是SAM CLI存在的问题,所以我在GitHub上开了一个问题(https://github.com/awslabs/aws-sam-cli/issues/2016)。 - Shamshiel
很遗憾,我还没有在AWS上工作过一段时间,自从我第一次发布这个问题以来。但是当我回到做更多的AWS工作时,我真的希望有人已经解决了这个问题。 - kshetline
2
AWS现在还没有解决这个问题已经快两年了,真的非常令人沮丧。 - Renato Byrro
1个回答

4

我已经好几个月没和遇到这个问题的那个项目打交道了,所以现在细节有点模糊。这个解决方法能够解决 sam-local-invoke 的问题,但会影响部署吗?我现在记不清使用 .zip 文件部署一个层是强制性还是可选的了。 - kshetline
2
据我所知,图层是在zip文件中还是在目录中并没有什么区别。我刚试了一下,它的效果和我的zip文件一样。 - Shamshiel
是的,将图层的“ContentUri”参数指向一个目录并不能解决这个问题... - Renato Byrro
请注意,对于Java而言,ContentUri 必须是一个zip文件。我使用了一个目录,但Java不支持它。在此处查看工作示例:https://github.com/awsdocs/aws-lambda-developer-guide/blob/main/sample-apps/s3-java/template.yml#L43 - stwr667

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接