我编写了一个简单的 python 脚本,它将通过 ssh 连接到 EC2 实例并在该实例中运行脚本。
我使用 paramiko 库来做 ssh 连接。下面是我的代码片段。
def lambda_handler(event, context):
client = boto3.client('ec2')
s3_client = boto3.client('s3')
# Download private key file from secure S3 bucket
s3_client.download_file('test','test.pem', '/tmp/test.pem')
k = paramiko.RSAKey.from_private_key_file("/tmp/test.pem")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
bastion_host = "xxx.com"
print ("Connecting to " + bastion_host)
c.connect(hostname=bastion_host, username="yyy", key_filename="/tmp/test.pem")
print ("Connected to " + bastion_host)
commands = [
"sh /home/ec2-user/TestDeploy.sh"
]
for command in commands:
print ("Executing {}".format(command))
stdin, stdout, stderr = c.exec_command(command)
print (stdout.read())
print (stderr.read())
return 'Hello from Lambda'
在我的本地设置中,python 版本为 3.6.2,它工作正常。但是当我将它与 AWS lambda 中的所有依赖库一起上传并运行时,它给了我以下错误。
cannot import name '_bcrypt'
我已经验证我在上传的 zip 中有 bcrypt 文件夹。
最佳答案
我猜您的本地 PC 不是 Linux PC。
您需要在 Linux PC 上构建部署包。 Lambda 下面运行 AMI 镜像,它是基于 linux 的。
我已经在我自己的博客上记录了这一点 here
关于python-3.x - AWS lambda 无法导入名称 '_bcrypt',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45318539/