Adrian Haltenberger
Adrian Haltenberger

Reputation: 21

How to fix "HostInitializationException" in Azure Functions

First I followed the instructions to get a Function Application running. https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-python

After successfull creation of the application, I've deployed a small HTTP triggered function to Azure for testing purposes.

My function is written in python. I am using a Linux OS for pushing to Azure. Everything looks fine.

I use this statement for publishing: func azure functionapp publish myApp --publish-local-settings

After successfull deployment to Azure, I've tried to reach "https://myAppName.azurewebsites.net", which gives me at first a Site with Error Code 502. After a few minutes it change its status and I get the welcome page of the Azure Functions.

If I try to reach the function directly via: https://myAppName.azurewebsites.net/api/functionName

I get an 502.. Even after waiting 30 Minutes, the function is still not running correctly..

Please let me know, if you have related useful information.

Take a look in the "Application Insights" shows some more information:

09:55:40 | Trace@(none)
Hosting stopping
09:55:40 | Exception | HostInitializationException@(none)
Did not find functions with language [python].
09:55:40 | Trace@(none)
A host error has occurred
09:55:40 | Trace@(none)
Creating function descriptors.
09:55:40 | Trace@(none)
Adding Function descriptor provider for language python.
09:55:40 | Trace@(none)
1 proxies loaded
import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )


{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}
{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python"
  }
}
{
  "version": "2.0"
}

Upvotes: 2

Views: 1009

Answers (1)

bharathn-msft
bharathn-msft

Reputation: 962

Possible reason for the errors you see is because of the local.settings.json file and that being published to Azure by using --publish-local-settings overriding the settings in Azure Function App.

With below files was able to host it locally and also publish it to Azure.

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsStorage": "{AzureWebJobsStorage}"
  }
}

host.json

{
    "version":  "2.0"
}

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

init.py

import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

Running the below steps in VS Code

func init MyFunctionProj2 (selected Python)
cd .\MyFunctionProj2\
func new (selected HTTP trigger)
func host start     

#Pick your region of choice

az functionapp create --resource-group rgname --os-type Linux --consumption-plan-location westeurope  --runtime python --name funcappname --storage-account storageaccountname

func azure functionapp publish funcappname

When hosted on local it works with below URL

enter image description here

When published to Azure Function by following above listed steps

enter image description here

You can get the function URL from Azure portal with in the Function

enter image description here

Check the Function App settings have the right connection string for storage.

Additional documentation reference for local settings file.

Hope this helps.

Upvotes: 1

Related Questions