Lui*_*dez 19 python paramiko amazon-web-services aws-lambda
所以我正在尝试创建一个aws lambda函数,登录实例并做一些事情.并且该脚本在lambda之外工作正常,但是当我使用与此https://aws.amazon.com/blogs/compute/scheduling-ssh-jobs-using-aws-lambda/相同的说明打包它时,它不会工作.它抛出了这个错误.
libffi-72499c49.so.6.0.4: cannot open shared object file: No such file or directory: ImportError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 12, in lambda_handler
key = paramiko.RSAKey.from_private_key(key)
File "/var/task/paramiko/pkey.py", line 217, in from_private_key
key = cls(file_obj=file_obj, password=password)
File "/var/task/paramiko/rsakey.py", line 42, in __init__
self._from_private_key(file_obj, password)
File "/var/task/paramiko/rsakey.py", line 168, in _from_private_key
self._decode_key(data)
File "/var/task/paramiko/rsakey.py", line 173, in _decode_key
data, password=None, backend=default_backend()
File "/var/task/cryptography/hazmat/backends/__init__.py", line 35, in default_backend
_default_backend = MultiBackend(_available_backends())
File "/var/task/cryptography/hazmat/backends/__init__.py", line 22, in _available_backends
"cryptography.backends"
File "/var/task/pkg_resources/__init__.py", line 2236, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/var/task/cryptography/hazmat/backends/openssl/__init__.py", line 7, in <module>
from cryptography.hazmat.backends.openssl.backend import backend
File "/var/task/cryptography/hazmat/backends/openssl/backend.py", line 15, in <module>
from cryptography import utils, x509
File "/var/task/cryptography/x509/__init__.py", line 7, in <module>
from cryptography.x509.base import (
File "/var/task/cryptography/x509/base.py", line 15, in <module>
from cryptography.x509.extensions import Extension, ExtensionType
File "/var/task/cryptography/x509/extensions.py", line 19, in <module>
from cryptography.hazmat.primitives import constant_time, serialization
File "/var/task/cryptography/hazmat/primitives/constant_time.py", line 9, in <module>
from cryptography.hazmat.bindings._constant_time import lib
ImportError: libffi-72499c49.so.6.0.4: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
Gab*_*sek 26
该教程中的zip命令缺少参数.今天我用pysftp遇到了这个问题,pysftp建立在paramiko之上.libffi-72499c49.so.6.0.4在一个隐藏的点目录里面lib64/python2.7/site-packages/.libs_cffi_backend.根据您在virtualenv中压缩依赖项的方式,您可能无意中排除了此目录.
首先,确保在您的Amazon Linux实例上安装了libffi-devel和openssl-devel ,否则加密模块可能无法正确编译.
sudo yum install libffi-devel openssl-devel
Run Code Online (Sandbox Code Playgroud)如果之前没有安装这些软件包,请删除并重建virtualenv.
确保在您使用'.'的网站包时加油.而不是'*',否则您将不会包含隐藏的文件和目录,因为它们的名称以句点开头.
cd path/to/my/helloworld-env/lib/python2.7/site-packages
zip -r9 path/to/zip/worker_function.zip .
cd path/to/my/helloworld-env/lib64/python2.7/site-packages
zip -r9 path/to/zip/worker_function.zip .
Run Code Online (Sandbox Code Playgroud)我的2美分:如果你想在环境中构建和测试你的lambda函数尽可能类似于实际的lambda,但仍然在你的控制之下,我会建议使用LambCI的Docker镜像.它们基于原始lambda文件系统的转储.他们还有特定于构建的变体(标签build-python2.7,build-python3.6对我们来说最有趣).这些图像不是很小 - 超过500mb - 但它们可以让你在建造时避免任何头痛.
与Amazon Linux相比,重要的好处是所有软件包版本都与真正的lambda相同.
以下是我自己建立的方式:
cd PROJECT_DIR
docker run --rm -it -v "$PWD":/var/task lambci/lambda:build-python2.7 bash
### now in docker
mkdir deps
pip install -t deps -r requirements.txt
# now all dependencies for our package are installed to deps/ directory,
# without any garbage like wheel or setuptools - unlike when using virtualenv
zip -r archive.zip MYCODE.py MYMODULE MYMODULE2.py
cd deps
# it's important to use . here, not * - or else some dot-starting directories will be omitted
zip -r ../archive.zip .
exit
### now locally
# just upload archive to lambda, with or without s3
Run Code Online (Sandbox Code Playgroud)
要使用GitLab CI自动化它,只需指示它使用相同的docker镜像并将这些命令放在deploy脚本部分中:
deploy:
stage: deploy
image: lambci/lambda:build-python2.7
script:
- mkdir deps
- pip install -t deps -r requirements.txt
- zip -r archive.zip MYCODE.py MYMODULE MYMODULE2.py
- cd deps && zip -r ../archive.zip . && cd ..
- aws s3 cp archive.zip ${bucket}/${key}
- aws lambda update-function-code --function-name ${func} --s3-bucket ${bucket} --s3-key ${key}
variables:
bucket: ...
key: ...
func: ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9213 次 |
| 最近记录: |