Skip to content

Instantly share code, notes, and snippets.

@6d61726b760a
Last active April 30, 2019 04:28
Show Gist options
  • Save 6d61726b760a/7fbb25692dbb1e972bd05652b04d9a11 to your computer and use it in GitHub Desktop.
Save 6d61726b760a/7fbb25692dbb1e972bd05652b04d9a11 to your computer and use it in GitHub Desktop.
lambda function to sftp files from s3 to anywhere else
import boto3
import os
import paramiko
#
# use a trigger to copy files from s3 to an on premise server
#
# TODO:
# configuration using lambda environment vars
#
def worker_handler(event, context):
sourceKey = event['Records'][0]['s3']['object']['key']
sourceFile = os.path.basename(sourceKey)
print("sourceKey = " + sourceKey + "\nsourceFile = " + sourceFile)
s3_client = boto3.client('s3')
#s3_client.download_file('BUCKET_NAME','PATH/TO/YOUR/SOURCE/FILE','/PATH/TO/DESTINATION/FILE')
s3_client.download_file('my-magic-bucket','lambda-sftp/req/sshkey.pem','/tmp/sshkey.pem')
s3_client.download_file('my-magic-bucket',sourceKey,'/tmp/' + sourceFile)
k = paramiko.RSAKey.from_private_key_file("/tmp/sshkey.pem")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host='111.222.333.444'
print("Connecting to " + host)
c.connect( hostname = host, username = "myusername", pkey = k )
print("Connected to " + host)
ftp_client=c.open_sftp()
print("uploading " + sourceFile)
# ftp_client.put('/path/to/source/file','/path/to/destination/file/on/remote/server')
ftp_client.put('/tmp/' + sourceFile,'/home/myusername/' + sourceFile)
print("uploaded " + sourceFile)
ftp_client.close()
return
{
'message' : "Script execution completed. See Cloudwatch logs for complete output"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment