I am creating an Azure function in Python with a http_trigger. It’s using the v2 model:
import azure.functions as func
import logging
app = func.FunctionApp()
@app.route(route="customers/{id:guid?}", auth_level=func.AuthLevel.FUNCTION, methods=["GET", "POST", "PUT", "DELETE"])
def customers(req: func.HttpRequest) -> func.HttpResponse:
If I create a singleton Cosmos DB client as mentioned in Manage connections in Azure Functions and Performance tips for Azure Cosmos DB Python SDK in the global area as follows:
import azure.functions as func
import logging
from azure.cosmos import CosmosClient
from azure.identity import DefaultAzureCredential
import os
cosmos_account = os.environ["COSMOS_ACCOUNT"]
cosmos_client = CosmosClient(f"https://{cosmos_account}.documents.azure:443/", credential)
cosmos_database = cosmos_client.get_database_client(os.environ["COSMOS_DATABASE"])
cosmos_container = cosmos_database.get_container_client(os.environ["COSMOS_CONTAINER"])
app = func.FunctionApp()
@app.route(route="customers/{id:guid?}", auth_level=func.AuthLevel.FUNCTION, methods=["GET", "POST", "PUT", "DELETE"])
def customers(req: func.HttpRequest) -> func.HttpResponse:
The function "customers" fails to be detected when it's loaded into my Azure Function. However if I remove that Cosmos DB client creation from the global space and move it into the "customers" function, it is loaded and able to be executed.
Based on best practices from the above links, it appears it should be a global client that can be reused. Can anyone share the proper syntax?