Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS lambda cannot import name '_bcrypt'

I have written a simple python script which will ssh to a EC2 instance and run a script in that instance.

I use paramiko library to do ssh connect.Below is my code snippet.

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'

In my local setup where the python version is 3.6.2 it is working fine. But when i upload it along with all the dependent libraries in AWS lambda and run, it gives me the below error.

cannot import name '_bcrypt'

I have verified that i have the bcrypt folder in the uploaded zip.

like image 301
geekops Avatar asked Mar 08 '23 12:03

geekops


1 Answers

I'm guessing your local PC is not a linux PC.

You need to build your deployment package on a linux PC. Lambda underneath runs AMI images, which is based on linux.

I have documented this on my own blog here

like image 81
Dayo Avatar answered Mar 15 '23 12:03

Dayo