我在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
when的输出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函数时,出现错误:
无法导入模块“处理程序”:/tmp/sls-py-req/cv2/cv2.cpython-36m-x86_64-linux-gnu.so:ELF加载命令地址/偏移未正确对齐
我认为这可能是无服务器框架问题。我该怎么做才能解决此问题?我尝试安装其他版本,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
。这样,我将所有其他依赖项(opencv2,numpy,joblib等)和pymongo放在一个.requirements.zip
文件中。
之后,我压缩了源代码和合并的.requirements.zip
文件,然后将zip手动上传到s3。它压缩到128MB。指出了我的lambda函数以使用S3中的此部署程序包,并且该方法有效。我得到了pymongo以及opencv2和其他依赖项。
但是,缺点是您必须上传到S3并自己更新功能。在解决此问题之前,我将不得不使用此“ hack”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句