Error when sending file via SFTP using Azure Functions

Henry Chen 20 Reputation points
2023-09-03T07:55:39.7866667+00:00

I have a script that sends a CSV file via SFTP to a folder using Azure functions. Testing the script locally, it works fine and I am able to receive the file, however, when I run the same script on Azure functions, I get this big error message:

Result: Failure Exception: SSHException: Unable to connect to localhost: [Errno 99] Cannot assign requested address Stack: File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 479, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 752, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/SFTPAgentBus/init.py", line 64, in main with pysftp.Connection(host='localhost', port=22, username='', password='', cnopts=connectOptions) as sftp: File "/home/site/wwwroot/.python_packages/lib/site-packages/pysftp/init.py", line 140, in init self._start_transport(host, port) File "/home/site/wwwroot/.python_packages/lib/site-packages/pysftp/init.py", line 176, in _start_transport self._transport = paramiko.Transport((host, port)) File "/home/site/wwwroot/.python_packages/lib/site-packages/paramiko/transport.py", line 448, in init raise SSHException(

Has anyone come across this error message before and if so you did you fix the issue?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RevelinoB 3,675 Reputation points
    2023-09-03T08:40:32.03+00:00

    Hi Henry Chen,

    When I look at your error message, it indicates that there's an issue connecting to localhost (i.e., 127.0.0.1) on port 22 from your Azure Function. When you run your function locally, it's likely connecting to an SFTP server running on your machine, hence the use of localhost. However, once deployed to Azure, localhost refers to the function's own environment/container, and it's very likely there's no SFTP server running there.

    Here's how you can go about resolving this:

    • Determine the Correct Host: You need to ensure that you're connecting to the correct SFTP host. If the SFTP server is outside the Azure environment, you'll need its actual address rather than localhost.
    • Network Restrictions: Ensure that there are no network restrictions preventing the Azure Function from connecting to the desired SFTP server. This might involve setting up network rules or firewalls to allow traffic from your Azure Function's IP.
    • Use App Settings: Instead of hardcoding connection information (like host, port, username, password), use Azure Function App Settings. This makes your function more flexible and allows you to change the connection details without altering the code. For example:

    host = os.environ.get("SFTP_HOST")

    port = int(os.environ.get("SFTP_PORT", 22))

    username = os.environ.get("SFTP_USERNAME")

    password = os.environ.get("SFTP_PASSWORD")

    • Check Dependencies: Ensure that all necessary packages (pysftp, paramiko, etc.) are correctly installed and referenced in your requirements.txt file for the Azure Function.
    • Logging: Improve logging in your function. This helps in debugging and understanding any issues that arise. For instance:
    try:
        with pysftp.Connection(host=host, port=port, username=username, password=password, cnopts=connectOptions) as sftp:
            # your code
    except Exception as e:
        logging.error(f"Error connecting to SFTP: {e}")
    
    
    

    Consider Managed Solutions: If you're transferring data between Azure services and external services, consider using Azure's managed solutions like Azure Logic Apps, which provide connectors for SFTP operations, reducing the need for custom-coded solutions.

    Testing: Before deploying, test your function against the actual SFTP server and not just a localhost version. This helps in catching connectivity issues earlier.

    In summary, the main issue is the hardcoding of localhost as the SFTP server when deploying to Azure. Ensure you're connecting to the right server, and make sure your function has the necessary permissions to connect.

    I hope this helps? If you have any questions please let me know.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.