Search code examples
rustazure-functions

Azure function app in Rust failing Http Worker, connection refused


I'm trying to deploy an azure function app (http trigger) implemented in Rust. I have created a function app using custom handler and try to read the port number from the environment variable FUNCTIONS_CUSTOMHANDLER_PORT

let port_key = "FUNCTIONS_CUSTOMHANDLER_PORT";
    let port: u16 = match env::var(port_key) {
        Ok(val) => val.parse().expect("Custom Handler port is not a number!"),
        Err(_) => 7071,
    };

However, the function app doesn't start properly, and the logs are full of messages like this: Waiting for HttpWorker to be initialized. Request to: http://127.0.0.1:42353/ failing with exception message: Connection refused (127.0.0.1:42353)

I can't find the variable FUNCTIONS_CUSTOMHANDLER_PORT in either the Function app setup in azure portal or in the list of env vars in Kudu. Do anyone know what I might need to do to make this variable available for the function app?


Solution

  • Make sure Custom handler is configured correctly in host.json and validate the configurations in function app. Configure the Azure Application Insights in Azure Functions to analyze the error messages about why the connection is being refused.

    I have created a Rust Azure function and deployed to Azure.

    Code Snippet:

    #[tokio::main]
    async fn main() {
        let example1 = warp::get()
            .and(warp::path("api"))
            .and(warp::path("HttpTrigger"))
            .and(warp::query::<HashMap<String, String>>())
            .map(|p: HashMap<String, String>| match p.get("name") {
                Some(name) => Response::builder().body(format!("Hello, {}. This HTTP triggered function executed successfully.", name)),
                None => Response::builder().body(String::from("This HTTP triggered function executed successfully. Pass a name in the query string for a personalized response.")),
            });
    
        let port_key = "FUNCTIONS_CUSTOMHANDLER_PORT";
        let port: u16 = match env::var(port_key) {
            Ok(val) => val.parse().expect("Custom Handler port is not a number!"),
            Err(_) => 3000,
        };
    
        warp::serve(example1).run((Ipv4Addr::LOCALHOST, port)).await
    }
    

    host.json:

    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        }
      },
      "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[4.*, 5.0.0)"
      },
      "customHandler": {
        "description": {
          "defaultExecutablePath": "handler",
          "workingDirectory": "",
          "arguments": []
        },
        "enableForwardingHttpRequest": true
      }
    }
    

    Follow the steps provided in MSDOC to compile the custom handler for Azure before deploying the function to Linux Azure function app.

    Added FUNCTIONS_CUSTOMHANDLER_PORT=<PortNumber> under Function App=>Settings=>Environment Variables=>App Settings.

    Check if the updated App settings are available in the KUDU site(https://functionapp.scm.azurewebsites.net)=> App Settings:

    enter image description here

    Deployed the function to Azure using Visual Studio.

    Able to run the function in portal:

    2024-09-16T12:59:03Z   [Information]   Executing 'Functions.HttpTrigger' (Reason='This function was programmatically called via the host APIs.', Id=b3ff7d0b-ae4b-4060-af60-2791b59f5fbc)
    2024-09-16T12:59:03Z   [Verbose]   Sending invocation id: 'b3ff7d0b-ae4b-4060-af60-2791b59f5fbc
    2024-09-16T12:59:03Z   [Verbose]   Forwarding httpTrigger invocation for function: 'HttpTrigger' invocationId: 'b3ff7d0b-aeXX9f5fbc'
    2024-09-16T12:59:03Z   [Verbose]   Sending invocation for function: 'HttpTrigger' invocationId: 'b3ff7d0b-ae4b-XX5fbc'
    2024-09-16T12:59:03Z   [Verbose]   Received invocation response for function: 'HttpTrigger' invocationId: 'b3ff7d0b-aXXX1b59f5fbc'
    2024-09-16T12:59:03Z   [Information]   Executed 'Functions.HttpTrigger' (Succeeded, Id=b3ff7d0b-ae4b-4060-af60-2791b59f5fbc, Duration=18ms)
    

    enter image description here