1

I am trying to move file from SFTP location to S3 location by using Paramiko library. The details given for the connection to SFTP is hostname, port, username and key_filename. The private key is a .pem file and I can't figure out a way to give the path to .pem file in key_filename parameter.

Please help!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Just a side note: Shipping a PRIVATE key in a Lambda sounds like a very, very bad idea from a security point of view. Those files should be treated as hyper sensitive. If I had to guess you are probably committing those to your source control etc. when you are building your Lambda. That sounds like a nightmare. I'd rather recommend moving those files directly from the server that is running the FTP server to your S3 bucket or find any other method. And if you have to do something like this make sure that your FTP user is only allowed to copy exactly the files that you need and nothing more. – Jens Mar 04 '21 at 08:36

1 Answers1

0

For referring to files in your Lambda task, see:
AWS Lambda read contents of file in zip uploaded as source code

So this should work:

private_key_path = os.environ['LAMBDA_TASK_ROOT'] + "/key.pem"

Alternatively, you do not have to store the key to a file. You can hard-code it to your Python script to avoid any external dependencies, see:
SSH/SCP through Paramiko with key in string


A related question about pysftp:
How to connect EC2 using pysftp via AWS Lambda without .pem file or alternate to .pem file

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992