Featured image of post Building Resilient Azure AI Architecture with API Management Gateway

Building Resilient Azure AI Architecture with API Management Gateway

Build an enterprise-grade, highly resilient AI architecture with Azure APIM for cross-subscription quota aggregation, load balancing, automatic failover, and unified governance.

As enterprise demand for AI capabilities grows, organizations are deeply integrating Microsoft Foundry model services into core business systems. As AI moves from experimentation to production, high availability, elastic scaling, unified governance, and security controls become unavoidable themes.

In production, a single subscription instance fails to meet requirements for continuity, stability, and throughput. A mature architecture consolidates resources across subscriptions, aggregating AI quotas to maximize throughput, with traffic distribution, automatic failover, and unified access control.

Azure API Management (APIM), as Azure’s native API gateway, is naturally suited for this role. By deploying APIM as the unified entry point, combined with the following capabilities, we can build an enterprise-grade, highly resilient AI architecture:

  • 🔀 Backend Load Balancing Pool — Load balancing and failover across subscriptions and Foundry instances, aggregating distributed quotas
  • 🔐 Managed Identity — Zero-key secure authentication between APIM and backend AI services
  • 🛡️ Unified API Governance — Request routing, traffic control, log auditing, and policy management at the gateway layer

This article walks through a complete hands-on example: from resource deployment and identity configuration to load balancing policies, building a production-ready resilient gateway architecture targeting multiple Microsoft Foundry resources.

Architecture Overview

1-Architecture

This architecture uses a centralized API gateway + multi-subscription backend pattern, with APIM as the unified entry point, distributing requests round-robin to Foundry instances across multiple subscriptions and regions.

Core design principles:

  • Unified Entry Point — Clients only need the APIM endpoint; the pool uses Round-Robin by default
  • Simplified Security Authentication — Zero-key communication via Managed Identity; clients only need a single API Key
  • Cross-Subscription Quota Aggregation — Aggregate quotas across subscriptions into a unified capacity pool
  • Cross-Region Disaster Recovery — APIM automatically routes to healthy instances when a region becomes unavailable

APIM’s Policy engine also provides flexible traffic control:

  • Pinned Routing — via X-AI-Foundry-Target header
  • Auto Failover — Auto-retry with exponential backoff on 4xx/5xx
  • Request Tracing — X-AI-Foundry-Backend response header for tracing

Architecture setup steps:

SectionActionGoalPlatform
1Create APIM instanceSet up the unified gateway entry point and enable Managed IdentityAzure Portal
2Create Foundry resourcesBuild the backend resource pool across multiple subscriptions and regionsAzure Portal
3Deploy AI modelsEnable AI service capability on backend resources (using Sora2 as an example)Foundry Portal
4Configure RBACGrant APIM Managed Identity permission to access backendsAzure Portal
5Configure Backend + LB PoolRegister backends and create the load balancing poolAzure Portal (APIM)
6Create API + PolicyDefine the API entry point and configure routing and failoverAzure Portal (APIM)
7Test and validateVerify load balancing and pinned backend routingcurl / Postman

Create APIM Instance

Create an API Management instance in Azure Portal, selecting v2 Tier, and enable System Assigned Managed Identity during creation.

Portal Steps: Search API Management Service → + Create → Fill in configuration → Enable on the Managed identity tab → Review + install

CLI Alternative:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
az rest --method PUT \
  --url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.ApiManagement/service/<apim-name>?api-version=2024-05-01" \
  --body '{
    "location": "<region>",
    "sku": { "name": "<BasicV2|StandardV2|PremiumV2>", "capacity": 1 },
    "identity": { "type": "SystemAssigned" },
    "properties": {
      "publisherName": "<organization-name>",
      "publisherEmail": "<admin-email>"
    }
  }'

After Creation:

  • Status: Online
  • Record Gateway URL (for example: https://<apim-name>.azure-api.net)
  • Record the Managed Identity Object (principal) ID for RBAC


Create Foundry Resources

Create Foundry resources across multiple subscriptions and regions, each with a corresponding project.

Portal Steps: Search Microsoft Foundry → Create a resource → Fill in Subscription / Region / Name / Project name → Create

CLI Alternative:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
az cognitiveservices account create \
  --subscription <subscription-id> \
  --resource-group <resource-group> \
  --name <foundry-resource-name> \
  --location <region-code> \
  --kind AIServices --sku S0 \
  --custom-domain <foundry-resource-name> \
  --allow-project-management && \
az cognitiveservices account project create \
  --subscription <subscription-id> \
  --name <foundry-resource-name> \
  --resource-group <resource-group> \
  --project-name <project-name> \
  --location <region-code>

Example Resource Distribution:

SubscriptionRegionResource Name
Subscription AEast US 2foundry-eastus2-01
Subscription ASweden Centralfoundry-swedencentral-01
Subscription BEast US 2foundry-eastus2-02
Subscription BSweden Centralfoundry-swedencentral-02

Deploy Models

Deploy AI models in each Foundry resource via the Foundry Portal.

Portal Steps: In Foundry Portal, select the target project → Build → Models → Deploy a base model → Search for a model (for example sora-2) → Deploy → Default settings

CLI Alternative:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
az cognitiveservices account deployment create \
  --subscription <subscription-id> \
  --resource-group <resource-group> \
  --name <foundry-resource-name> \
  --deployment-name <deployment-name> \
  --model-name <model-name> \
  --model-version <model-version> \
  --model-format <model-format> \
  --sku-name <sku-name> \
  --sku-capacity <capacity>

Model names, versions, and SKUs vary by region. See: Models and Region Availability


Configure RBAC

Grant the Cognitive Services OpenAI User role to APIM’s Managed Identity on each Foundry resource.

Portal Steps: Go to the Foundry resource → Access control (IAM) → + Add → Add role assignment → Search Cognitive Services OpenAI User → In Members choose Managed identity → Select the API Management service → Review + assign

CLI Alternative:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Get APIM Managed Identity Principal ID
az apim show \
  --subscription <subscription-id> \
  --name <apim-name> \
  --resource-group <resource-group> \
  --query "identity.principalId" -o tsv

# Assign role for each Foundry resource
az role assignment create \
  --assignee <principal-id> \
  --role "Cognitive Services OpenAI User" \
  --scope /subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.CognitiveServices/accounts/<foundry-resource-name>

Cognitive Services OpenAI User is the minimum permission role—allows inference calls but not resource configuration changes.


Configure Backend + LB Pool

Create Backend

Create a Backend for each Foundry resource with Managed Identity authentication.

Get OpenAI Endpoint: On the project Home page in Foundry Portal, find Azure OpenAI endpoint, or use the CLI:

1
2
3
4
5
az cognitiveservices account show \
  --subscription <subscription-id> \
  --resource-group <resource-group> \
  --name <foundry-resource-name> \
  --query "properties.endpoints.\"OpenAI Language Model Instance API\"" -o tsv

Portal Steps: APIM → Backends → + Create new backend → Fill in Name / Runtime URL → Managed Identity tab → Client identity = System assigned → Resource ID = https://cognitiveservices.azure.com → Create

Key Configuration:

ConfigurationExample ValueDescription
Name<foundry-resource-name>Recommended to match the Foundry resource name
Runtime URLhttps://<foundry-resource-name>.openai.azure.com/openai/v1Foundry OpenAI endpoint (+ openai/v1)
Client identitySystem assigned identityUse APIM system-assigned managed identity
Resource IDhttps://cognitiveservices.azure.comToken audience, fixed value

CLI Alternative:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
az rest --method PUT \
  --url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.ApiManagement/service/<apim-name>/backends/<backend-name>?api-version=2024-05-01" \
  --body '{
    "properties": {
      "url": "https://<foundry-resource-name>.openai.azure.com/openai/v1",
      "protocol": "http",
      "credentials": {
        "managedIdentity": {
          "clientId": null,
          "resource": "https://cognitiveservices.azure.com"
        }
      }
    }
  }'

Create LB Pool

Portal Steps: APIM → Backends → Load balancer tab → + Create new pool → Fill in Pool Name → Select all Backends → Send requests evenly (Round-robin) → Create

Portal provides two options: Send requests evenly (Round-robin) or Customize weight and priority.


Create API + Policy

Create API

Portal Steps: APIM → APIs → + Add API → HTTP → Fill in Display name / API URL suffix (openai/v1) → Leave Web service URL empty → Create

Then add Operations: create /* wildcard path for POST, GET, and DELETE.

This example uses wildcard /* to proxy all requests. For finer control, create operations for specific paths like /videos/* for Sora2.

Configure Policy

Select All operations → click </> in Inbound processing → paste the policy below, update the pool name → Save

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<policies>
    <inbound>
        <base />
        <!-- Read request header so callers can pin a backend -->
        <set-variable name="target" value="@(context.Request.Headers.GetValueOrDefault("X-AI-Foundry-Target", ""))" />
        <!-- Backend load balancing pool name -->
        <set-variable name="pool_name" value="<your-ai-hub-pool-name>" />
        <choose>
            <when condition="@(context.Variables.GetValueOrDefault<string>("target") != "")">
                <set-backend-service backend-id="@(context.Variables.GetValueOrDefault<string>("target"))" />
            </when>
            <otherwise>
                <set-backend-service backend-id="@((string)context.Variables["pool_name"])" />
            </otherwise>
        </choose>
    </inbound>
    <outbound>
        <base />
        <set-header name="X-AI-Foundry-Backend" exists-action="override">
            <value>@(context.Request.Url.Host.Split('.')[0])</value>
        </set-header>
    </outbound>
    <on-error>
        <base />
    </on-error>
    <backend>
        <choose>
            <when condition="@(context.Variables.GetValueOrDefault<string>("target") != "")">
                <forward-request buffer-request-body="true" />
            </when>
            <otherwise>
                <retry condition="@(context.Response == null || context.Response.StatusCode >= 400)" count="3" interval="1" delta="1" max-interval="5" first-fast-retry="true">
                    <set-backend-service backend-id="@((string)context.Variables["pool_name"] )" />
                    <forward-request buffer-request-body="true" />
                </retry>
            </otherwise>
        </choose>
    </backend>
</policies>

Policy Logic:

AreaFunction
inboundRead X-AI-Foundry-Target; if present, route to the specified backend, otherwise use the LB Pool round-robin
outboundReturn the actual backend name in the X-AI-Foundry-Backend response header
backendForward directly for pinned routing; in pool mode, auto-retry and switch backend on failure (up to 3 attempts with exponential backoff)

Test and Validate

Prerequisites:

Gather the following before testing:

  • Get Gateway URL: APIM → Overview → Gateway URL
  • Get API Key: APIM → Subscriptions → Built-in all-access → Show keys → Primary key

Test 1: Load Balancing

Send consecutive requests without X-AI-Foundry-Target, and observe X-AI-Foundry-Backend rotating across backends:

1
2
3
curl -D - -o /dev/null --location \
  'https://<apim-gateway-url>/openai/v1/videos' \
  --header 'Ocp-Apim-Subscription-Key: <your-subscription-key>'

Expected: X-AI-Foundry-Backend changes with each request, confirming Round-robin is working.

Test 2: Pinned Backend Routing

Use X-AI-Foundry-Target header to pin to a specific backend:

1
2
3
4
5
6
7
curl -s -D - -o /dev/null --location \
  'https://<apim-gateway-url>/openai/v1/responses' \
  --header 'Content-Type: application/json' \
  --header 'Ocp-Apim-Subscription-Key: <your-subscription-key>' \
  --header 'X-AI-Foundry-Target: <backend-name>' \
  --data '{"model": "<model-name>", "input": "Hello"}' \
  | grep -i X-AI-Foundry-Backend

Expected: X-AI-Foundry-Backend consistently returns the specified backend name.


FAQ

Q: Why not use APIM Backend’s Circuit Breaker?

This architecture targets load balancing and quota aggregation. Circuit Breaker would “break” on backend failure, but a 429 on one model shouldn’t block other models on the same backend. Simple failover via retry + set-backend-service is more appropriate.

Q: Is this architecture only for Sora2?

No. It applies to any Foundry / Azure OpenAI model service (Responses API, Chat Completions, Embeddings, Image Generation, etc.)—just point the Backend URL to the corresponding endpoint.


Reference Documentation