Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,你可以用 HCL 語法建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立組態檔之後,您會建立一個 執行計劃 ,讓您在部署基礎結構變更之前先預覽這些變更。 驗證變更之後,您可以套用執行計畫來部署基礎結構。
在本文中,你使用 azapi_resource_list 資料來源來列出Azure資源,並以 JMESPath 表達式來篩選結果。 你建立兩個儲存帳號,然後用 azapi_resource_list 來列出並提取它們的屬性。
- 建立一個資源群組和兩個 AzureRM 供應商的儲存帳號
- 用
azapi_resource_list來列出儲存帳號,並使用 JMESPath 擷取它們的名稱和位置
先決條件
- Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
設定 Terraform:如果您尚未這麼做,請使用下列其中一個選項來設定 Terraform:
當您使用Microsoft帳戶登入 Azure 入口網站時,會使用該帳戶的預設 Azure 訂用帳戶。
Terraform 會自動使用預設 Azure 訂用帳戶中的資訊進行驗證。
執行 az account show 以確認目前的Microsoft帳戶和 Azure 訂用帳戶。
az account show
您透過 Terraform 所做的任何變更都會顯示在顯示的 Azure 訂用帳戶上。 如果是您想要的,請略過本文的其餘部分。
瞭解 response_export_values
response_export_values 屬性決定要從 API 回應中擷取的內容,並使這些內容在資料來源的 output 屬性中可用。 它接受列表或地圖:
-
清單:指定要提取的 JSON 路徑。 用
["*"]來匯出完整的回應正文。 - 地圖:使用 JMESPath 表達式來過濾並重塑回應。 鍵是結果名稱,值則是 JMESPath 表達式。
當你需要擷取特定欄位或轉換列表回應時,會偏好映射形式,因為它能產生更乾淨、更實用的輸出值。
實作 Terraform 程式碼
建立目錄,然後在目錄中測試範例 Terraform 程式碼,並將其設為目前的目錄。
建立名為
providers.tf的檔案,並插入下列程式碼:terraform { required_providers { azapi = { source = "Azure/azapi" version = "~> 2.0" } azurerm = { source = "hashicorp/azurerm" version = "~> 4.0" } random = { source = "hashicorp/random" version = "~> 3.0" } } } provider "azurerm" { features {} } provider "azapi" {}建立名為
variables.tf的檔案,並插入下列程式碼:variable "resource_group_location" { type = string default = "eastus" description = "Location of the resource group." } variable "resource_group_name_prefix" { type = string default = "rg" description = "Prefix of the resource group name that's combined with a random value to create a unique name." }建立名為
main.tf的檔案,並插入下列程式碼:resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "random_string" "storage_suffix" { length = 8 upper = false special = false } resource "azurerm_resource_group" "example" { location = var.resource_group_location name = random_pet.rg_name.id } resource "azurerm_storage_account" "example" { count = 2 name = "st${random_string.storage_suffix.result}${count.index}" resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location account_tier = "Standard" account_replication_type = "LRS" }
執行 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時請不要使用任何參數。
列出資源列表與 azapi_resource_list
現在儲存帳號已經建立,新增資料來源來列出它們,並使用 JMESPath 提取屬性。
建立名為
list_resources.tf的檔案,並插入下列程式碼:data "azapi_resource_list" "storage_accounts" { type = "Microsoft.Storage/storageAccounts@2023-01-01" parent_id = azurerm_resource_group.example.id # Use JMESPath expressions to extract specific fields from the response. # The API returns a list of resources in a top-level "value" array. response_export_values = { "names" = "value[].name" "locations" = "value[].location" "skus" = "value[].sku.name" } }建立名為
outputs.tf的檔案,並插入下列程式碼:output "resource_group_name" { value = azurerm_resource_group.example.name } output "storage_account_names" { value = data.azapi_resource_list.storage_accounts.output.names } output "storage_account_locations" { value = data.azapi_resource_list.storage_accounts.output.locations } output "storage_account_skus" { value = data.azapi_resource_list.storage_accounts.output.skus }再次執行
terraform apply以建立資料來源並擷取輸出:terraform apply
有關azapi_resource_list的重點
- 欄位
type用來識別要列出的資源類型和 API 版本。 - 欄位設定
parent_id範圍:資源群組 ID 用於在資源群組內列出,訂閱 ID 跨訂閱列出,或父資源 ID 用於列出子資源(例如,VNet 下的子網路)。 - 映射形式的
response_export_values使用 JMESPath 表達式來處理原始 API 回應。 儲存帳戶列表 API 回傳的結果是一個頂層value陣列,因此表達式以value[]開頭。
列出不同範圍的資源
決定 parent_id 刊登範圍。 範例:
# List all storage accounts in a subscription
data "azapi_resource_list" "all_storage" {
type = "Microsoft.Storage/storageAccounts@2023-01-01"
parent_id = "/subscriptions/${var.subscription_id}"
response_export_values = {
"names" = "value[].name"
}
}
# List subnets in a virtual network (child resource listing)
data "azapi_resource_list" "subnets" {
type = "Microsoft.Network/virtualNetworks/subnets@2023-11-01"
parent_id = azurerm_virtual_network.example.id
response_export_values = ["*"]
}
清理資源
當您不再需要透過 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 時的常見問題進行疑難解答