我正在处理一个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文件:
我这样调用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替代,但没有帮助。
更新:如果我更改...
但是,当我尝试使用“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
如果使用已部署层(其中xxxxxxxxxxxx
和y
是特定的ID和版本),那么sam local invoke
可以工作。但我不想使用需要先部署的东西,我想使用最新的本地未部署的代码。