Skip to content

About

GPU-powered Azure Functions for AI image generation with Stable Diffusion on Container Apps

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

15 Commits

Folders and files

Repository files navigation

Serverless GPU Tutorial: AI Image Generator with Azure Functions

Deploy Stable Diffusion on Azure Container Apps with serverless GPUs. NVIDIA T4 GPU power with scale-to-zero pricing. One-command deployment with azd up.

Deploy to Azure

This project demonstrates how to deploy a GPU-accelerated image generation API (using Stable Diffusion) as an Azure Function running on Azure Container Apps with serverless GPU workload profiles.

🎯 Overview

This sample is inspired by the Azure Container Apps GPU Image Generation Tutorial, but modified to run as an Azure Function instead of a regular container. This provides:

  • πŸš€ Fast - NVIDIA T4 GPUs generate images in seconds
  • πŸ’° Cost-effective - Scale to zero, only pay when generating images
  • πŸ”§ Simple - No GPU drivers or infrastructure to manage
  • πŸ“ˆ Scalable - Handles multiple requests automatically
  • ⚑ Event-driven - Azure Functions programming model with triggers and bindings

πŸ“ Project Structure

gpu-function-image-gen/
β”œβ”€β”€ function_app.py        # Main Azure Functions application code
β”œβ”€β”€ host.json              # Azure Functions host configuration
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ Dockerfile             # GPU-enabled Docker image
β”œβ”€β”€ azure.yaml             # Azure Developer CLI configuration
β”œβ”€β”€ infra/                 # Bicep templates for infrastructure
β”‚   β”œβ”€β”€ main.bicep
β”‚   β”œβ”€β”€ api.bicep
β”‚   └── core/host/container-apps.bicep
β”œβ”€β”€ deploy.ps1             # PowerShell deployment script
β”œβ”€β”€ deploy.sh              # Bash deployment script
└── README.md              # This file

πŸš€ Quick Start

Prerequisites

  1. Azure subscription with access to GPU quotas
  2. Azure CLI installed and configured
  3. GPU quota approved - Request access here

Deploy to Azure

You have three deployment options:

Option Method Best For
Option A Azure Developer CLI (azd up) Fastest, one-command deployment
Option B PowerShell/Bash scripts More control, customizable
Option C Manual CLI commands Learning, step-by-step

Option A: Azure Developer CLI (Recommended) πŸš€

The fastest way to deploy - one command does everything!

# Install Azure Developer CLI if you haven't
# https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd

# Clone and deploy
git clone https://github.com/Azure-Samples/function-on-aca-gpu.git
cd function-on-aca-gpu
azd up

You'll be prompted for:

  • Environment name: A unique name (e.g., gpufunc-dev)
  • Azure location: Select swedencentral
  • Azure subscription: Select your subscription

Resources created:

  • Resource Group: rg-{environmentName}
  • Log Analytics: log-{environmentName}
  • Application Insights: appi-{environmentName}
  • Container Registry: acr{environmentName}
  • Storage Account: st{environmentName}
  • Container Apps Environment: cae-{environmentName} (with GPU workload profile)
  • Function App: ca-{environmentName}

Clean up:

azd down

Option B: PowerShell/Bash Scripts

Windows (PowerShell):

cd function-on-aca-gpu
.\deploy.ps1

Linux/macOS/WSL (Bash):

cd function-on-aca-gpu
chmod +x deploy.sh
./deploy.sh

Option C: Manual Deployment Steps

If you prefer to deploy manually:

  1. Create Resource Group and ACR:

    az group create --name gpu-functions-rg --location swedencentral
    az acr create --resource-group gpu-functions-rg --name gpufunctionsacr --sku Standard --admin-enabled true
  2. Build and push the Docker image:

    az acr build --registry gpufunctionsacr --image gpu-image-gen:latest --file Dockerfile .
  3. Create Container Apps Environment with GPU:

    az containerapp env create --name gpu-functions-env --resource-group gpu-functions-rg --location swedencentral --enable-workload-profiles
    
    az containerapp env workload-profile add --name gpu-functions-env --resource-group gpu-functions-rg --workload-profile-name gpu-profile --workload-profile-type Consumption-GPU-NC8as-T4
  4. Create Storage Account:

    az storage account create --name gpufuncstg123 --resource-group gpu-functions-rg --location swedencentral --sku Standard_LRS
  5. Deploy Function App:

    az functionapp create \
        --name gpu-image-gen-func \
        --resource-group gpu-functions-rg \
        --storage-account gpufuncstg123 \
        --environment gpu-functions-env \
        --functions-version 4 \
        --runtime python \
        --image gpufunctionsacr.azurecr.io/gpu-image-gen:latest \
        --registry-server gpufunctionsacr.azurecr.io \
        --registry-username <acr-username> \
        --registry-password <acr-password> \
        --workload-profile-name gpu-profile \
        --cpu 4 \
        --memory 28Gi

πŸ“‘ API Endpoints

Generate Image

POST /api/generate

Generate an image from a text prompt.

Request Body:

{
  "prompt": "A beautiful sunset over mountains, digital art, 4k",
  "negative_prompt": "blurry, low quality",
  "num_steps": 25,
  "guidance_scale": 7.5,
  "width": 512,
  "height": 512
}

Response:

{
  "success": true,
  "prompt": "A beautiful sunset over mountains...",
  "image": "<base64-encoded-png>",
  "format": "png",
  "width": 512,
  "height": 512
}

Example with curl:

curl -X POST https://<your-function-app>.azurewebsites.net/api/generate \
  -H "Content-Type: application/json" \
  -d '{"prompt": "A cute cat wearing a space helmet, digital art"}'

Health Check

GET /api/health

Check service health and GPU status.

Response:

{
  "status": "healthy",
  "gpu_available": true,
  "gpu_info": {
    "name": "Tesla T4",
    "memory_total_gb": 15.0,
    "memory_allocated_gb": 2.5,
    "memory_reserved_gb": 3.0
  },
  "model_loaded": true
}

Web UI

GET /api/

Access a simple web interface to generate images interactively.

βš™οΈ Configuration

Environment Variables

Variable Description Default
MODEL_ID Hugging Face model ID stabilityai/stable-diffusion-2-1-base
AzureWebJobsStorage Storage connection string Required
FUNCTIONS_WORKER_RUNTIME Runtime identifier python

Supported GPU Workload Profiles

Profile GPU vCPUs Memory Best For
Consumption-GPU-NC8as-T4 NVIDIA T4 8 56 GB Image generation, inference
Consumption-GPU-NC16as-T4 NVIDIA T4 16 110 GB Larger models
Consumption-GPU-NC24as-T4 NVIDIA T4 24 220 GB Multiple concurrent requests

Supported Regions

GPU workload profiles are available in:

  • Sweden Central
  • West US 3
  • Australia East
  • East US 2

Check the official documentation for the latest supported regions.

πŸ”§ Local Development

With GPU (requires NVIDIA GPU and Docker with GPU support):

# Build the image
docker build -t gpu-image-gen:local -f Dockerfile .

# Run with GPU support
docker run --gpus all -p 7071:80 gpu-image-gen:local

Without GPU (CPU-only, slower):

# Create virtual environment
python -m venv .venv
.venv\Scripts\activate  # Windows
# or: source .venv/bin/activate  # Linux/macOS

# Install dependencies (CPU-only PyTorch)
pip install -r requirements.txt

# Run locally
func start

πŸ’‘ Tips for Better Performance

  1. Reduce cold start time:

    • Uncomment the model pre-download in Dockerfile
    • Use artifact streaming (enable in Azure Portal)
    • Keep min-replicas >= 1 for warm instances
  2. Optimize inference:

    • Enable xFormers for memory-efficient attention
    • Use smaller image dimensions (512x512)
    • Reduce inference steps (20-30 is usually sufficient)
  3. Cost optimization:

    • Set min-replicas: 0 when not in use
    • Use appropriate timeout values
    • Monitor GPU utilization

πŸ“š Related Resources

🧹 Clean Up

To remove all resources:

az group delete --name gpu-functions-rg --yes --no-wait

πŸ“„ License

This sample is provided under the MIT license.

About

GPU-powered Azure Functions for AI image generation with Stable Diffusion on Container Apps

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages