|
| 1 | +@description('Name of the Key Vault') |
| 2 | +param keyVaultName string |
| 3 | + |
| 4 | +@description('Azure region for the Key Vault') |
| 5 | +param location string |
| 6 | + |
| 7 | +@description('Principal IDs of managed identities to grant secret read access') |
| 8 | +param readerPrincipalIds array = [] |
| 9 | + |
| 10 | +resource keyVault 'Microsoft.KeyVault/vaults@2023-07-01' = { |
| 11 | + name: keyVaultName |
| 12 | + location: location |
| 13 | + properties: { |
| 14 | + sku: { |
| 15 | + family: 'A' |
| 16 | + name: 'standard' |
| 17 | + } |
| 18 | + tenantId: subscription().tenantId |
| 19 | + enableRbacAuthorization: true |
| 20 | + enableSoftDelete: true |
| 21 | + softDeleteRetentionInDays: 7 |
| 22 | + enabledForDeployment: false |
| 23 | + enabledForTemplateDeployment: false |
| 24 | + enabledForDiskEncryption: false |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// Grant each managed identity the Key Vault Secrets User role (read secrets) |
| 29 | +@batchSize(1) |
| 30 | +resource secretsUserRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = [for (principalId, i) in readerPrincipalIds: { |
| 31 | + // Role assignment scope must be the vault resource |
| 32 | + scope: keyVault |
| 33 | + // Deterministic GUID: vaultId + principalId |
| 34 | + name: guid(keyVault.id, principalId, '4633458b-17de-408a-b874-0445c86b69e6') |
| 35 | + properties: { |
| 36 | + // Key Vault Secrets User built-in role |
| 37 | + roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '4633458b-17de-408a-b874-0445c86b69e6') |
| 38 | + principalId: principalId |
| 39 | + principalType: 'ServicePrincipal' |
| 40 | + } |
| 41 | +}] |
| 42 | + |
| 43 | +output id string = keyVault.id |
| 44 | +output name string = keyVault.name |
| 45 | +output uri string = keyVault.properties.vaultUri |
0 commit comments