快速入門:使用 AzAPI Terraform 提供者部署您的第一個 Azure 資源

Terraform 可啟用雲端基礎結構的定義、預覽和部署。 使用 Terraform,您可以使用 HCL 語法建立組態檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立組態檔之後,您會建立一個 執行計劃 ,讓您在部署基礎結構變更之前先預覽這些變更。 驗證變更之後,您可以套用執行計畫來部署基礎結構。

AzAPI Terraform 提供者直接透過 Azure Resource Manager API 管理Azure資源。 它對於尚未被 AzureRM 支援的資源、屬性或 API 版本特別有用。 在這個例子中,你使用 azapi_resource 來建立一個 Azure Container Registry

  • 使用 AzAPI 建立資源群組與容器登錄檔
  • 註冊 Microsoft.ContainerRegistry 提供者

必要條件

  • Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始之前建立 免費帳戶

當您使用Microsoft帳戶登入 Azure 入口網站時,會使用該帳戶的預設 Azure 訂用帳戶。

Terraform 會自動使用預設 Azure 訂用帳戶中的資訊進行驗證。

執行 az account show 以確認目前的Microsoft帳戶和 Azure 訂用帳戶。

az account show

您透過 Terraform 所做的任何變更都會顯示在顯示的 Azure 訂用帳戶上。 如果是您想要的,請略過本文的其餘部分。

實作 Terraform 程式碼

  1. 建立目錄,然後在目錄中測試範例 Terraform 程式碼,並將其設為目前的目錄。

  2. 建立名為 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 {}
    }
    
  3. 建立名為 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 ID so name is unique in your Azure subscription."
    }
    
    variable "container_registry_name" {
      type        = string
      default     = ""
      description = "Name of the container registry."
    }
    
  4. 建立名為 main.tf 的檔案,並插入下列程式碼:

    # Create a resource group with a random name.
    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      location = var.resource_group_location
      name     = random_pet.rg_name.id
    }
    
    # Create a user assigned identity resource.
    resource "azurerm_user_assigned_identity" "example" {
      name                = "example"
      resource_group_name = azurerm_resource_group.rg.name
      location            = azurerm_resource_group.rg.location
    }
    
    # Create a random name for the container registry.
    resource "random_string" "acr_name" {
      length  = 10
      special = false
      upper   = false
      numeric = false
    }
    
    # Manage a container registry resource.
    resource "azapi_resource" "example" {
      type      = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
      name      = coalesce(var.container_registry_name, random_string.acr_name.result)
      parent_id = azurerm_resource_group.rg.id
    
      location = azurerm_resource_group.rg.location
      identity {
        type         = "SystemAssigned, UserAssigned"
        identity_ids = [azurerm_user_assigned_identity.example.id]
      }
    
      body = {
        sku = {
          name = "Standard"
        }
        properties = {
          adminUserEnabled = true
        }
      }
    
      tags = {
        "Key" = "Value"
      }
    
      response_export_values = ["properties.loginServer", "properties.policies.quarantinePolicy.status"]
    }
    
  5. 建立名為 outputs.tf 的檔案,並插入下列程式碼:

    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "azure_container_registry_name" {
      value = azapi_resource.example.name
    }
    

初始化 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,不需要使用參數。

驗證結果

  1. 取得資源組名。

    resource_group_name=$(terraform output -raw resource_group_name)
    
  2. 取得容器登錄名稱。

    azure_container_registry_name=$(terraform output -raw azure_container_registry_name)
    
  3. 執行 az acr show 來檢視容器登錄。

    az acr show --name $azure_container_registry_name --resource-group $resource_group_name
    

清除資源

當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:

  1. 執行 terraform 計劃 並指定 destroy 旗標。

    terraform plan -destroy -out main.destroy.tfplan
    

    要點:

    • terraform plan 命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。
    • 選用的 -out 參數可讓您指定計畫的輸出檔。 使用 -out 參數可確保您所檢閱的方案就是所套用的方案。
  2. 執行 terraform apply 以套用執行計劃。

    terraform apply main.destroy.tfplan
    

對 Azure 上的 Terraform 進行疑難排解

針對在 Azure 上使用 Terraform 時的常見問題進行疑難解答

下一步