logo
Published on

A more elegant way to develop Azure Functions using Python

azure
Authors

The Azure Functions Programming Model V2 offers Python developers a more streamlined and Pythonic approach to building and managing serverless applications on Azure. With improvements like decorators for triggers and bindings, the introduction of blueprints, and enhanced async support, V2 brings simplicity, flexibility, and elegance to serverless development.

In this post, we will explore how this new programming model can enhance the structure, readability, and maintainability of your Azure Function projects, focusing on a real-world use case involving AI-driven tasks such as text classification, image generation, and translation.

What has Changed?

If you have worked with Azure Functions in Python before, you will be familiar with the boilerplate-heavy structure of the V1 model, where each function needed a separate folder and configuration (function.json). V2 addresses these issues head-on, reducing file bloat and emphasizing code simplicity through Python's native constructs.

Key Features of V2:

  • Decorators for Triggers and Bindings: Python decorators replace the need for external JSON configuration files. Triggers such as HTTP requests, timers, and storage queues can now be declared directly in your code, making it easier to understand and manage.
  • Blueprints: Modularize your application by grouping related functions into blueprints, ensuring your project remains clean and maintainable as it scales.
  • Simplified Folder Structure: Instead of creating separate folders for each function, V2 allows you to define all your functions in a single file (function_app.py). Note: This might not be ideal as the number of your functions get larger. Fortunately you can still choose to create a standalon package for each function by leveraging Blueprints. See example in the next section.

Example Use Case: AI-Powered Text Classification, Image Generation, and Translation

To demonstrate the capabilities of the V2 programming model, let us walk through a project that uses Azure Functions to handle AI-driven tasks. This project will:

  • Classify text using GPT-4o-mini
  • Generate images via an AI model like DALL-E.
  • Translate text using GPT-4o

You can find the complete project and code samples on this GitHub Repo.

Project Structure

V1 Project Structure

In V1, each function requires a separate folder, and you must define the function’s configuration in a function.json file. The logic is contained in the __init__.py file within each function's folder.

.
├── ClassifyFunction/
│   ├── __init__.py
│   ├── function.json
├── TranslateFunction/
│   ├── __init__.py
│   ├── function.json
├── GenerateImageFunction/
│   ├── __init__.py
│   ├── function.json
├── host.json
├── local.settings.json
└── requirements.txt

Drawbacks: The need for a function.json configuration file for each function is arguably cumbersome and verbose, which is simplified in V2 by using Pythonic decorators for triggers and bindings.

V2 Project Structure In V2, you can define multiple functions in a single file (function_app.py) and use Python decorators to declare triggers and bindings, eliminating the need for the function.json file. Each folder (image_generator, translator, classifier) contains the logic for its respective task.tests/ includes unit tests for each function, promoting testability and code reliability.

.
├── image_generator/
│   ├── __init__.py
│   └── generate_image.py
├── translator/
│   ├── __init__.py
│   └── translate_text.py
├── classifier/
│   ├── __init__.py
│   └── classify_text.py
├── function_app.py
├── host.json
├── local.settings.json
├── requirements.txt
└── tests/
    ├── __init__.py
    ├── test_image_generator.py
    ├── test_translator.py
    └── test_classifier.py


Bonus: Seamless Integration with FastAPI

If you’re already comfortable with frameworks like FastAPI, you will be pleased to know that V2 integrates seamlessly. Using AsgiFunctionApp, you can run FastAPI applications directly within Azure Functions, allowing you to build scalable APIs on a serverless platform.

Here’s an example FastAPI app running as an Azure Function:

# ./function_app.py
from fastapi import FastAPI
from azure.functions import AsgiFunctionApp

app = FastAPI()

@app.get("/translate")
async def translate(text: str):
    # Logic to call Azure Translator or other translation API
    return {"translation": "translated text"}

function_app = AsgiFunctionApp(app=app)

This integration allows developers to:

  • Utilize FastAPI’s validation, routing, and documentation features.
  • Take advantage of Azure Functions' scalability without maintaining a full server setup.

Conclusion

The Azure Functions Programming Model V2 for Python simplifies serverless development, bringing the elegance of Pythonic design to the world of cloud functions. Whether you’re building AI-powered applications or integrating REST APIs with FastAPI, V2 provides a flexible, scalable, and maintainable way to manage your serverless functions.

References