Skip to content

Adopt AVM AI Gateway module (avm/res/api-management/aigateway) in infra/main.bicep - #32

Draft
Paul Yuknewicz (paulyuk) wants to merge 1 commit into
mainfrom
copilot/adopt-avm-aigateway-module
Draft

Paul Yuknewicz (paulyuk) wants to merge 1 commit into
mainfrom
copilot/adopt-avm-aigateway-module

Conversation

@paulyuk

Copy link
Copy Markdown
Member

Problem

infra/main.bicep currently invokes a custom infra/ai-gateway/main.bicep wrapper with bespoke parameter arrays (foundryModels, modelAliasesConfig, toolServerConfigs) instead of the standard Azure Verified Module (avm/res/api-management/aigateway). Running a logical test of switching this sample to the new AVM module also surfaced four concrete integration requirements for multi-resource-group Foundry + GitHub MCP samples (all fixed in paulyuk/bicep-registry-modules@ad26c1a):

  1. Cross-RG Foundry RBAC ordering with system-assigned identity (assignUpstreamRole: true): When AI Gateway uses a system-assigned managed identity (managedIdentities: { systemAssigned: true }), gateway.identity.principalId must be granted Azure AI User (53ca6127-db72-4b80-b1b0-d745d6d5456d) on the upstream foundryModelsRg account before workspaces/default/modelProviders/foundry is provisioned.
  2. Two-leg GitHub MCP (toolServers/github + Foundry Project RemoteTool connection):
    • Upstream (workspaces/default/toolServers/github): Requires failureMode: 'failClosed', inline policies (tokenLimit, costLimit, contentSafety), and dictionary headers supporting both plain values (X-MCP-Readonly: 'true', X-MCP-Tools: 'list_pull_requests,list_issues,actions_list') and optional @secure() secret references (Authorization via secrets.githubMcpAuth, or injected in postprovision from gh auth token).
    • Downstream (Microsoft.CognitiveServices/accounts/projects/connections@2025-04-01-preview): infra/ai-gateway/github-connection.bicep reads existing apiKeys/default.listSecrets().key in gatewayRg and provisions the github RemoteTool project connection in foundryAgentsRg pointing to https://${aiGateway.outputs.defaultHostname}/mcp/github.
  3. Subscription-scoped Application Insights wiring: Allows telemetryExporters (kind: 'ApplicationInsights') to resolve connectionString directly from applicationInsights.resourceId so subscription-scoped infra/main.bicep does not need to export a secret connection string from a child module.

Stack Order

Stacked on top of the active Bicep PRs so those land first and this PR can switch its module path from ./avm/res/api-management/aigateway/main.bicep to br/public:avm/res/api-management/aigateway:0.1.0 once the upstream AVM PR is approved:

  1. Remove legacy Microsoft.Web/connectorGateways resource from AI Gateway Bicep #31 — Remove legacy Microsoft.Web/connectorGateways
  2. Configure AI Gateway native OpenTelemetry monitoring #26 — Update AI Gateway monitoring
  3. Move Foundry remote-tool connection provisioning to Bicep #27 — Move Foundry GitHub remote-tool connection to Bicep
  4. Configure AI Gateway model and GitHub MCP policies #29 — Configure AI Gateway model and tool policies (base branch of this stacked PR)
  5. This PR (Draft) — Switch module aiGateway in infra/main.bicep to avm/res/api-management/aigateway

Solution & Canonical DX in infra/main.bicep

// Swap './avm/res/api-management/aigateway/main.bicep' -> 'br/public:avm/res/api-management/aigateway:0.1.0' once AVM PR is approved
module aiGateway './avm/res/api-management/aigateway/main.bicep' = {
  name: 'ai-gateway-${resourceToken}'
  scope: gatewayRg
  params: {
    name: effectiveAiGatewayName
    location: aiGatewayLocation
    tags: tags
    managedIdentities: {
      systemAssigned: true
    }
    apiKeys: [
      {
        name: 'default'
        properties: {
          displayName: 'Default hosted agent key'
        }
      }
    ]
    secrets: !empty(githubMcpAuthorization)
      ? {
          githubMcpAuth: githubMcpAuthorization
        }
      : {}
    workspaces: [
      {
        name: 'default'
        modelProviders: [
          {
            name: 'foundry'
            properties: {
              kind: 'Foundry'
              displayName: 'Foundry'
              foundry: {
                endpoint: foundryModels.outputs.aiServicesEndpoint
                resourceIds: [
                  foundryModels.outputs.aiServicesId
                ]
                assignUpstreamRole: true
                authentication: {
                  kind: 'ManagedIdentity'
                  managedIdentity: {
                    resource: 'https://cognitiveservices.azure.com/'
                  }
                }
              }
            }
            models: [for (deployment, i) in gatewayModelDeployments: {
              name: deployment.deploymentName
              properties: {
                displayName: deployment.modelName
                description: deployment.description
                apiFormat: 'OpenAIChatCompletions'
                supportedEndpoints: [
                  '/openai/v1/chat/completions'
                  '/openai/v1/responses'
                ]
                deployment: {
                  resourceId: foundryModels.outputs.modelDeploymentIds[i]
                  modelName: deployment.deploymentName
                  modelVersion: deployment.modelVersion
                }
                policies: [
                  {
                    type: 'tokenLimit'
                    period: 'minute'
                    count: deployment.capacity * 1000
                    counterKey: ['identity']
                  }
                  {
                    id: 'monthly-caller-budget'
                    type: 'costLimit'
                    period: 'month'
                    amount: 10
                    counterKey: ['identity']
                  }
                  {
                    type: 'contentSafety'
                    hateSeverity: 'Medium'
                    selfHarmSeverity: 'Medium'
                    sexualSeverity: 'Medium'
                    violenceSeverity: 'Medium'
                  }
                ]
              }
            }]
          }
        ]
        aliases: [
          {
            name: modelAliasName // 'gpt-latest'
            properties: {
              displayName: 'Primary Model Router'
              routes: [for target in effectiveModelAliasBackends: {
                providerName: 'foundry'
                modelName: target.modelArmName
                priority: target.priority
                weight: target.weight
              }]
            }
          }
          {
            name: miniModelAliasName // 'gpt-mini-latest'
            properties: {
              displayName: 'Mini Model Router'
              routes: [
                {
                  providerName: 'foundry'
                  modelName: miniModelDeploymentName
                  priority: 0
                  weight: 100
                }
              ]
            }
          }
        ]
        toolServers: [
          {
            name: 'github'
            properties: {
              displayName: 'GitHub repository digest'
              description: 'Read-only GitHub tools for repository digests.'
              type: 'mcp'
              failureMode: 'failClosed'
              policies: [
                {
                  type: 'tokenLimit'
                  period: 'minute'
                  count: 50000
                  counterKey: ['identity']
                }
                {
                  id: 'monthly-caller-budget'
                  type: 'costLimit'
                  period: 'month'
                  amount: 5
                  counterKey: ['identity']
                }
                {
                  type: 'contentSafety'
                  hateSeverity: 'Medium'
                  selfHarmSeverity: 'Medium'
                  sexualSeverity: 'Medium'
                  violenceSeverity: 'Medium'
                }
              ]
              endpoints: [
                {
                  namespace: 'github'
                  kind: 'mcp'
                  mcp: {
                    url: 'https://api.githubcopilot.com/mcp/'
                    transport: 'streamableHttp'
                  }
                  credentials: {
                    type: 'header'
                    headers: concat(
                      !empty(githubMcpAuthorization)
                        ? [{ name: 'Authorization', secretName: 'githubMcpAuth' }]
                        : [],
                      [
                        { name: 'X-MCP-Readonly', value: 'true' }
                        { name: 'X-MCP-Tools', value: 'list_pull_requests,list_issues,actions_list' }
                      ]
                    )
                  }
                }
              ]
            }
          }
        ]
        telemetryExporters: enableMonitoring
          ? [
              {
                name: 'applicationInsights'
                properties: {
                  kind: 'ApplicationInsights'
                  payloadCapture: false
                  applicationInsights: {
                    resourceId: aiGatewayMonitoring.outputs.appInsightsId
                  }
                }
              }
            ]
          : []
      }
    ]
  }
}

Validation

  • az bicep build --file infra/main.bicep compiles cleanly with zero errors.
  • bash tests/test-ai-gateway-model-registration.sh passes end-to-end.

@paulyuk
Paul Yuknewicz (paulyuk) changed the base branch from copilot/configure-token-limit-content-safety to main November 1, 2026 21:33
…and cross-RG Foundry RBAC

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4448b1aa-1ea5-4c5f-af63-7ec98b7acb9e
@paulyuk
Paul Yuknewicz (paulyuk) force-pushed the copilot/adopt-avm-aigateway-module branch from 42512a8 to 4901964 Compare November 1, 2026 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant