[我是Python 2.7和AWS Lambda的新手,欢迎任何帮助]
我按照AWS Lambda教程创建了一个虚拟环境,其中包含与使用paramiko将文件复制到SFTP服务器相关的Python库,作为AWS Lambda上的计划任务,以运行以下脚本:
import paramiko
def worker_handler(event, context):
host = "sftpserver.testdpom.com"
port = 22
transport = paramiko.Transport((host, port))
sftp = paramiko.SFTPClient.from_transport(transport)
username = "xxxx"
password = "xxxxxx"
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put("test.txt", "test.txt")
sftp.close()
transport.close()
return
{
'message' : "Script execution completed. See Cloudwatch logs for complete output"
}
python脚本在我的本地机器上正常工作,但当我在AWS Lambda上测试包时,我得到错误"ImportError: No module named _constant_time"和下面的堆栈跟踪。
您能想到AWS Lambda环境中出现此错误的任何可能原因吗?
File "/var/task/paramiko/kex_group1.py", line 111, in _parse_kexdh_reply
self.transport._verify_key(host_key, sig)
File "/var/task/paramiko/transport.py", line 1617, in _verify_key
key = self._key_info[self.host_key_type](Message(host_key))
File "/var/task/paramiko/rsakey.py", line 58, in __init__
).public_key(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 2235, 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: No module named _constant_time
由于lambda在amazon linux实例上运行,因此您基本上需要:
- 启动amazon linux ec2实例
- 创建一个虚拟环境和
pip install
你需要的所有包 -
scp
将文件保存到本地部署包所在的位置
这一切都是由于pip install
如何根据你是在linux上还是在mac上做不同的事情而发生的(我假设windows也是如此)。
下面是一个启动脚本,可以让ec2实例加快速度。#!/bin/bash
sudo yum upgrade -y
sudo yum group install -y "Development tools"
sudo yum install -y
python27
libffi libffi-devel
openssl openssl-devel
virtualenv venv
source venv/bin/activate
pip install paramiko
#!/bin/bash
sudo yum upgrade -y
sudo yum group install -y "Development tools"
sudo yum install -y
python27
libffi libffi-devel
openssl openssl-devel
virtualenv venv
source venv/bin/activate
pip install paramiko
paramiko
包将在/path/to/venv/lib/python2.7/site-packages/paramiko
中,cryptography
包将在path/to/venv/lib64/python2.7/cryptography
中。
我一直在我的本地mac上使用pip install
的组合,并在包不起作用时这样做(如paramiko
和psycopg2
),并且还有一些其他有用的包,人们已经预编译并将其放在github上其他地方专门用于lambda。
HTH !