使用 Terraform 建立 Batch 帳戶 (包括儲存體),以便開始使用 Azure Batch。 您需有 Batch 帳戶才能建立計算資源 (計算節點的集區) 和 Batch 作業。 您可以將 Azure 記憶體帳戶與您的 Batch 帳戶連結。 此配對對於部署應用程式和儲存大部分真實世界工作負載的輸入和輸出數據很有用。
完成本快速入門之後,您將了解 Batch 服務的重要概念,並可準備使用更多真實的工作負載來大規模試用 Batch。
Terraform 允許對雲端基礎結構進行定義、預覽和部署。 使用 Terraform,您可以使用 HCL 語法建立組態檔。 HCL 語法可讓您指定雲端提供者,例如 Azure,以及構成雲端基礎結構的專案。 建立組態檔之後,您會建立 執行計劃,讓您在部署基礎結構變更之前預覽這些變更。 驗證變更之後,您會套用執行計劃來部署基礎結構。
在本文中,您將學會如何:
- 使用 random_pet,為 Azure 資源群組名稱建立隨機值
- 使用 azurerm_resource_group 建立 Azure 資源群組
- 使用 random_string 建立隨機值
- 使用 azurerm_storage_account 建立 Azure 記憶體帳戶
- 使用 azurerm_batch_account 建立 Azure Batch 帳戶
先決條件
實作 Terraform 程式代碼
備註
本文的範例程式代碼位於 Azure Terraform GitHub 存放庫。 您可以檢視記錄檔,其中包含目前和舊版 Terraform 的測試結果。
建立目錄,在其中測試並執行範例 Terraform 程式代碼,並將其設為目前目錄。
建立名為
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 {} }建立名為
main.tf的檔案,並插入下列程序代碼:resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "azurerm_resource_group" "rg" { name = random_pet.rg_name.id location = var.resource_group_location } resource "random_string" "azurerm_storage_account_name" { length = 13 lower = true numeric = false special = false upper = false } resource "random_string" "azurerm_batch_account_name" { length = 13 lower = true numeric = false special = false upper = false } resource "azurerm_storage_account" "storage" { name = "storage${random_string.azurerm_storage_account_name.result}" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location account_tier = element(split("_", var.storage_account_type), 0) account_replication_type = element(split("_", var.storage_account_type), 1) } resource "azurerm_batch_account" "batch" { name = "batch${random_string.azurerm_batch_account_name.result}" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location storage_account_id = azurerm_storage_account.storage.id storage_account_authentication_mode = "StorageKeys" }建立名為
variables.tf的檔案,並插入下列程序代碼:variable "resource_group_location" { type = string default = "eastus" description = "Location for all resources." } 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 "storage_account_type" { type = string default = "Standard_LRS" description = "Azure Storage account type." validation { condition = contains(["Premium_LRS", "Premium_ZRS", "Standard_GRS", "Standard_GZRS", "Standard_LRS", "Standard_RAGRS", "Standard_RAGZRS", "Standard_ZRS"], var.storage_account_type) error_message = "Invalid storage account type. The value should be one of the following: 'Premium_LRS','Premium_ZRS','Standard_GRS','Standard_GZRS','Standard_LRS','Standard_RAGRS','Standard_RAGZRS','Standard_ZRS'." } }建立名為
outputs.tf的檔案,並插入下列程序代碼:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "batch_name" { value = azurerm_batch_account.batch.name } output "storage_name" { value = azurerm_storage_account.storage.name }
初始化 Terraform
執行 terraform init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。
terraform init -upgrade
重點:
-
-upgrade參數會將必要的提供者外掛程式升級至符合組態版本條件約束的最新版本。
建立 Terraform 執行計劃
執行 terraform plan 以建立執行計畫。
terraform plan -out main.tfplan
重點:
-
terraform plan命令會建立執行計劃,但不會執行它。 然而,它會決定哪些動作是必要的,以建立您組態檔中所指定的設定。 此模式可讓您在對實際資源進行任何變更之前,先確認執行計劃是否符合您的預期。 - 選擇性
-out參數可讓您指定計劃的輸出檔。 使用-out參數可確保您查看的計劃確切地被套用。
套用 Terraform 執行計畫
執行 terraform 套用,將執行計劃套用至您的雲端基礎結構。
terraform apply main.tfplan
重點:
- 範例
terraform apply命令假設您先前已執行terraform plan -out main.tfplan。 - 如果您為
-out參數指定了不同的檔案名,請在呼叫terraform apply時使用相同的檔案名。 - 如果您未使用
-out參數,請呼叫沒有任何參數的terraform apply。
確認結果
取得 Azure 資源群組名稱。
resource_group_name=$(terraform output -raw resource_group_name)取得 Batch 帳戶名稱。
batch_name=$(terraform output -raw batch_name)執行 az batch account show 以顯示新 Batch 帳戶的相關信息。
az batch account show \ --resource-group $resource_group_name \ --name $batch_name
清理資源
當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:
執行 terraform 方案 並指定
destroy旗標。terraform plan -destroy -out main.destroy.tfplan重點:
-
terraform plan命令會建立執行計劃,但不會執行它。 然而,它會決定哪些動作是必要的,以建立您組態檔中所指定的設定。 此模式可讓您在對實際資源進行任何變更之前,先確認執行計劃是否符合您的預期。 - 選擇性
-out參數可讓您指定計劃的輸出檔。 使用-out參數可確保您查看的計劃確切地被套用。
-
運行 terraform apply 來套用執行計劃。
terraform apply main.destroy.tfplan
排除 Azure 上 Terraform 的故障
針對在 Azure 上使用 Terraform 時的常見問題進行疑難解答