2

I am creating a Python AWS Lambda function that connects to db to extract data as CSV then sftp that CSV into an SFTP server (abc.example.com). I am using pysftp and Paramiko. Looks like pysftp needs a private key file for password less connection to SFTP host. How do I get this private key file?

Do we need to create a public/private key pair (ssh-keygen) at destination SFTP host? And then use the public part of that key within Lambda function?

Thanks

2 Answers 2

0

Yes, if you don't have it already then you have to create keys using ssh-keygen on sftp host and use it.

import pysftp
with pysftp.Connection('hostname', username='me', private_key='/path/to/keyfile') as sftp:
    #
    # ... do sftp operations
    #

Reference: https://pysftp.readthedocs.io/en/release_0.2.8/cookbook.html

Sign up to request clarification or add additional context in comments.

2 Comments

I have generated key pair at SFTP server (abc.example.com , this is an ec2) under the user u1009697. It creates 2 files. id_rsa and id_rsa.pub. Now I took the id_rsa.pub file and used into lambda function - with pysftp.Connection(abc.example.com, username=u1009697, private_key=id_rsa.pub, cnopts=cnopts) as sftp: logger.debug("SFTP object created") but it says SSH Error:not a valid DSA private key file
You have to use id_rsa not id_rsa.pub
0

Just setup a public key authentication the same way you would do it for a normal (GUI/commandline) SFTP or SSH client. There's nothing pysftp/Python/Lambda-specific about that.

There are zillions of guide on the Internet showing how to do that.
For example my article Set up SSH public key authentication.


And then use the private key in your Python/pysftp code:
Connect to SFTP with key file using Python pysftp


As pysftp requires the key in a physical file, what can be complicated to do in AWS Lambda, you can also hard-code the key in the Python code, if you switch to Paramiko:
SSH/SCP through Paramiko with key in string
(see pysftp vs. Paramiko)

3 Comments

Thanks Martin. I have generated key pair at SFTP server (abc.example.com , this is an ec2) under the user u1009697. It creates 2 files. id_rsa and id_rsa.pub. Now I took the id_rsa.pub file and used into lambda function - with pysftp.Connection(abc.example.com, username=u1009697, private_key=id_rsa.pub, cnopts=cnopts) as sftp: logger.debug("SFTP object created") but it says SSH Error:not a valid DSA private key file
1) You should not generate the key pair on the server (though that's not your main problem). 2) The main problem is that the private_key parameter obviously takes the private key (id_rsa), not the public key (id_rsa.pub). 3) The public key has to go to the authorized_keys file. – Please follow the instructions I have linked in my answer. First try to setup the authentication with some GUI/commandline SFTP/SSH client. And only then try to code the client.
Thanks a lot Martin for your guidance, I am all set now !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.