Intermediate

Azure Functions for AI Inference

Run machine learning inference on Azure Functions with custom containers, Durable Functions for complex pipelines, and Blob Storage model caching.

Azure Functions Hosting Plans

PlanMemoryTimeoutBest For ML
Consumption1.5 GB10 minSmall models (sklearn, ONNX)
Flex Consumption4 GB30 minMedium models with always-ready instances
Premium (EP1-EP3)3.5-14 GBUnlimitedLarger models with warm instances
Container AppsCustomCustomFull PyTorch/TF with GPU support

Python Function with ML Model

Python - Azure Function
import azure.functions as func
import json
import onnxruntime as ort
from azure.storage.blob import BlobServiceClient

app = func.FunctionApp()

# Load model at module level for reuse
session = ort.InferenceSession("model/sentiment.onnx")

@app.route(route="predict", methods=["POST"])
def predict(req: func.HttpRequest) -> func.HttpResponse:
    body = req.get_json()
    inputs = {session.get_inputs()[0].name: body["tokens"]}

    result = session.run(None, inputs)

    return func.HttpResponse(
        json.dumps({"sentiment": result[0].tolist()}),
        mimetype="application/json"
    )

Durable Functions for ML Pipelines

Durable Functions enable orchestrating multi-step inference pipelines where you need to chain preprocessing, inference, and postprocessing as separate functions with built-in retry logic and state management.

Model Management with Blob Storage

Store model versions in Azure Blob Storage and download them during function initialization. Use the function's local temporary storage as a model cache to avoid re-downloading on warm invocations.

Best practice: Use the Premium plan with always-ready instances for production ML inference. This eliminates cold starts while still providing auto-scaling. Set FUNCTIONS_WORKER_PROCESS_COUNT to 1 for ML workloads to avoid memory contention between worker processes.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.