|
| 1 | +// ========== existing-ai-services-roles.bicep ========== // |
| 2 | +// Module to assign RBAC roles to managed identity on an existing AI Services account |
| 3 | +// This is required when reusing an existing AI Foundry project from a different resource group |
| 4 | + |
| 5 | +@description('Required. The principal ID of the managed identity to grant access.') |
| 6 | +param principalId string |
| 7 | + |
| 8 | +@description('Required. The name of the existing AI Services account.') |
| 9 | +param aiServicesName string |
| 10 | + |
| 11 | +@description('Optional. The name of the existing AI Project.') |
| 12 | +param aiProjectName string = '' |
| 13 | + |
| 14 | +@description('Optional. The principal type of the identity.') |
| 15 | +@allowed([ |
| 16 | + 'Device' |
| 17 | + 'ForeignGroup' |
| 18 | + 'Group' |
| 19 | + 'ServicePrincipal' |
| 20 | + 'User' |
| 21 | +]) |
| 22 | +param principalType string = 'ServicePrincipal' |
| 23 | + |
| 24 | +// ========== Role Definitions ========== // |
| 25 | + |
| 26 | +// Azure AI User role - for AI Foundry project access (used by AIProjectClient for image generation) |
| 27 | +resource azureAiUserRole 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = { |
| 28 | + name: '53ca6127-db72-4b80-b1b0-d745d6d5456d' |
| 29 | +} |
| 30 | + |
| 31 | +// Cognitive Services OpenAI User role - for chat completions (used by AzureOpenAIChatClient) |
| 32 | +resource cognitiveServicesOpenAiUserRole 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = { |
| 33 | + name: '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd' |
| 34 | +} |
| 35 | + |
| 36 | +// ========== Existing Resources ========== // |
| 37 | + |
| 38 | +// Reference the existing AI Services account |
| 39 | +resource existingAiServices 'Microsoft.CognitiveServices/accounts@2025-04-01-preview' existing = { |
| 40 | + name: aiServicesName |
| 41 | +} |
| 42 | + |
| 43 | +// Reference the existing AI Project (if provided) |
| 44 | +resource existingAiProject 'Microsoft.CognitiveServices/accounts/projects@2025-04-01-preview' existing = if (!empty(aiProjectName)) { |
| 45 | + name: aiProjectName |
| 46 | + parent: existingAiServices |
| 47 | +} |
| 48 | + |
| 49 | +// ========== Role Assignments ========== // |
| 50 | + |
| 51 | +// Azure AI User role assignment - same as reference accelerator |
| 52 | +// Required for AIProjectClient (used for image generation in Foundry mode) |
| 53 | +resource assignAzureAiUserRole 'Microsoft.Authorization/roleAssignments@2022-04-01' = { |
| 54 | + name: guid(existingAiServices.id, principalId, azureAiUserRole.id) |
| 55 | + scope: existingAiServices |
| 56 | + properties: { |
| 57 | + roleDefinitionId: azureAiUserRole.id |
| 58 | + principalId: principalId |
| 59 | + principalType: principalType |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Cognitive Services OpenAI User role assignment |
| 64 | +// Required for AzureOpenAIChatClient (used for chat completions) |
| 65 | +resource assignCognitiveServicesOpenAiUserRole 'Microsoft.Authorization/roleAssignments@2022-04-01' = { |
| 66 | + name: guid(existingAiServices.id, principalId, cognitiveServicesOpenAiUserRole.id) |
| 67 | + scope: existingAiServices |
| 68 | + properties: { |
| 69 | + roleDefinitionId: cognitiveServicesOpenAiUserRole.id |
| 70 | + principalId: principalId |
| 71 | + principalType: principalType |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// ========== Outputs ========== // |
| 76 | + |
| 77 | +@description('The resource ID of the existing AI Services account.') |
| 78 | +output aiServicesResourceId string = existingAiServices.id |
| 79 | + |
| 80 | +@description('The endpoint of the existing AI Services account.') |
| 81 | +output aiServicesEndpoint string = existingAiServices.properties.endpoint |
| 82 | + |
| 83 | +@description('The principal ID of the existing AI Project (if provided).') |
| 84 | +output aiProjectPrincipalId string = !empty(aiProjectName) ? existingAiProject.identity.principalId : '' |
0 commit comments