共用方式為


快速入門:使用 Terraform 建立 Azure Front Door

本快速入門描述如何使用 Terraform 來建立 Front Door 設定檔,以設定 Web 端點的高可用性。

注意

針對 Web 工作負載,強烈建議使用 Azure DDoS 保護Web 應用程式防火牆來防範新興的 DDoS 攻擊。 另一個選項是運用 Azure Front Door 和 Web 應用程式防火牆。 Azure Front Door 提供平台層級保護來防範網路層級 DDoS 攻擊。 如需詳細資訊,請參閱 Azure 服務的安全性基準

在本文中,您將學會如何:

必要條件

實作 Terraform 程式碼

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

  2. 建立名為 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 {}
    }
    
  3. 建立名為 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_id" "front_door_endpoint_name" {
      byte_length = 8
    }
    
    locals {
      front_door_profile_name      = "MyFrontDoor"
      front_door_endpoint_name     = "afd-${lower(random_id.front_door_endpoint_name.hex)}"
      front_door_origin_group_name = "MyOriginGroup"
      front_door_origin_name       = "MyAppServiceOrigin"
      front_door_route_name        = "MyRoute"
    }
    
    resource "azurerm_cdn_frontdoor_profile" "my_front_door" {
      name                = local.front_door_profile_name
      resource_group_name = azurerm_resource_group.rg.name
      sku_name            = var.front_door_sku_name
    }
    
    resource "azurerm_cdn_frontdoor_endpoint" "my_endpoint" {
      name                     = local.front_door_endpoint_name
      cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.my_front_door.id
    }
    
    resource "azurerm_cdn_frontdoor_origin_group" "my_origin_group" {
      name                     = local.front_door_origin_group_name
      cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.my_front_door.id
      session_affinity_enabled = true
    
      load_balancing {
        sample_size                 = 4
        successful_samples_required = 3
      }
    
      health_probe {
        path                = "/"
        request_type        = "HEAD"
        protocol            = "Https"
        interval_in_seconds = 100
      }
    }
    
    resource "azurerm_cdn_frontdoor_origin" "my_app_service_origin" {
      name                          = local.front_door_origin_name
      cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.my_origin_group.id
    
      enabled                        = true
      host_name                      = azurerm_windows_web_app.app.default_hostname
      http_port                      = 80
      https_port                     = 443
      origin_host_header             = azurerm_windows_web_app.app.default_hostname
      priority                       = 1
      weight                         = 1000
      certificate_name_check_enabled = true
    }
    
    resource "azurerm_cdn_frontdoor_route" "my_route" {
      name                          = local.front_door_route_name
      cdn_frontdoor_endpoint_id     = azurerm_cdn_frontdoor_endpoint.my_endpoint.id
      cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.my_origin_group.id
      cdn_frontdoor_origin_ids      = [azurerm_cdn_frontdoor_origin.my_app_service_origin.id]
    
      supported_protocols    = ["Http", "Https"]
      patterns_to_match      = ["/*"]
      forwarding_protocol    = "HttpsOnly"
      link_to_default_domain = true
      https_redirect_enabled = true
    }
    
  4. 建立名為 app-service.tf 的檔案,並插入下列程式碼:

    resource "random_id" "app_name" {
      byte_length = 8
    }
    
    locals {
      app_name              = "myapp-${lower(random_id.app_name.hex)}"
      app_service_plan_name = "AppServicePlan"
    }
    
    resource "azurerm_service_plan" "app_service_plan" {
      name                = local.app_service_plan_name
      location            = azurerm_resource_group.rg.location
      resource_group_name = azurerm_resource_group.rg.name
    
      sku_name     = var.app_service_plan_sku_name
      os_type      = "Windows"
      worker_count = var.app_service_plan_capacity
    }
    
    resource "azurerm_windows_web_app" "app" {
      name                = local.app_name
      location            = azurerm_resource_group.rg.location
      resource_group_name = azurerm_resource_group.rg.name
      service_plan_id     = azurerm_service_plan.app_service_plan.id
    
      https_only = true
    
      site_config {
        ftps_state          = "Disabled"
        minimum_tls_version = "1.2"
        ip_restriction {
          service_tag               = "AzureFrontDoor.Backend"
          ip_address                = null
          virtual_network_subnet_id = null
          action                    = "Allow"
          priority                  = 100
          headers {
            x_azure_fdid      = [azurerm_cdn_frontdoor_profile.my_front_door.resource_guid]
            x_fd_health_probe = []
            x_forwarded_for   = []
            x_forwarded_host  = []
          }
          name = "Allow traffic from Front Door"
        }
      }
    }
    
  5. 建立名為 variables.tf 的檔案,並插入下列程式碼:

    variable "resource_group_location" {
      type        = string
      description = "Location for all resources."
      default     = "eastus"
    }
    
    variable "resource_group_name_prefix" {
      type        = string
      description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
      default     = "rg"
    }
    
    variable "app_service_plan_sku_name" {
      type        = string
      description = "The SKU for the plan. Possible values include: B1, B2, B3, D1, F1, I1, I2, I3, I1v2, I2v2, I3v2, I4v2, I5v2, I6v2, P1v2, P2v2, P3v2, P0v3, P1v3, P2v3, P3v3, P1mv3, P2mv3, P3mv3, P4mv3, P5mv3, S1, S2, S3, SHARED, EP1, EP2, EP3, WS1, WS2, WS3, Y1."
      default     = "S1"
      validation {
        condition     = contains(["B1", "B2", "B3", "D1", "F1", "I1", "I2", "I3", "I1v2", "I2v2", "I3v2", "I4v2", "I5v2", "I6v2", "P1v2", "P2v2", "P3v2", "P0v3", "P1v3", "P2v3", "P3v3", "P1mv3", "P2mv3", "P3mv3", "P4mv3", "P5mv3", "S1", "S2", "S3", "SHARED", "EP1", "EP2", "EP3", "WS1", "WS2", "WS3", "Y1"], var.app_service_plan_sku_name)
        error_message = "The SKU value must be one of the following: B1, B2, B3, D1, F1, I1, I2, I3, I1v2, I2v2, I3v2, I4v2, I5v2, I6v2, P1v2, P2v2, P3v2, P0v3, P1v3, P2v3, P3v3, P1mv3, P2mv3, P3mv3, P4mv3, P5mv3, S1, S2, S3, SHARED, EP1, EP2, EP3, WS1, WS2, WS3, Y1."
      }
    }
    
    variable "app_service_plan_capacity" {
      type        = number
      description = "The number of Workers (instances) to be allocated."
      default     = 1
    }
    
    variable "front_door_sku_name" {
      type        = string
      description = "The SKU for the Front Door profile. Possible values include: Standard_AzureFrontDoor, Premium_AzureFrontDoor"
      default     = "Standard_AzureFrontDoor"
      validation {
        condition     = contains(["Standard_AzureFrontDoor", "Premium_AzureFrontDoor"], var.front_door_sku_name)
        error_message = "The SKU value must be one of the following: Standard_AzureFrontDoor, Premium_AzureFrontDoor."
      }
    }
    
  6. 建立名為 outputs.tf 的檔案,並插入下列程式碼:

    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    
    output "frontDoorEndpointHostName" {
      value = azurerm_cdn_frontdoor_endpoint.my_endpoint.host_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 apply 將執行計畫套用至您的雲端基礎結構。

terraform apply main.tfplan

重點︰

  • 範例 terraform apply 命令假設您之前已執行過 terraform plan -out main.tfplan
  • 如果您為 -out 參數指定了不同的檔案名稱,請在呼叫 terraform apply 時使用該檔案名稱。
  • 若您未使用 -out 參數,請呼叫 terraform apply,不需要使用參數。

驗證結果

  1. 取得 Front Door 端點:

    terraform output -raw frontDoorEndpointHostName
    
  2. 將端點貼到瀏覽器中。

    成功連線至端點的螢幕快照。

清除資源

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

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

    terraform plan -destroy -out main.destroy.tfplan
    

    重點︰

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

    terraform apply main.destroy.tfplan
    

對 Azure 上的 Terraform 進行疑難排解

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

下一步