azure-resources/key-vault/keyVault.json

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.47.16.16243",
      "templateHash": "9835955146811098196"
    }
  },
  "parameters": {
    "keyVaultName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 24,
      "metadata": {
        "description": "The name of the Key Vault (3-24 letters, digits and hyphens, globally unique)"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Azure region of a newly created Key Vault"
      }
    },
    "deployKeyVault": {
      "type": "bool",
      "defaultValue": true,
      "metadata": {
        "description": "Whether to create the Key Vault (set to false to only assign the roles on an existing vault)"
      }
    },
    "servicePrincipalId": {
      "type": "string",
      "metadata": {
        "description": "The object ID of the RealmJoin Client service principal"
      }
    },
    "roleDefinitionIds": {
      "type": "array",
      "metadata": {
        "description": "The role definition IDs to assign to the RealmJoin Client service principal (only roles not assigned yet)"
      }
    }
  },
  "resources": [
    {
      "condition": "[parameters('deployKeyVault')]",
      "type": "Microsoft.KeyVault/vaults",
      "apiVersion": "2026-05-15",
      "name": "[parameters('keyVaultName')]",
      "location": "[parameters('location')]",
      "properties": {
        "tenantId": "[subscription().tenantId]",
        "sku": {
          "family": "A",
          "name": "standard"
        },
        "enableRbacAuthorization": true
      }
    },
    {
      "copy": {
        "name": "clientRoleAssignments",
        "count": "[length(parameters('roleDefinitionIds'))]"
      },
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2022-04-01",
      "scope": "[resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))]",
      "name": "[guid(resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName')), parameters('servicePrincipalId'), parameters('roleDefinitionIds')[copyIndex()])]",
      "properties": {
        "principalId": "[parameters('servicePrincipalId')]",
        "principalType": "ServicePrincipal",
        "roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', parameters('roleDefinitionIds')[copyIndex()])]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))]"
      ]
    }
  ],
  "outputs": {
    "keyVaultId": {
      "type": "string",
      "metadata": {
        "description": "The resource ID of the Key Vault"
      },
      "value": "[resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))]"
    },
    "keyVaultName": {
      "type": "string",
      "metadata": {
        "description": "The name of the Key Vault"
      },
      "value": "[parameters('keyVaultName')]"
    },
    "vaultUri": {
      "type": "string",
      "metadata": {
        "description": "The URI of the Key Vault"
      },
      "value": "[reference(resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName')), '2026-05-15').vaultUri]"
    }
  }
}