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

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

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

在本文中,藉由使用 Azure Resource Manager 部署範本,您將了解如何在 Azure VM 上執行 Azure 資源作業的下列受控識別:

必要條件

Azure 資源管理員範本

如同 Azure 入口網站和指令碼,Azure Resource Manager 範本可讓您部署由 Azure 資源群組所定義的全新或已修改資源。 範本編輯和部署有幾個選項可用 (在本機和入口網站),包括:

不論您選擇的選項,在初始部署和重新部署期間,範本的語法都相同。 在新的或現有虛擬機器上啟用系統或使用者指派的受控識別,是以相同的方式來進行。 此外,根據預設,Azure Resource Manager 會對部署採取累加式更新

系統指派的受控識別

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

在建立 Azure VM 時,或在現有虛擬機器上啟用系統指派的受控識別

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

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

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

    "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",
                 }                        
         }
     ]
    

指派角色給虛擬機器的系統指派受控識別

在虛擬機器上啟用系統指派的受控識別後,建議您授與其這類角色:在角色建立所在資源群組的讀者存取權。 您可以在使用 Azure Resource Manager 範本指派 Azure 角色一文中找到詳細的資訊,以協助您進行此步驟。

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

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

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

  2. 編輯器中載入範本,然後在 resources 區段找出想要的 Microsoft.Compute/virtualMachines 資源。 若您的虛擬機器只有系統指派的受控識別,您可以將識別類型變更為 None 予以停用。

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

    如果虛擬機器同時具有系統與使用者指派的身分識別,請從受控識別類型中移除 SystemAssigned,並保留 UserAssigned 以及 userAssignedIdentities 字典值。

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

    apiVersion2017-12-01 且虛擬機器同時具有系統與使用者指派的受控識別,請從身分識別類型中移除 SystemAssigned,並保留 UserAssigned 以及使用者指派受控識別的 identityIds 陣列。

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

{
    "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,您的帳戶需要受控識別運算子角色指派。 不需要指派其他 Azure AD 目錄角色。

  1. resources 元素之下,新增下列項目,以將使用者指派的受控識別指派至您的虛擬機器。 請務必將 <USERASSIGNEDIDENTITY> 取代為您建立的使用者指派受控識別名稱。

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

    如果 apiVersion2018-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

    如果 apiVersion2017-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 中移除使用者指派的身分識別,您的帳戶需要虛擬機器參與者角色指派。 不需要指派其他 Azure AD 目錄角色。

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

  2. 編輯器中載入範本,然後在 resources 區段找出想要的 Microsoft.Compute/virtualMachines 資源。 若您的虛擬機器只有使用者指派的受控識別,您可以將身分識別類型變更為 None 予以停用。

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

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

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

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

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

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

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

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

後續步驟