使用 Terraform 設定 Azure 計算資源庫
Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,您可以使用 HCL 語法來建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立設定檔之後,您可以建立執行計畫,讓您先預覽基礎結構變更,之後再部署。 驗證變更之後,您可以套用執行計畫來部署基礎結構。
本文說明如何設定 Azure 計算資源庫。
在本文中,您將學會如何:
- 使用 Terraform 設定 Azure 計算資源庫 (先前 共用映像庫)
1.設定您的環境
- Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
設定 Terraform:如果您尚未這麼做,請使用下列其中一個選項來設定 Terraform:
2.實作 Terraform 程式代碼
建立目錄,然後在目錄中測試範例 Terraform 程式碼,並將其設為目前的目錄。
建立名為
providers.tf
的檔案,並插入下列程式碼:terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>2.0" } azuread = { source = "hashicorp/azuread" } } } provider "azurerm" { features {} }
建立名為
main.tf
的檔案,並插入下列程式碼:resource "azurerm_resource_group" "sigrg" { location = var.deploy_location name = var.rg_shared_name } # generate a random string (consisting of four characters) # https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string resource "random_string" "rando" { length = 4 upper = false special = false } # Creates Shared Image Gallery # https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/shared_image_gallery resource "azurerm_shared_image_gallery" "sig" { name = "sig${random_string.random.id}" resource_group_name = azurerm_resource_group.sigrg.name location = azurerm_resource_group.sigrg.location description = "Shared images" tags = { Environment = "Demo" Tech = "Terraform" } } #Creates image definition # https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/shared_image resource "azurerm_shared_image" "example" { name = "avd-image" gallery_name = azurerm_shared_image_gallery.sig.name resource_group_name = azurerm_resource_group.sigrg.name location = azurerm_resource_group.sigrg.location os_type = "Windows" identifier { publisher = "MicrosoftWindowsDesktop" offer = "office-365" sku = "20h2-evd-o365pp" } }
建立名為
variables.tf
的檔案,並插入下列程式碼:
variable "deploy_location" {
type = string
default = "eastus"
description = "The Azure Region in which all resources in this example should be created."
}
variable "rg_shared_name" {
type = string
default = "rg-shared-resources"
description = "Name of the Resource group in which to deploy shared resources"
}
- 建立名為
output.tf
的檔案,並插入下列程式碼:
output "location" {
description = "The Azure region"
value = azurerm_resource_group.sigrg.location
}
output "Compute_Gallery" {
description = "Azure Compute Gallery"
value = azurerm_shared_image_gallery.sig.name
}
3.建立 Terraform 執行計劃
執行 terraform plan 以建立執行計畫。
terraform plan -out main.tfplan
重點︰
terraform plan
命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。- 選用的
-out
參數可讓您指定計畫的輸出檔。 使用-out
參數可確保您所檢閱的方案就是所套用的方案。
4.套用 Terraform 執行計劃
執行 terraform apply 將執行計畫套用至您的雲端基礎結構。
terraform apply main.tfplan
重點︰
- 範例
terraform apply
命令會假設您先前已執行terraform plan -out main.tfplan
。 - 如果您為
-out
參數指定了不同的檔案名稱,請在呼叫terraform apply
時使用該檔案名稱。 - 若您未使用
-out
參數,請呼叫terraform apply
,不需要使用參數。
5.清除資源
當您不再需要透過 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 時的常見問題進行疑難排解
下一步
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應