訓練
認證
Microsoft Certified: Azure Fundamentals - Certifications
示範雲端概念、核心 Azure 服務和 Azure 管理,以及治理功能與工具的基本概念。
在本快速入門中,您會使用 Terraform 建立 Azure 資源群組和機密總帳。 Azure 機密總帳是完全受控的服務,可提供防竄改登錄來儲存敏感數據。 其建置在 Azure 機密運算之上,其會使用硬體式信任的執行環境來保護數據,即使數據正在使用中也不受威脅影響。 此總賬的設計目的是要維護其所儲存數據的機密性和完整性,因此非常適合需要高層級數據保護的使用案例。 在此腳本中建立的資源包括 Azure 機密總帳和 Azure 資源群組。
Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,您可以使用 HCL 語法來建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立設定檔之後,您可以建立執行計畫,讓您先預覽基礎結構變更,之後再部署。 驗證變更之後,您可以套用執行計畫來部署基礎結構。
建立具有有效訂閱的 Azure 帳戶。 您可以免費建立帳戶。
注意
本文中的範例程式碼位於 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 init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。
terraform init -upgrade
重點︰
-upgrade
參數會將必要的提供者外掛程式升級至符合設定版本條件約束的最新版本。執行 terraform plan 以建立執行計畫。
terraform plan -out main.tfplan
重點︰
terraform plan
命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。-out
參數可讓您指定計畫的輸出檔。 使用 -out
參數可確保您所檢閱的方案就是所套用的方案。執行 terraform apply 將執行計畫套用至您的雲端基礎結構。
terraform apply main.tfplan
重點︰
terraform apply
命令假設您之前已執行過 terraform plan -out main.tfplan
。-out
參數指定了不同的檔案名稱,請在呼叫 terraform apply
時使用該檔案名稱。-out
參數,請呼叫 terraform apply
,不需要使用參數。執行 az confidential-ledger show
以檢視 Azure 機密總帳。
az confidentialledger show --ledger-name <ledger_name> --resource-group <resource_group_name>
您必須以 Azure 機密總帳的名稱取代 ,並以<resource_group_name>
您的資源群組名稱取代 <ledger_name>
。
當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:
執行 terraform plan 並指定 destroy
旗標。
terraform plan -destroy -out main.destroy.tfplan
重點︰
terraform plan
命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。-out
參數可讓您指定計畫的輸出檔。 使用 -out
參數可確保您所檢閱的方案就是所套用的方案。執行 terraform apply 以套用執行方案。
terraform apply main.destroy.tfplan
訓練
認證
Microsoft Certified: Azure Fundamentals - Certifications
示範雲端概念、核心 Azure 服務和 Azure 管理,以及治理功能與工具的基本概念。
文件
在 Azure 機密總帳中管理 Microsoft Entra 權杖型使用者
了解如何在 Azure 機密總帳中管理 Microsoft Entra 權杖型使用者
使用 Azure Resource Manager 範本建立 Microsoft Azure 機密總帳
了解如何使用 Azure Resource Manager 範本建立 Microsoft Azure 機密總帳。
Azure 機密總帳寫入交易收據