Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# APIM Connection Examples

This folder contains Azure Bicep templates for creating APIM (API Management) connections to Azure AI Foundry projects.

## Prerequisites

1. **Azure CLI** installed and configured
2. **Existing APIM service** with APIs configured
3. **AI Foundry account and project** already created

## How to Deploy

### Basic APIM Connection
```bash
# 1. Edit parameters-basic.json with your resource IDs
# 2. Deploy using the parameters file
az deployment group create \
--resource-group <your-resource-group> \
--template-file connection-apim-basic.bicep \
--parameters @parameters-basic.json
```

### Deployment API Version APIM Connection
```bash
# 1. Edit parameters-deployment-api.json with your resource IDs
# 2. Deploy using the parameters file
az deployment group create \
--resource-group <your-resource-group> \
--template-file connection-apim-deployment-api-version.bicep \
--parameters @parameters-deployment-api.json
```

### Dynamic Discovery APIM Connection
```bash
# 1. Edit parameters-dynamic.json with your resource IDs
# 2. Deploy using the parameters file
az deployment group create \
--resource-group <your-resource-group> \
--template-file connection-apim-dynamic-discovery.bicep \
--parameters @parameters-dynamic.json
```

### Static Models APIM Connection
```bash
# 1. Edit parameters-static.json with your resource IDs
# 2. Deploy using the parameters file
az deployment group create \
--resource-group <your-resource-group> \
--template-file connection-apim-static-models.bicep \
--parameters @parameters-static.json
```

## Parameter Files

- `parameters-basic.json`: For basic APIM connections with minimal configuration
- `parameters-deployment-api.json`: For APIM connections with API versioning (includes inferenceAPIVersion and deploymentAPIVersion)
- `parameters-dynamic.json`: For APIM connections with dynamic model discovery (includes OpenAI endpoint configurations)
- `parameters-static.json`: For APIM connections with static model lists (includes customizable staticModels array)

Edit these files to update the resource IDs for your environment.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Connections enable your AI applications to access tools and objects managed elsewhere in or outside of Azure.

This example demonstrates how to add an Azure API Management connection for a specific API.
This implements Example 1 from the APIM Connection documentation: "All Defaults with Required Fields Only"

Uses all APIM defaults with only the required fields:
- deploymentInPath: Controls how deployment names are passed to APIM gateway
- inferenceAPIVersion: API version for model inference calls

This uses APIM default endpoints:
- List Deployments: /deployments
- Get Deployment: /deployments/{deploymentName}
- Provider: AzureOpenAI

IMPORTANT: Make sure you are logged into the subscription where the AI Foundry resource exists before deploying.
The connection will be created in the AI Foundry project, so you need to be in that subscription context.
Use: az account set --subscription <foundry-subscription-id>
*/

param projectResourceId string = '/subscriptions/12345678-1234-1234-1234-123456789abc/resourceGroups/rg-sample/providers/Microsoft.CognitiveServices/accounts/sample-foundry-account/projects/sample-project'
param apimResourceId string = '/subscriptions/87654321-4321-4321-4321-cba987654321/resourceGroups/rg-sample-apim/providers/Microsoft.ApiManagement/service/sample-apim-service'
param apiName string = 'foundry'
param apimSubscriptionName string = 'master' // Default subscription name in APIM, update it to your subscription name for apikey auth

// Connection naming - can be overridden via parameter
param connectionName string = '' // Optional: specify custom connection name

// Generate connection name if not provided
var apimServiceName = split(apimResourceId, '/')[8]
var generatedConnectionName = 'apim-${apimServiceName}-${apiName}'
var finalConnectionName = connectionName != '' ? connectionName : generatedConnectionName

// Connection configuration
@allowed([
'ApiKey'
'AAD'
])
param authType string = 'ApiKey' // Authentication type for the connection

param isSharedToAll bool = false // Whether the connection should be shared to all users in the project

// APIM-specific configuration parameters
@allowed([
'true'
'false'
])
param deploymentInPath string = 'true' // Controls how deployment names are passed to APIM gateway

param inferenceAPIVersion string = '2024-02-01' // API version for inference calls (chat completions, embeddings, etc.)

// Build the metadata object for Example 1: All Defaults with Required Fields Only
var example1Metadata = {
deploymentInPath: deploymentInPath
inferenceAPIVersion: inferenceAPIVersion
}

// Use the common module to create the APIM connection
module apimConnection 'modules/apim-connection-common.bicep' = {
name: 'apim-connection-example1'
params: {
projectResourceId: projectResourceId
connectionName: finalConnectionName
apimResourceId: apimResourceId
apiName: apiName
apimSubscriptionName: apimSubscriptionName
authType: authType
isSharedToAll: isSharedToAll
metadata: example1Metadata
}
}

// Output information from the connection
output connectionName string = apimConnection.outputs.connectionName
output connectionId string = apimConnection.outputs.connectionId
output targetUrl string = apimConnection.outputs.targetUrl
output authType string = apimConnection.outputs.authType
output metadata object = apimConnection.outputs.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.37.4.10188",
"templateHash": "10056086952953764903"
}
},
"parameters": {
"projectResourceId": {
"type": "string",
"defaultValue": "/subscriptions/12345678-1234-1234-1234-123456789abc/resourceGroups/rg-sample/providers/Microsoft.CognitiveServices/accounts/sample-foundry-account/projects/sample-project"
},
"apimResourceId": {
"type": "string",
"defaultValue": "/subscriptions/87654321-4321-4321-4321-cba987654321/resourceGroups/rg-sample-apim/providers/Microsoft.ApiManagement/service/sample-apim-service"
},
"apiName": {
"type": "string",
"defaultValue": "foundry"
},
"apimSubscriptionName": {
"type": "string",
"defaultValue": "master"
},
"connectionName": {
"type": "string",
"defaultValue": ""
},
"authType": {
"type": "string",
"defaultValue": "ApiKey",
"allowedValues": [
"ApiKey",
"AAD"
]
},
"isSharedToAll": {
"type": "bool",
"defaultValue": false
},
"deploymentInPath": {
"type": "string",
"defaultValue": "true",
"allowedValues": [
"true",
"false"
]
},
"inferenceAPIVersion": {
"type": "string",
"defaultValue": "2024-02-01"
}
},
"variables": {
"apimServiceName": "[split(parameters('apimResourceId'), '/')[8]]",
"generatedConnectionName": "[format('apim-{0}-{1}', variables('apimServiceName'), parameters('apiName'))]",
"finalConnectionName": "[if(not(equals(parameters('connectionName'), '')), parameters('connectionName'), variables('generatedConnectionName'))]",
"example1Metadata": {
"deploymentInPath": "[parameters('deploymentInPath')]",
"inferenceAPIVersion": "[parameters('inferenceAPIVersion')]"
}
},
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2022-09-01",
"name": "apim-connection-example1",
"properties": {
"expressionEvaluationOptions": {
"scope": "inner"
},
"mode": "Incremental",
"parameters": {
"projectResourceId": {
"value": "[parameters('projectResourceId')]"
},
"connectionName": {
"value": "[variables('finalConnectionName')]"
},
"apimResourceId": {
"value": "[parameters('apimResourceId')]"
},
"apiName": {
"value": "[parameters('apiName')]"
},
"apimSubscriptionName": {
"value": "[parameters('apimSubscriptionName')]"
},
"authType": {
"value": "[parameters('authType')]"
},
"isSharedToAll": {
"value": "[parameters('isSharedToAll')]"
},
"metadata": {
"value": "[variables('example1Metadata')]"
}
},
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.37.4.10188",
"templateHash": "5161752358692660897"
}
},
"parameters": {
"projectResourceId": {
"type": "string"
},
"connectionName": {
"type": "string"
},
"apimResourceId": {
"type": "string"
},
"apiName": {
"type": "string"
},
"apimSubscriptionName": {
"type": "string",
"defaultValue": "master"
},
"authType": {
"type": "string",
"defaultValue": "ApiKey"
},
"isSharedToAll": {
"type": "bool",
"defaultValue": false
},
"metadata": {
"type": "object"
}
},
"variables": {
"aiFoundryName": "[split(parameters('projectResourceId'), '/')[8]]",
"projectName": "[split(parameters('projectResourceId'), '/')[10]]",
"apimSubscriptionId": "[split(parameters('apimResourceId'), '/')[2]]",
"apimResourceGroupName": "[split(parameters('apimResourceId'), '/')[4]]",
"apimServiceName": "[split(parameters('apimResourceId'), '/')[8]]"
},
"resources": [
{
"condition": "[equals(parameters('authType'), 'ApiKey')]",
"type": "Microsoft.CognitiveServices/accounts/projects/connections",
"apiVersion": "2025-04-01-preview",
"name": "[format('{0}/{1}/{2}', variables('aiFoundryName'), variables('projectName'), parameters('connectionName'))]",
"properties": {
"category": "ApiManagement",
"target": "[format('{0}/{1}', reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', variables('apimSubscriptionId'), variables('apimResourceGroupName')), 'Microsoft.ApiManagement/service', variables('apimServiceName')), '2021-08-01').gatewayUrl, reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', variables('apimSubscriptionId'), variables('apimResourceGroupName')), 'Microsoft.ApiManagement/service/apis', variables('apimServiceName'), parameters('apiName')), '2021-08-01').path)]",
"authType": "ApiKey",
"isSharedToAll": "[parameters('isSharedToAll')]",
"credentials": {
"key": "[listSecrets(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', variables('apimSubscriptionId'), variables('apimResourceGroupName')), 'Microsoft.ApiManagement/service/subscriptions', variables('apimServiceName'), parameters('apimSubscriptionName')), '2021-08-01').primaryKey]"
},
"metadata": "[parameters('metadata')]"
}
}
],
"outputs": {
"connectionName": {
"type": "string",
"value": "[if(equals(parameters('authType'), 'ApiKey'), parameters('connectionName'), '')]"
},
"connectionId": {
"type": "string",
"value": "[if(equals(parameters('authType'), 'ApiKey'), resourceId('Microsoft.CognitiveServices/accounts/projects/connections', variables('aiFoundryName'), variables('projectName'), parameters('connectionName')), '')]"
},
"targetUrl": {
"type": "string",
"value": "[format('{0}/{1}', reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', variables('apimSubscriptionId'), variables('apimResourceGroupName')), 'Microsoft.ApiManagement/service', variables('apimServiceName')), '2021-08-01').gatewayUrl, reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', variables('apimSubscriptionId'), variables('apimResourceGroupName')), 'Microsoft.ApiManagement/service/apis', variables('apimServiceName'), parameters('apiName')), '2021-08-01').path)]"
},
"authType": {
"type": "string",
"value": "[parameters('authType')]"
},
"metadata": {
"type": "object",
"value": "[parameters('metadata')]"
}
}
}
}
}
],
"outputs": {
"connectionName": {
"type": "string",
"value": "[reference(resourceId('Microsoft.Resources/deployments', 'apim-connection-example1'), '2022-09-01').outputs.connectionName.value]"
},
"connectionId": {
"type": "string",
"value": "[reference(resourceId('Microsoft.Resources/deployments', 'apim-connection-example1'), '2022-09-01').outputs.connectionId.value]"
},
"targetUrl": {
"type": "string",
"value": "[reference(resourceId('Microsoft.Resources/deployments', 'apim-connection-example1'), '2022-09-01').outputs.targetUrl.value]"
},
"authType": {
"type": "string",
"value": "[reference(resourceId('Microsoft.Resources/deployments', 'apim-connection-example1'), '2022-09-01').outputs.authType.value]"
},
"metadata": {
"type": "object",
"value": "[reference(resourceId('Microsoft.Resources/deployments', 'apim-connection-example1'), '2022-09-01').outputs.metadata.value]"
}
}
}
Loading