適用於:所有 API 管理層級
本文說明如何使用 Terraform 在 Azure 上建立 API 管理實例。 您也可以使用 Terraform 來執行常見的管理工作,例如,在 APIM 執行個體中匯入 API。
Azure API 管理 可協助組織將 API 發布至外部、合作夥伴和內部開發人員,以釋放其數據和服務的潛力。 「API 管理」提供的核心專長認證,透過開發人員參與、商務洞察力、分析、安全性和保護,可確保 API 程式獲致成功。 透過 APIM,為裝載於任何位置的現有後端服務,建立和管理新式 API 閘道。
Terraform 可啟用雲端基礎結構的定義、預覽和部署。 使用 Terraform,您可以使用 HCL 語法建立組態檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立組態檔之後,您會建立一個 執行計劃 ,讓您在部署基礎結構變更之前先預覽這些變更。 驗證變更之後,您可以套用執行計畫來部署基礎結構。
在本文中,您將學會如何:
- 使用 random_pet 建立 Azure 資源組名的隨機寵物名稱
- 使用 azurerm_resource_group 建立 Azure 資源群組
- 使用 random_string建立 Azure API 管理服務名稱的隨機字串
- 使用 azurerm_api_management 建立 Azure API 管理服務
必要條件
如果您沒有 Azure 訂用帳戶,請在開始之前建立 免費帳戶 。
對於 Azure CLI:
使用 Azure Cloud Shell 中的 Bash 環境。 如需詳細資訊,請參閱 開始使用 Azure Cloud Shell。
如果您想要在本機執行 CLI 參考命令, 請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱 如何在 Docker 容器中執行 Azure CLI。
如果您使用本機安裝,請使用 az login 命令登入 Azure CLI。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱 使用 Azure CLI 向 Azure 進行驗證。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需延伸模組的詳細資訊,請參閱 使用和管理 Azure CLI 的擴充功能。
執行 az version 以尋找已安裝的版本和相依連結庫。 若要升級至最新版本,請執行 az upgrade。
對於 Azure PowerShell:
- 如果您選擇在本機使用 Azure PowerShell:
- 安裝最新版的 Az PowerShell 模組。
- 使用 Connect-AzAccount Cmdlet 連線到您的 Azure 帳戶。
- 如果您選擇使用 Azure Cloud Shell:
- 如需詳細資訊 ,請參閱 Azure Cloud Shell 概觀。
- 如果您選擇在本機使用 Azure PowerShell:
實作 Terraform 程式碼
注意
本文的範例程式代碼位於 Azure Terraform GitHub 存放庫中。 您可以檢視包含 目前和舊版 Terraform 測試結果的記錄檔。
建立目錄,然後在目錄中測試並執行範例 Terraform 程式碼,且設為目前的目錄。
建立名為
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_api_management_name" { length = 13 lower = true numeric = false special = false upper = false } resource "azurerm_api_management" "api" { name = "apiservice${random_string.azurerm_api_management_name.result}" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name publisher_email = var.publisher_email publisher_name = var.publisher_name sku_name = "${var.sku_name}_${var.sku_count}" }建立名為
outputs.tf的檔案,並插入下列程式碼:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "api_management_service_name" { value = azurerm_api_management.api.name }建立名為
providers.tf的檔案,並插入下列程式碼:terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>4.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }建立名為
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 "publisher_email" { default = "test@contoso.com" description = "The email address of the owner of the service" type = string validation { condition = length(var.publisher_email) > 0 error_message = "The publisher_email must contain at least one character." } } variable "publisher_name" { default = "publisher" description = "The name of the owner of the service" type = string validation { condition = length(var.publisher_name) > 0 error_message = "The publisher_name must contain at least one character." } } variable "sku_name" { description = "The pricing tier of this API Management service" default = "BasicV2" type = string validation { condition = contains(["Basic", "BasicV2", "Consumption", "Developer", "Premium", "PremiumV2", "Standard", "StandardV2"], var.sku_name) error_message = "The sku must be one of the following: Basic, BasicV2, Consumption, Developer, Premium, PremiumV2, Standard, StandardV2." } } variable "sku_count" { description = "The instance size of this API Management service." default = 1 type = number validation { condition = contains([1, 2], var.sku_count) error_message = "The sku_count must be one of the following: 1, 2." } }
初始化 Terraform
執行 terraform init 以初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。
terraform init -upgrade
要點:
-
-upgrade參數會將必要的提供者外掛程式升級至符合設定版本條件約束的最新版本。
建立 Terraform 執行計畫
執行 terraform 計劃 以建立執行計劃。
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,不需要使用參數。
注意
建立和啟動 API 管理 服務可能需要 30 到 40 分鐘的時間。
驗證結果
取得 Azure 資源群組名稱。
resource_group_name=$(terraform output -raw resource_group_name)取得服務名稱。
api_management_service_name=$(terraform output -raw api_management_service_name)執行 az apim show 以顯示新服務的相關信息。
az apim show --resource-group $resource_group_name \ --name $api_management_service_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 時的常見問題進行疑難解答