Lambda: SFTP -> S3 python script, INCREDIBLY poor network performance

Hey,

I wrote a script in Python to grab files from an SFTP server off-prem and download them into memory, then dump to s3.

However, I am getting absolutely abysmal network speeds. It’s taking almost 4 minutes to download a 70MB file. The exact calculation for network speed when I do the math is ~2.5mbps.

That’s like what my DSL was in college 20 years ago.

I’ve verified it’s not an SFTP server issue-- at my office I’m able to snatch it using the same exact code in seconds.

I have a number of files and some are like 30MB, so this approach which I thought would be a slam dunk is not looking so great anymore. Anybody ever run in to this, or know how to increased network speed?

Thanks.

Solved:

It’s not a lambda issue but a paramiko issue.

If you want to download to a file buffer in memory which is preferable for lambda, use getfo instead of file/read. See my comment below for code. After doing that things work incredibly well.

Lambda CPU performance is tied to the memory that you allocate to the function,

I have no clue if it’s the same for network performance, but it’s worth trying, can you resize your function memory and try again?

Interesting. I had the memory and time limit both pegged out (3000mb and 15 minutes respectively). Do you mean it’s inversely proportional? Like lowering memory will make it faster?

I was having an issue transferring about 50 gigs. I used the aws cli s3 cp command to go to s3

I then copied from s3 to the EC2 instance. For the EC2 to S3 I ran across some acceleration parameters. (posted latter) I went from S3 to EC2 in about 15 - 20 minutes. Home to S3 was several hours. Yea this was within AWS but it was much much faster.

https://docs.aws.amazon.com/cli/latest/topic/s3-config.html

About 1/2 way down are the parameters. I did not have an acceleration endpoint but it still worked, so delete that line. I also had some issues with what file to put this in .aws\credentials or .aws\config

More memory → more speed

I provisioned a c5n.metal instance and used python requests to run a speed test:

128Mb → 100Mbit/s

256Mb → 300Mbit/s

1.5Gb → 650Mbit/s

so give your lambda more juice and get better downloading speed.

I actually installed and uploaded Speedtest-cli to verify your exact findings.

I also figured out the problem. It was systemic from running in Lambda but also as part of the paramiko library defaulting to reading files in 32k chunks which is incredibly inefficient using file() and read().

When I switched to using BytesIO() and paramiko’s

getfo()

things started to really blaze. getfo stands for get file object.

In essence if anybody else comes looking this is a paramiko issue, and don’t use file but instead getfo like this:

from io import BytesIO

fileob = BytesIO()
sftp.getfo(remotePath, fileob)
# must do this or it won’t work
fileob.seek(0)
obj = s3.Object(bucket, key)
obj.put(Body=fileob)

Hi I know its a very long time since this post.
But just wanted to know if you managed failures/retries in this script ? Could you share it?/

If I were to manage the failures and read once only what would be the best approach, do I use services like SQS to keep track etc?