在本快速入門中,您會使用 Terraform 建立 Azure 資源群組和機密總帳。 Azure 機密總帳是完全受控的服務,可提供防竄改登錄來儲存敏感數據。 其建置在 Azure 機密運算之上,其會使用硬體式信任的執行環境來保護數據,即使數據正在使用中也不受威脅影響。 此總賬的設計目的是要維護其所儲存數據的機密性和完整性,因此非常適合需要高層級數據保護的使用案例。 在此腳本中建立的資源包括 Azure 機密總帳和 Azure 資源群組。
Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,您可以使用 HCL 語法來建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立設定檔之後,您可以建立執行計畫,讓您先預覽基礎結構變更,之後再部署。 驗證變更之後,您可以套用執行計畫來部署基礎結構。
- 產生資源群組的隨機寵物名稱。
- 使用產生的名稱建立 Azure 資源群組。
- 擷取目前的 Azure 用戶端組態。
- 產生 Azure 機密總賬名稱的隨機字串。
- 使用產生的名稱建立 Azure 機密總帳,並將它指派給資源群組。
- 將 Azure AD 型服務主體指派給機密總帳。
- 將機密總帳標記為範例。
- 輸出資源組名、機密總賬名稱、機密總帳類型和機密總帳角色名稱。
- 指定必要的 Terraform 版本和必要的提供者。
- 定義沒有其他功能的 Azure 提供者。
- 定義資源組名前置詞、資源組名、機密總賬名稱、機密總賬類型,以及機密總賬角色名稱的變數。
必要條件
建立具有有效訂閱的 Azure 帳戶。 您可以免費建立帳戶。
實作 Terraform 程式碼
注意
本文中的範例程式碼位於 Azure Terraform GitHub 存放庫。 您可以檢視內含目前和舊版 Terraform 測試結果的記錄檔。
建立目錄,然後在目錄中測試並執行範例 Terraform 程式碼,且設為目前的目錄。
建立名為
main.tf
的檔案,並插入下列程序代碼:resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "azurerm_resource_group" "rg" { location = var.resource_group_location name = random_pet.rg_name.id } data "azurerm_client_config" "current" { } resource "random_string" "azurerm_confidential_ledger_name" { length = 13 lower = true numeric = false special = false upper = false } resource "azurerm_confidential_ledger" "example" { name = coalesce(var.confidential_ledger_name, "ledger-${random_string.azurerm_confidential_ledger_name.result}") resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location ledger_type = var.confidential_ledger_type azuread_based_service_principal { principal_id = data.azurerm_client_config.current.object_id tenant_id = data.azurerm_client_config.current.tenant_id ledger_role_name = var.confidential_ledger_role_name } tags = { IsExample = "True" } }
建立名為
outputs.tf
的檔案,並插入下列程序代碼:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "confidential_ledger_name" { value = azurerm_confidential_ledger.example.name } output "confidential_ledger_type" { value = azurerm_confidential_ledger.example.ledger_type } output "confidential_ledger_role_name" { value = azurerm_confidential_ledger.example.azuread_based_service_principal[0].ledger_role_name }
建立名為
providers.tf
的檔案,並插入下列程序代碼:terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
建立名為
variables.tf
的檔案,並插入下列程序代碼:variable "resource_group_name_prefix" { type = string default = "rg" description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." } variable "resource_group_location" { type = string default = "eastus" description = "Location of the resource group." } variable "confidential_ledger_name" { type = string description = "The name of the confidential ledger resource. The value will be randomly generated if blank." default = "" } variable "confidential_ledger_type" { type = string default = "Public" validation { condition = contains(["Public", "Private"], var.confidential_ledger_type) error_message = "The confidential ledger type value must be one of the following: Public, Private." } description = "Type of the confidential ledger." } variable "confidential_ledger_role_name" { type = string default = "Administrator" description = "Role name for the confidential ledger." }
初始化 Terraform
執行 terraform init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。
terraform init -upgrade
重點︰
-
-upgrade
參數會將必要的提供者外掛程式升級至符合設定版本條件約束的最新版本。
建立 Terraform 執行計畫
執行 terraform plan 以建立執行計畫。
terraform plan -out main.tfplan
重點︰
-
terraform plan
命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。 - 選用的
-out
參數可讓您指定計畫的輸出檔。 使用-out
參數可確保您所檢閱的方案就是所套用的方案。
套用 Terraform 執行計畫
執行 terraform apply 將執行計畫套用至您的雲端基礎結構。
terraform apply main.tfplan
重點︰
- 範例
terraform apply
命令假設您之前已執行過terraform plan -out main.tfplan
。 - 如果您為
-out
參數指定了不同的檔案名稱,請在呼叫terraform apply
時使用該檔案名稱。 - 若您未使用
-out
參數,請呼叫terraform apply
,不需要使用參數。
驗證結果
Azure 命令列介面 (Azure CLI)
執行 az confidential-ledger show
以檢視 Azure 機密總帳。
az confidentialledger show --ledger-name <ledger_name> --resource-group <resource_group_name>
您必須以 Azure 機密總帳的名稱取代 ,並以<ledger_name>
您的資源群組名稱取代 <resource_group_name>
。
清除資源
當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:
執行 terraform plan 並指定
destroy
旗標。terraform plan -destroy -out main.destroy.tfplan
重點︰
-
terraform plan
命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。 - 選用的
-out
參數可讓您指定計畫的輸出檔。 使用-out
參數可確保您所檢閱的方案就是所套用的方案。
-
執行 terraform apply 以套用執行方案。
terraform apply main.destroy.tfplan
對 Azure 上的 Terraform 進行疑難排解
針對在 Azure 上使用 Terraform 時的常見問題進行疑難排解。