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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With