Skip to content

Instantly share code, notes, and snippets.

@ijin
Created February 12, 2016 18:00
Show Gist options
  • Save ijin/26afca1e9b03ecaf4d8e to your computer and use it in GitHub Desktop.
Save ijin/26afca1e9b03ecaf4d8e to your computer and use it in GitHub Desktop.
ssh from lambda using keys fetched from KMS-SSE backed s3
from __future__ import print_function
from botocore.client import Config
import boto3
import paramiko
print('Loading function')
s3 = boto3.client('s3', config=Config(signature_version='s3v4'))
def lambda_handler(event, context):
s3.download_file('cs-test-data', 'id_rsa', '/tmp/id_rsa')
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host='ec2-52-192-221-60.ap-northeast-1.compute.amazonaws.com'
client.connect(host, username='ec2-user', key_filename='/tmp/id_rsa')
stdin, stdout, stderr = client.exec_command('uptime')
error = stderr.read()
output = stdout.read()
print("err:" + error)
print("out:" + output)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment