|
| 1 | +// Run command examples: |
| 2 | +// For existing storage account: az deployment group create --name AzDeploy --resource-group {RESOURCE_GROUP} --template-file main.bicep --parameters aiFoundryName={AI_FOUNDRY_NAME} userOwnedStorageResourceId={STORAGE_ACCOUNT_RESOURCE_ID} location={LOCATION} |
| 3 | +// For new storage account: az deployment group create --name AzDeploy --resource-group {RESOURCE_GROUP} --template-file main.bicep --parameters aiFoundryName={AI_FOUNDRY_NAME} location={LOCATION} |
| 4 | + |
| 5 | +@description('Name of the Azure AI Foundry account') |
| 6 | +param aiFoundryName string |
| 7 | + |
| 8 | +@description('Location for the resource') |
| 9 | +param location string = resourceGroup().location |
| 10 | + |
| 11 | +// Storage Account Parameters |
| 12 | +@description('Azure storage resource ID (leave empty to create a new storage account)') |
| 13 | +param userOwnedStorageResourceId string = '' |
| 14 | + |
| 15 | +@description('Name of the Storage Account derived from the resource ID or provided for new account') |
| 16 | +param storageAccountName string = userOwnedStorageResourceId != '' ? last(split(userOwnedStorageResourceId, '/')) : 'st${uniqueString(subscription().subscriptionId, resourceGroup().name)}aaviles' // no '-' allowed |
| 17 | + |
| 18 | +@description('Resource group name of the Storage Account derived from the resource ID or same as AI Foundry for new accounts') |
| 19 | +param storageResourceGroupName string = userOwnedStorageResourceId != '' ? split(userOwnedStorageResourceId, '/')[4] : resourceGroup().name |
| 20 | + |
| 21 | +// Derived values for cross-subscription support |
| 22 | +var targetStorageSubscriptionId = userOwnedStorageResourceId != '' ? split(userOwnedStorageResourceId, '/')[2] : subscription().subscriptionId |
| 23 | +// -------------------------------- |
| 24 | + |
| 25 | +// ==================================================================== |
| 26 | +// DEPLOYMENT STEPS OVERVIEW: |
| 27 | +// 1. Storage Account Configuration (existing vs new) |
| 28 | +// 2. Create Storage Account (if new - always in same RG as AI Foundry) |
| 29 | +// 3. Create AI Foundry with User Owned Storage |
| 30 | +// 4. Create Role Assignment for AI Foundry's managed identity on the Storage Account |
| 31 | +// ==================================================================== |
| 32 | + |
| 33 | +// ==================================================================== |
| 34 | +// STEP 1: STORAGE ACCOUNT CONFIGURATION |
| 35 | +// ==================================================================== |
| 36 | +@description('Automatically determined based on whether userOwnedStorageResourceId is provided') |
| 37 | +var useExistingStorageAccount bool = userOwnedStorageResourceId != '' |
| 38 | + |
| 39 | +// ==================================================================== |
| 40 | +// STEP 2: CREATE STORAGE ACCOUNT (IF NOT USING EXISTING ONE) |
| 41 | +// ==================================================================== |
| 42 | +// New storage accounts are always created in the same resource group as AI Foundry |
| 43 | +// For cross-subscription existing storage, we use the target subscription for role assignment |
| 44 | + |
| 45 | +// For cross-subscription storage account creation (not recommended, but supported) |
| 46 | +module storageAccountModule './modules/storageAccount.bicep' = if (!useExistingStorageAccount && targetStorageSubscriptionId != subscription().subscriptionId) { |
| 47 | + name: 'crossSubStorageAccount' |
| 48 | + scope: resourceGroup(targetStorageSubscriptionId, resourceGroup().name) |
| 49 | + params: { |
| 50 | + storageAccountName: storageAccountName |
| 51 | + location: location |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// For same-subscription storage account creation (recommended) |
| 56 | +resource storageAccount 'Microsoft.Storage/storageAccounts@2025-01-01' = if (!useExistingStorageAccount && targetStorageSubscriptionId == subscription().subscriptionId) { |
| 57 | + name: storageAccountName |
| 58 | + location: location |
| 59 | + sku: { |
| 60 | + name: 'Standard_RAGRS' |
| 61 | + } |
| 62 | + kind: 'StorageV2' |
| 63 | + properties: { |
| 64 | + // For Azure AI Foundry BYOS, these settings are required |
| 65 | + allowBlobPublicAccess: false |
| 66 | + allowSharedKeyAccess: true // Must be true for AI Foundry to access |
| 67 | + minimumTlsVersion: 'TLS1_2' |
| 68 | + supportsHttpsTrafficOnly: true |
| 69 | + encryption: { |
| 70 | + services: { |
| 71 | + blob: { |
| 72 | + enabled: true |
| 73 | + } |
| 74 | + file: { |
| 75 | + enabled: true |
| 76 | + } |
| 77 | + } |
| 78 | + keySource: 'Microsoft.Storage' |
| 79 | + } |
| 80 | + // Required for AI Foundry BYOS |
| 81 | + allowCrossTenantReplication: false |
| 82 | + defaultToOAuthAuthentication: false |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// ==================================================================== |
| 87 | +// STEP 3: CREATE AI FOUNDRY WITH USER OWNED STORAGE |
| 88 | +// ==================================================================== |
| 89 | +resource aiFoundry 'Microsoft.CognitiveServices/accounts@2025-07-01-preview' = { |
| 90 | + name: aiFoundryName |
| 91 | + location: location |
| 92 | + sku: { |
| 93 | + name: 'S0' |
| 94 | + } |
| 95 | + kind: 'AIServices' |
| 96 | + identity: { |
| 97 | + type: 'SystemAssigned' |
| 98 | + } |
| 99 | + properties: { |
| 100 | + allowProjectManagement: true |
| 101 | + publicNetworkAccess: 'Enabled' // or 'Disabled' |
| 102 | + disableLocalAuth: false |
| 103 | + customSubDomainName: toLower(replace(aiFoundryName, '_', '-')) |
| 104 | + userOwnedStorage: [{ |
| 105 | + resourceId: useExistingStorageAccount ? userOwnedStorageResourceId : '/subscriptions/${targetStorageSubscriptionId}/resourceGroups/${resourceGroup().name}/providers/Microsoft.Storage/storageAccounts/${storageAccountName}' |
| 106 | + }] |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// ==================================================================== |
| 111 | +// STEP 4: CREATE ROLE ASSIGNMENT FOR AI FOUNDRY'S MANAGED IDENTITY |
| 112 | +// ==================================================================== |
| 113 | +// Role assignment deployed to the correct subscription/resource group based on storage location |
| 114 | +module roleAssignmentModule './modules/roleAssignment.bicep' = { |
| 115 | + name: 'storageRoleAssignment' |
| 116 | + scope: resourceGroup(targetStorageSubscriptionId, storageResourceGroupName) |
| 117 | + params: { |
| 118 | + storageAccountName: storageAccountName |
| 119 | + principalId: aiFoundry.identity.principalId |
| 120 | + aiFoundryResourceId: aiFoundry.id |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// ==================================================================== |
| 125 | +// OUTPUTS |
| 126 | +// ==================================================================== |
| 127 | +@description('The resource ID of the AI Foundry account') |
| 128 | +output aiFoundryId string = aiFoundry.id |
| 129 | + |
| 130 | +@description('The name of the AI Foundry account') |
| 131 | +output aiFoundryName string = aiFoundry.name |
| 132 | + |
| 133 | +@description('The resource ID of the storage account being used') |
| 134 | +output storageAccountId string = useExistingStorageAccount ? userOwnedStorageResourceId : '/subscriptions/${targetStorageSubscriptionId}/resourceGroups/${resourceGroup().name}/providers/Microsoft.Storage/storageAccounts/${storageAccountName}' |
| 135 | + |
| 136 | +@description('The managed identity principal ID of the AI Foundry account') |
| 137 | +output aiFoundryPrincipalId string = aiFoundry.identity.principalId |
| 138 | + |
| 139 | +@description('The subscription ID where the storage account is located') |
| 140 | +output storageSubscriptionId string = targetStorageSubscriptionId |
| 141 | + |
| 142 | +@description('Status of role assignment') |
| 143 | +output roleAssignmentStatus string = 'Storage Blob Data Contributor role assigned automatically via module' |
0 commit comments