【问题标题】:Dependencies Missing while deploying an AWS Lambda using Serverless Framework使用无服务器框架部署 AWS Lambda 时缺少依赖项
【发布时间】:2021-02-27 11:38:02
【问题描述】:

我正在尝试使用无服务器框架将预测 Lambda 处理程序部署到 AWS,我的代码依赖项是 pytorch 和 transformers(HF)。

我正面临这个问题,不知道如何克服,

我已经正确配置了 requirements.txt 文件,它有以下两行,

transformers==2.10
https://download.pytorch.org/whl/cpu/torch-1.5.0%2Bcpu-cp38-cp38-linux_x86_64.whl

当我尝试使用“无服务器部署”命令时,有时它会创建一个包,其中包含包内 requirements.zip 中的所有依赖项,大约 144 MB,这是预期的结果。

日志供您参考

C:\Users\Rajeshwaranp\serverless-HFT>sls deploy
Serverless: Deprecation warning: Starting with next major version, API Gateway naming will be changed from "{stage}-{service}" to "{service}-{stage}".
            Set "provider.apiGateway.shouldStartNameWithService" to "true" to adapt to the new behavior now.
            More Info: https://www.serverless.com/framework/docs/deprecations/#AWS_API_GATEWAY_NAME_STARTING_WITH_SERVICE
Serverless: Adding Python requirements helper...
Serverless: Generated requirements from C:\Users\Rajeshwaranp\serverless-HFT\requirements.txt in C:\Users\Rajeshwaranp\serverless-HFT\.serverless\requirements.txt...
Serverless: Installing requirements from C:\Users\Rajeshwaranp\serverless-HFT\cache\72130b8b1c12a9cc09719c938b787b829ceb78a73eeafa251ccf974d7dafec58_slspyc\requirements.txt ...
Serverless: Docker Image: lambci/lambda:build-python3.8
Serverless: Using download cache directory C:\Users\Rajeshwaranp\serverless-HFT\cache\downloadCacheslspyc
Serverless: Running docker run --rm -v C\:/Users/Rajeshwaranp/serverless-HFT/cache/72130b8b1c12a9cc09719c938b787b829ceb78a73eeafa251ccf974d7dafec58_slspyc\:/var/task\:z -v C\:/Users/Rajeshwaranp/serverless-HFT/cache/downloadCacheslspyc\:/var/useDownloadCache\:z -u 0 lambci/lambda\:build-python3.8 python -m pip install -t /var/task/ -r /var/task/requirements.txt --cache-dir /var/useDownloadCache...
Serverless: Zipping required Python packages...
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Removing Python requirements helper...
Serverless: Injecting required Python packages to package...
Serverless: WARNING: Function predict_answer has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 30 seconds.
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
........
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service serverless-hft.zip file to S3 (144.02 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.................................
Serverless: Stack update finished...

但有时,当我使用相同的命令进行部署时,它会创建一个 128KB(大约)的包,而包内的 requirements.zip 文件中没有任何依赖项。

日志供您参考,

C:\Users\Rajeshwaranp\serverless-HFT>sls deploy
Serverless: Deprecation warning: Starting with next major version, API Gateway naming will be changed from "{stage}-{service}" to "{service}-{stage}".
            Set "provider.apiGateway.shouldStartNameWithService" to "true" to adapt to the new behavior now.
            More Info: https://www.serverless.com/framework/docs/deprecations/#AWS_API_GATEWAY_NAME_STARTING_WITH_SERVICE
Serverless: Adding Python requirements helper...
Serverless: Generated requirements from C:\Users\Rajeshwaranp\serverless-HFT\requirements.txt in C:\Users\Rajeshwaranp\serverless-HFT\.serverless\requirements.txt...
Serverless: Using static cache of requirements found at C:\Users\Rajeshwaranp\serverless-HFT\cache\72130b8b1c12a9cc09719c938b787b829ceb78a73eeafa251ccf974d7dafec58_slspyc ...
Serverless: Zipping required Python packages...
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Removing Python requirements helper...
Serverless: Injecting required Python packages to package...
Serverless: WARNING: Function predict_answer has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 30 seconds.
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service serverless-hft.zip file to S3 (124.15 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...

那么为什么会这样呢?以及如何调整部署命令以便始终包含依赖项?

【问题讨论】:

  • 您是否使用 lambda 层来包含您的依赖项?
  • 不,@SridharRaju,我这里没有使用图层。
  • 为了打包依赖,我们将使用 lambda 层并提供这些层的功能。我会添加一个答案供您参考。
  • 感谢您的回复 Sridhar,我正在使用 Lambda 层,但我仍然想了解导致这种奇怪行为的原因,即有时它会打包有时会留下依赖关系。
  • 您可以将无服务器文件添加到问题中吗?

标签: amazon-web-services aws-lambda serverless-framework serverless aws-serverless


【解决方案1】:

使用 lambda 层打包您的所有需求,确保您在 requirements.txt 文件中包含所有需求。

这仅在插件部分中列出 serverless-python-requirements 插件时有效。 用这个替换您的自定义键,并为需要这些要求的函数提供使用该层的参考。

plugins:
  - serverless-python-requirements
custom:
  pythonRequirements:
    layer: true
functions:
  auth:
    handler: data.auth
    layers:
      - { Ref: PythonRequirementsLambdaLayer}

【讨论】:

    猜你喜欢
    • 2021-03-07
    • 2020-10-08
    • 2021-08-23
    • 1970-01-01
    • 2021-01-07
    • 2021-10-07
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多