question

BhattacharjeePratyay-6256 avatar image
0 Votes"
BhattacharjeePratyay-6256 asked BillBell-6414 commented

File transfer with custom sftp connector using Azure Function

Hi,

We are trying to sftp files using the SftpClient class from the Renci.SshNet package in an Azure function but frequently it throws an error as - Error System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
We have already whitelisted all the IP ranges for the Function App where the Azure Function is hosted for the specific region, on the destination server.
Also, sometimes the sftp connection is successful as well but most of the times the connection fails with the above error.
Can you please advise what could be the issue here.
Is it possible that the IP for the function app might go beyond the list of IP ranges provided by Microsoft for the specific region, due to which the firewall is blocking the connection in the destination server.

Thanks!

azure-functions
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BhattacharjeePratyay-6256 avatar image
1 Vote"
BhattacharjeePratyay-6256 answered BillBell-6414 commented

I was able to find the additional Outbound IP addresses for the function app and got them whitelisted on the destination server. After that the SFTP connectivity is working fine.

Just for reference, I was able to locate the additional Outbound IP addresses (which were not listed in the microsoft documentation of IP ranges by regions) under Function App --> Settings --> Networking (preview) --> Outbound Traffic (Outbound addresses).

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for sharing. Very good.

0 Votes 0 ·
MayankBargali-MSFT avatar image
0 Votes"
MayankBargali-MSFT answered BhattacharjeePratyay-6256 commented

Hi @BhattacharjeePratyay-6256

Welcome to Microsoft Q&A! Thanks for posting the question.

As per the error this is more towards the socket exception at the client/server or in the network in between.
If you are running your function in consumption plan then there are limited number of sockets available for your application to communicate. If you are in webapp plan you can refer to the limit here.

As you have mentioned that you are using the SFTP connector so there would be outbound calls that uses socket to communicate. I will suggest you to review your function code and verify that you are reusing the object/connection so the communication happens through the same socket rather than creating the new one which could cause the socket exception at the function app end.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @MayankBargali-MSFT for your response.
As you suggested, we tried to use a static sftp object/connection so the communication happens through the same socket rather than creating the new one but still it failed with the same error.
We have a feeling that the the outbound IP ranges for this function app for Canada Central region provided by Microsoft is not the exhaustive list.
However, there is no way for us to check the outbound IP of the function except checking through the server logs at the destination server.
Please advise if there is any other solution to fix this. Thanks!

0 Votes 0 ·
MayankBargali-MSFT avatar image MayankBargali-MSFT BhattacharjeePratyay-6256 ·

@BhattacharjeePratyay-6256 Can you provide more information on my private comment so I can verify it at my end.

0 Votes 0 ·
BillBell-6414 avatar image
0 Votes"
BillBell-6414 answered BillBell-6414 commented

I would recommend trying WinSCP. Install app on PC and make you can sftp from desktop. Install WinSCP Nuget package in VS. Then make sure you can ftp from Azure function when you run it on your PC. When you run in Azure I had an issue with executable path. Look at https://docs.microsoft.com/en-us/answers/questions/442547/newbie-getting-winscp-working.html

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Agree, that if connection works once then it should work all the time. I've never used Renci.SshNet package. My go to FTP program is WinSCP. It is the most reliable.

1 Vote 1 ·

Thanks @BillBell-6414 for your response.
However, in my case I won't be able to connect to the destination server from my PC using WinSCP, since the destination server team did not agree to whitelist my local IP to the destination server due to security reasons.
Also, in my case the sftp connection gets established sometimes but fails most of the times. If it were an issue with the executable path in Azure, would the connection get established even once in the first place? Please advise.

0 Votes 0 ·