我在python3.6中有一个Lambda函数,它使用以下软件包:
opencv-python
imutils
numpy
joblib
mahotas
scikit-image
scikit-learn==0.22.1
sklearn
pymongo==3.10.1
我正在使用无服务器框架来最小化部署规模并部署到lambda。我已经使用serverless-python-requirements
插件来管理软件包。这是我的template.yml文件的样子:functions:
hello:
handler: handler.hello
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: non-linux
zip: true
slim: true
noDeploy:
- boto3
- botocore
- docutils
- jmespath
- pip
- python-dateutil
- s3transfer
- setuptools
- six
- tensorboard
package:
exclude:
- node_modules/**
- model/**
- .vscode/**
我需要使用slim
和zip
选项,因为否则部署包将太大(〜350mb)。由于某种原因,如果我不将
pymongo
包括在requirements.txt
中,则该函数运行良好。不包括sls deploy
时的pymongo
输出为:Serverless: Adding Python requirements helper...
Serverless: Generated requirements from /home/amman/Desktop/serverless-hello-world/requirements.txt in /home/amman/Desktop/serverless-hello-world/.serverless/requirements.txt...
Serverless: Using static cache of requirements found at /home/amman/.cache/serverless-python-requirements/3967fa669ece2345132bfe2a31be4287e2d61deedfb8b6006997a2192cea5753_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: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service hello-world.zip file to S3 (128.52 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.........
Serverless: Stack update finished...
因此,.zip的总大小为〜128 MB,并且该函数运行良好。但是,如果我包括pymongo
,sls deploy
的输出为:Serverless: Adding Python requirements helper...
Serverless: Generated requirements from /home/amman/Desktop/serverless-hello-world/requirements.txt in /home/amman/Desktop/serverless-hello-world/.serverless/requirements.txt...
Serverless: Installing requirements from /home/amman/.cache/serverless-python-requirements/279b0240a975ac6ad3c96e3b0ed81eec7981a8e66e0216037484878bfcaf4479_slspyc/requirements.txt ...
Serverless: Using download cache directory /home/amman/.cache/serverless-python-requirements/downloadCacheslspyc
Serverless: Running ...
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: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service hello-world.zip file to S3 (109.37 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.........
Serverless: Stack update finished...
现在,大小已减少至〜109 MB。我是否添加了新的依赖关系,大小是否应该增加?当我运行lambda函数时,出现错误:我认为这可能是无服务器框架问题。我该怎么做才能解决此问题?我曾尝试安装不同版本的
pymongo
,但没有运气。我正在使用以下Serverless Framework版本:
> serverless --version
Framework Core: 1.73.1
Plugin: 3.6.13
SDK: 2.3.1
Components: 2.31.2
编辑:pymongo有替代品吗?我见过一些,但他们使用pymongo作为基础依赖。 最佳答案
我无法使用无服务器解决此问题。因此,我决定不使用sls deploy
来使用pymongo
,一旦无服务器生成.requirements.zip
文件,我将该文件复制到其他位置,然后再次运行sls deploy
,但这一次在pymongo
中仅使用pymongo[srv]
(和requirements.txt
)。生成的.requirements.zip
包含pymongo及其依赖项。我合并了这个.requirments.zip
和第一个requirements.zip
生成的一个sls deploy
的文件。这样,我在一个.requirements.zip
文件中获得了所有其他依赖项(opencv2,numpy,joblib等)和pymongo。
之后,我压缩了源代码以及合并的.requirements.zip
文件,然后手动压缩了,将zip上传到s3。它压缩到128MB。指出了我的lambda函数以使用S3中的此部署程序包,并且该方法有效。我得到了pymongo以及opencv2和其他依赖项。
但是,缺点是您必须上传到S3并自己更新功能。在解决此问题之前,我将不得不使用此“hack”。