Share via


快速入門:使用 Terraform 建立原則指派,以識別不符合規範的資源

了解 Azure 中合規性的第一個步驟是識別您資源的狀態。 本快速入門會逐步引導您完成建立原則指派的程序,以識別未使用受控磁碟的虛擬機器。

在此程序結束時,您將會成功識別出未在跨訂用帳戶中使用受控磁碟的虛擬機器。 它們「不符合」原則指派的規範。

必要條件

建立 Terraform 組態、變數和輸出檔案

在本快速入門中,您會建立一個原則指派,並且指派稽核沒有受控磁碟的虛擬機器 (06a78e20-9358-41c9-923c-fb736d382a4d) 定義。 此原則定義會識別與原則定義中設定之條件不相符的資源。

首先,設定 Terraform 組態、變數和輸出檔案。 Azure 原則的 Terraform 資源會使用 Azure 提供者

  1. 建立名為 policy-assignment 的新資料夾,並將目錄變更到其中。

  2. 使用下列程式碼建立 main.tf

    注意

    若要在管理群組建立原則指派,請使用 azurerm_management_group_policy_assignment 資源,資源群組請使用 azurerm_resource_group_policy_assignment ,而訂用帳戶則使用 azurerm_subscription_policy_assignment 資源。

      provider "azurerm" {
        features {}
      }
    
      terraform {
      required_providers {
          azurerm = {
              source = "hashicorp/azurerm"
              version = ">= 2.96.0"
          }
      }
      }
    
      resource "azurerm_subscription_policy_assignment" "auditvms" {
      name = "audit-vm-manageddisks"
      subscription_id = var.cust_scope
      policy_definition_id = "/providers/Microsoft.Authorization/policyDefinitions/06a78e20-9358-41c9-923c-fb736d382a4d"
      description = "Shows all virtual machines not using managed disks"
      display_name = "Audit VMs without managed disks assignment"
      }
    
  3. 使用下列程式碼建立 variables.tf

    variable "cust_scope" {
        default = "{scope}"
    }
    

    範圍會決定在哪些資源或資源群組上強制執行原則指派。 此範圍可包含管理群組到個別資源。 請務必根據宣告的資源將 {scope} 取代為下列其中一個模式:

    • 訂用帳戶:/subscriptions/{subscriptionId}
    • 資源群組:/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}
    • 資源:/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/[{parentResourcePath}/]
  4. 使用下列程式碼建立 output.tf

    output "assignment_id" {
        value = azurerm_subscription_policy_assignment.auditvms.id
    }
    

初始化 Terraform 並建立計畫

接下來,初始化 Terraform 以下載所需的提供者,然後建立計畫。

  1. 執行 terraform init 命令。 此命令會下載在 Terraform 組態中建立 Azure 資源所需的 Azure 模組。

    terraform init
    

    Screenshot of running the terraform init command that shows downloading the azurerm module and a success message.

  2. 使用適用於 Terraform 的 Azure CLI 進行驗證。 如需更多資訊,請參閱 Azure 提供者:使用 Azure CLI 進行驗證

    az login
    
  3. 使用 terraform plan 命令和 out 參數來建立執行計畫。

    terraform plan -out assignment.tfplan
    

    Screenshot of running the terraform plan command and out parameter to show the Azure resource that would be created.

    注意

    如需保存執行計畫和安全性的詳細資訊,請參閱 Terraform 計畫:安全性警告

套用 Terraform 執行計畫

最後,套用執行計畫。

執行 terraform apply 命令並指定已建立的 assignment.tfplan

terraform apply assignment.tfplan

Screenshot of running the terraform apply command and the resulting resource creation.

出現「套用完成! 資源: 1 個已新增、0 個已變更、0 個已終結。」訊息,原則指派現在已建立。 因為定義了 outputs.tf 檔案,所以也會傳回 assignment_id

識別不符合規範的資源

若要檢視這個新指派下不符合規範的資源,請使用 terraform apply 傳回的 assignment_id。 藉此,執行下列命令,以取得不合規資源的資源識別碼,而這些識別碼會輸出到 JSON 檔案中:

armclient post "/subscriptions/<subscriptionID>/providers/Microsoft.PolicyInsights/policyStates/latest/queryResults?api-version=2019-10-01&$filter=IsCompliant eq false and PolicyAssignmentId eq '<policyAssignmentID>'&$apply=groupby((ResourceId))" > <json file to direct the output with the resource IDs into>

您的結果類似下列範例:

{
    "@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest",
    "@odata.count": 3,
    "value": [{
            "@odata.id": null,
            "@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest/$entity",
            "ResourceId": "/subscriptions/<subscriptionId>/resourcegroups/<rgname>/providers/microsoft.compute/virtualmachines/<virtualmachineId>"
        },
        {
            "@odata.id": null,
            "@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest/$entity",
            "ResourceId": "/subscriptions/<subscriptionId>/resourcegroups/<rgname>/providers/microsoft.compute/virtualmachines/<virtualmachine2Id>"
        },
        {
            "@odata.id": null,
            "@odata.context": "https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest/$entity",
            "ResourceId": "/subscriptions/<subscriptionName>/resourcegroups/<rgname>/providers/microsoft.compute/virtualmachines/<virtualmachine3ID>"
        }

    ]
}

結果類似於您通常在 Azure 入口網站檢視中看到列在 [不符合規範的資源] 之下的內容。

清除資源

若要移除所建立的指派,請使用 Azure CLI 或以 terraform destroy 反轉 Terraform 執行計畫。

  • Azure CLI

    az policy assignment delete --name 'audit-vm-manageddisks' --scope '/subscriptions/<subscriptionID>/<resourceGroupName>'
    
  • Terraform

    terraform destroy
    

下一步

在這個快速入門中,您指派原則定義以識別 Azure 環境中的不相容資源。

若要深入了解指派原則,以驗證新資源是相容的,請繼續進行以下的教學課程: