使用範本在 Azure VM 上設定 Azure 資源的受控識別

Azure 資源的受控識別是 Microsoft Entra ID 的功能。 每個支援適用於 Azure 資源的受控識別 Azure 服務均受限於其本身的時間表。 開始之前,請務必檢閱 資源受控識別的可用性 狀態和 已知問題

Azure 資源受控識別會在 Microsoft Entra ID 中為 Azure 服務提供自動受控識別。 您可以使用此身分識別來向任何支援 Microsoft Entra 驗證的服務進行驗證,不需要任何您程式碼中的認證。

在本文中,您會使用 Azure Resource Manager 部署範本,瞭解如何在 Azure VM 上針對 Azure 資源作業執行下列受控識別:

必要條件

Azure 資源管理員範本

如同Azure 入口網站和腳本, Azure Resource Manager 範本可讓您部署 Azure 資源群組所定義的新或修改的資源。 有數個選項可用於範本編輯和部署,包括本機和入口網站型:

無論您選擇的選項為何,在初始部署和重新部署期間,範本語法都相同。 在新的或現有的 VM 上啟用系統或使用者指派的受控識別,會以相同方式完成。 此外,根據預設,Azure Resource Manager 會 對部署執行累加式更新

系統指派的受控識別

在本節中,您將使用 Azure Resource Manager 範本來啟用和停用系統指派的受控識別。

在建立 Azure VM 或現有 VM 期間啟用系統指派的受控識別

若要在 VM 上啟用系統指派的受控識別,您的帳戶需要 虛擬機器參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 無論您是在本機或透過Azure 入口網站登入 Azure,請使用與包含 VM 的 Azure 訂用帳戶相關聯的帳戶。

  2. 若要啟用系統指派的受控識別,請將範本載入編輯器中,找出 Microsoft.Compute/virtualMachines 區段內 resources 感興趣的資源,並將 屬性新增 "identity" 至與 屬性相同的層級 "type": "Microsoft.Compute/virtualMachines" 。 使用下列語法:

    "identity": {
        "type": "SystemAssigned"
    },
    
  3. 當您完成時,應該將下列區段新增至 resource 範本的 區段,其應該如下所示:

     "resources": [
         {
             //other resource provider properties...
             "apiVersion": "2018-06-01",
             "type": "Microsoft.Compute/virtualMachines",
             "name": "[variables('vmName')]",
             "location": "[resourceGroup().location]",
             "identity": {
                 "type": "SystemAssigned",
                 }                        
         }
     ]
    

指派 VM 系統指派受控識別的角色

在 VM 上啟用系統指派的受控識別之後,您可能會想要將角色授與它,例如 讀取者 存取建立所在的資源群組。 您可以在使用 Azure Resource Manager 範本 指派 Azure 角色一文中找到 詳細資訊,以協助您完成此步驟。

從 Azure VM 停用系統指派的受控識別

若要從 VM 移除系統指派的受控識別,您的帳戶需要 虛擬機器參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 無論您是在本機或透過Azure 入口網站登入 Azure,請使用與包含 VM 的 Azure 訂用帳戶相關聯的帳戶。

  2. 將範本 載入編輯器 ,並在 區段中找出 Microsoft.Compute/virtualMachines 感興趣的 resources 資源。 如果您的 VM 只有系統指派的受控識別,您可以將身分識別類型變更為 None 來停用它。

    Microsoft.Compute/virtualMachines API 2018-06-01 版

    如果您的 VM 同時具有系統與使用者指派的受控識別,請從身分識別類型中移除 SystemAssigned ,並保留 UserAssigneduserAssignedIdentities 字典值。

    Microsoft.Compute/virtualMachines API 2018-06-01 版

    apiVersion如果您的 是 2017-12-01 且您的 VM 同時具有系統與使用者指派的受控識別,請從身分識別類型中移除 SystemAssigned ,並保留 UserAssignedidentityIds 使用者指派的受控識別陣列。

下列範例示範如何從沒有使用者指派受控識別的 VM 中移除系統指派的受控識別:

{
    "apiVersion": "2018-06-01",
    "type": "Microsoft.Compute/virtualMachines",
    "name": "[parameters('vmName')]",
    "location": "[resourceGroup().location]",
    "identity": {
        "type": "None"
    }
}

使用者指派的受控識別

在本節中,您會使用 Azure Resource Manager 範本,將使用者指派的受控識別指派給 Azure VM。

注意

若要使用 Azure Resource Manager 範本建立使用者指派的受控識別,請參閱 建立使用者指派的受控識別

將使用者指派的受控識別指派給 Azure VM

若要將使用者指派的身分識別指派給 VM,您的帳戶需要 受控識別操作員 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 在 元素底 resources 下,新增下列專案,將使用者指派的受控識別指派給您的 VM。 請務必將 取代 <USERASSIGNEDIDENTITY> 為您建立的使用者指派受控識別名稱。

    Microsoft.Compute/virtualMachines API 2018-06-01 版

    apiVersion如果您的 是 2018-06-01 ,則使用者指派的受控識別會以字典格式儲存 userAssignedIdentities ,而且 <USERASSIGNEDIDENTITYNAME> 值必須儲存在範本 區段中定義的 variables 變數中。

     {
         "apiVersion": "2018-06-01",
         "type": "Microsoft.Compute/virtualMachines",
         "name": "[variables('vmName')]",
         "location": "[resourceGroup().location]",
         "identity": {
             "type": "userAssigned",
             "userAssignedIdentities": {
                 "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]": {}
             }
         }
     }
    

    Microsoft.Compute/virtualMachines API 2017-12-01 版

    apiVersion如果您的 是 2017-12-01 ,則使用者指派的受控識別會儲存在陣列中 identityIds ,而且 <USERASSIGNEDIDENTITYNAME> 值必須儲存在範本區段中定義的 variables 變數中。

    {
        "apiVersion": "2017-12-01",
        "type": "Microsoft.Compute/virtualMachines",
        "name": "[variables('vmName')]",
        "location": "[resourceGroup().location]",
        "identity": {
            "type": "userAssigned",
            "identityIds": [
                "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]"
            ]
        }
    }
    
  2. 當您完成時,應該將下列區段新增至 resource 範本的 區段,其應該如下所示:

    Microsoft.Compute/virtualMachines API 2018-06-01 版

      "resources": [
         {
             //other resource provider properties...
             "apiVersion": "2018-06-01",
             "type": "Microsoft.Compute/virtualMachines",
             "name": "[variables('vmName')]",
             "location": "[resourceGroup().location]",
             "identity": {
                 "type": "userAssigned",
                 "userAssignedIdentities": {
                    "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]": {}
                 }
             }
         }
     ] 
    

    Microsoft.Compute/virtualMachines API 2017-12-01 版

    "resources": [
         {
             //other resource provider properties...
             "apiVersion": "2017-12-01",
             "type": "Microsoft.Compute/virtualMachines",
             "name": "[variables('vmName')]",
             "location": "[resourceGroup().location]",
             "identity": {
                 "type": "userAssigned",
                 "identityIds": [
                    "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]"
                 ]
             }
         }
    ]
    

從 Azure VM 移除使用者指派的受控識別

若要從 VM 移除使用者指派的身分識別,您的帳戶需要 虛擬機器參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 無論您是在本機或透過Azure 入口網站登入 Azure,請使用與包含 VM 的 Azure 訂用帳戶相關聯的帳戶。

  2. 將範本 載入編輯器 ,並在 區段中找出 Microsoft.Compute/virtualMachines 感興趣的 resources 資源。 如果您有只有使用者指派受控識別的 VM,您可以將身分識別類型變更為 None 來停用它。

    下列範例示範如何從沒有系統指派受控識別的 VM 中移除所有使用者指派的受控識別:

     {
       "apiVersion": "2018-06-01",
       "type": "Microsoft.Compute/virtualMachines",
       "name": "[parameters('vmName')]",
       "location": "[resourceGroup().location]",
       "identity": {
           "type": "None"
           },
     }
    

    Microsoft.Compute/virtualMachines API 2018-06-01 版

    若要從 VM 移除單一使用者指派的受控識別,請從 useraAssignedIdentities 字典中移除它。

    如果您有系統指派的受控識別,請將它保留在 type 值下方的值 identity 中。

    Microsoft.Compute/virtualMachines API 2017-12-01 版

    若要從 VM 移除單一使用者指派的受控識別,請從 identityIds 陣列中移除它。

    如果您有系統指派的受控識別,請將它保留在 type 值下方的值 identity 中。

下一步