我在python3.6中有一个Lambda函数,它使用以下包:
opencv-python
imutils
numpy
joblib
mahotas
scikit-image
scikit-learn==0.22.1
sklearn
pymongo==3.10.1我正在使用Serverless框架来最小化部署规模,并部署到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选项,因为否则部署包将太大(~350 be )。
由于某些原因,如果我不将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函数时,会得到一个错误:
无法导入模块“处理程序”的/tmp/sls-py-req/cv2/cv2.cpython-36m-x86_64-linux-gnu.so:
加载命令地址/偏移量未正确对齐
我认为这可能是一个无服务器的框架问题。我能做些什么来解决这个问题?我尝试过安装不同版本的pymongo,但没有成功。
我使用的是以下Serverless Framework版本:
> serverless --version
Framework Core: 1.73.1
Plugin: 3.6.13
SDK: 2.3.1
Components: 2.31.2编辑:除了pymongo,还有其他的选择吗?我见过一些人,但他们使用pymongo作为潜在的依赖关系。
发布于 2020-06-24 21:14:53
我无法用无服务器解决这个问题。因此,我决定不使用sls deploy而不使用pymongo,一旦没有服务器生成.requirements.zip文件,我就将该文件复制到其他地方,并再次运行sls deploy,但这次只在requirements.txt中使用pymongo (和pymongo[srv])。生成包含pymongo及其依赖项的.requirements.zip。我合并了来自这个.requirments.zip的文件和从第一个sls deploy生成的一个requirements.zip文件。这样,我就可以在一个opencv2文件中获得所有其他依赖项(opencv2、numpy、joblib等)和pymongo。
之后,我将源代码、合并的.requirements.zip文件和手动压缩,将zip上传到s3。它压缩到128 It。指向我的lambda函数使用来自S3的这个部署包,它就成功了。我得到了pymongo与opencv2和其他依赖项。
但是,一个缺点是,您必须上传到S3并自己更新函数。在问题解决之前,我将不得不使用这个“黑客”。
发布于 2020-12-15 08:11:46
使用图层。单独包装功能。
lambda:
handler: lambda/handler.lambda_handler
runtime: python3.8
layers:
- {Ref: PythonRequirementsLambdaLayer}
package:
individually: true
...
pythonRequirements:
dockerizePip: non-linux
dockerImage: lambci/lambda:build-python3.8
layer: true
slim: true
slimPatterns:
# the commented ones are included in slim: true
# - '**/*.py[c|o]'
# - '**/__pycache__*'
# - '**/*.dist-info*'
- '**/*.egg-info*'
- '**/test/*'
- '**/tests/*'
invalidateCaches: truehttps://stackoverflow.com/questions/62560559
复制相似问题