How to send file using SFTP to another Host from AWS

Fri, Mar 18, 2022

Read in 2 minutes

In real-time , you may need put files into third-party systems. In this article , we are going explore how to transfer files from AWS AC to Third party host using SFTP .

1. Introduction:

In real-time , you may need put file into third-party systems. In this article , we are going explore how to transfer files from AWS AC to Third party host using SFTP.

2.Different ways of transferring files :

SFTP - ssh file transfer protocol FTP - File Transfer Protocol FTPS - secure file transfer protocol SCP - Secure Copy protocol HTTP and HTTPS - Hyper Text Transfer Protocol AS2, AS3, and AS4 PeSIT - end to end file transfer protocol

3.How to transfer a file from your AWS Ac to another third party host :

Here we have the files to be transferred are in s3 and using a lambda we will get the file and put into third party system.

I am using python and python “pysftp” module to make SFTP transfer.


Import json
Import boto3
Import pystp

Def lamda_handler(event,context)

S3=boto3.client('s3')

Try:
cnopts = pysftp.CnOpts()
Cnopts.hostkeys = None

# this is to make connection with host
Conn = pysftp.Connection(hostname='hostname',username='username',passsword='password',port=2222,cnopts=cnopts)
Print("Connection established successfully")

# To download file from s3 bucket

File = s3.download_file("bucketname","fileName","/tmp/filename.xml)
Conn.put("/tmp/filename.xml")

Except Exception as e:
Print( e)

Conn.close( )

Return { }

4.Running the Script as Lambda ::

You have to create a POJO class for the table.

Copy this script in a lambda and lambda should have permission to access the s3 buckets .

6. Get all python packages for Lambda Layer :

https://github.com/keithrozario/Klayers

Please follow the readme and set the layer using arn.