快速入門:使用 Terraform 建立執行個體 - Azure SQL 受控執行個體
文章使用下列 Terraform 和 Terraform 提供者版本進行測試:
本文說明如何使用 Terraform,在虛擬網路 (vNet) 以及與路由表和網路安全性群組相關聯的子網路中部署 Azure SQL 受控執行個體。
Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,您可以使用 HCL 語法來建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立設定檔之後,您可以建立執行計畫,讓您先預覽基礎結構變更,之後再部署。 驗證變更之後,您可以套用執行計畫來部署基礎結構。
在本文中,您將學會如何:
- 建立執行 Azure SQL 受控執行個體的所有支援服務
- 部署 SQL 受控執行個體
注意
本文中的範例程式碼位於 Azure Terraform GitHub 存放庫。 請參閱更多文章和範例程式碼,其中顯示如何使用 Terraform 來管理 Azure 資源
必要條件
- Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
實作 Terraform 程式碼
建立目錄,然後在目錄中測試並執行範例 Terraform 程式碼,且設為目前的目錄。
建立名為
providers.tf
的檔案,並插入下列程式碼:terraform { required_version = ">= 1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.0, < 4.0" } random = { source = "hashicorp/random" version = ">= 3.0" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } }
建立名為
main.tf
的檔案,並插入下列程式碼:# TODO set the variables below either enter them in plain text after = sign, or change them in variables.tf # (var.xyz will take the default value from variables.tf if you don't change it) # Create resource group resource "azurerm_resource_group" "example" { name = "${random_pet.prefix.id}-rg" location = var.location } # Create security group resource "azurerm_network_security_group" "example" { name = "${random_pet.prefix.id}-nsg" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name } # Create a virtual network resource "azurerm_virtual_network" "example" { name = "${random_pet.prefix.id}-vnet" resource_group_name = azurerm_resource_group.example.name address_space = ["10.0.0.0/24"] location = azurerm_resource_group.example.location } # Create a subnet resource "azurerm_subnet" "example" { name = "${random_pet.prefix.id}-subnet" resource_group_name = azurerm_resource_group.example.name virtual_network_name = azurerm_virtual_network.example.name address_prefixes = ["10.0.0.0/27"] delegation { name = "managedinstancedelegation" service_delegation { name = "Microsoft.Sql/managedInstances" actions = [ "Microsoft.Network/virtualNetworks/subnets/join/action", "Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action", "Microsoft.Network/virtualNetworks/subnets/unprepareNetworkPolicies/action" ] } } } # Associate subnet and the security group resource "azurerm_subnet_network_security_group_association" "example" { subnet_id = azurerm_subnet.example.id network_security_group_id = azurerm_network_security_group.example.id } # Create a route table resource "azurerm_route_table" "example" { name = "${random_pet.prefix.id}-rt" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name disable_bgp_route_propagation = false } # Associate subnet and the route table resource "azurerm_subnet_route_table_association" "example" { subnet_id = azurerm_subnet.example.id route_table_id = azurerm_route_table.example.id depends_on = [azurerm_subnet_network_security_group_association.example] } # Create managed instance resource "azurerm_mssql_managed_instance" "main" { name = "${random_pet.prefix.id}-mssql" resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location subnet_id = azurerm_subnet.example.id administrator_login = "${replace(random_pet.prefix.id, "-", "")}admin" administrator_login_password = random_password.password.result license_type = var.license_type sku_name = var.sku_name vcores = var.vcores storage_size_in_gb = var.storage_size_in_gb depends_on = [azurerm_subnet_route_table_association.example] } resource "random_password" "password" { length = 20 min_lower = 1 min_upper = 1 min_numeric = 1 min_special = 1 special = true } resource "random_pet" "prefix" { prefix = var.prefix length = 1 }
建立名為
variables.tf
的檔案,並插入下列程式碼:variable "prefix" { type = string default = "mi" description = "Prefix of the resource name" } variable "location" { type = string description = "Enter the location where you want to deploy the resources" default = "eastus" } variable "sku_name" { type = string description = "Enter SKU" default = "GP_Gen5" } variable "license_type" { type = string description = "Enter license type" default = "BasePrice" } variable "vcores" { type = number description = "Enter number of vCores you want to deploy" default = 8 } variable "storage_size_in_gb" { type = number description = "Enter storage size in GB" default = 32 }
初始化 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 入口網站內的結果,請瀏覽至新的資源群組。 新的執行個體在部署後位於新的資源群組中。 若要查看部署進度,請讓 PowerShell 保持開啟或瀏覽至 Azure 入口網站,搜尋 SQL 受控執行個體,然後依狀態篩選所有執行個體。
清除資源
當您不再需要透過 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 時的常見問題進行疑難排解