你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Terraform 创建 Azure Front Door

本快速入门介绍如何使用 Terraform 创建 Front Door 配置文件,以便设置 Web 终结点的高可用性。

注意

对于 Web 工作负载,强烈建议使用 Azure DDoS 防护Web 应用程序防护墙来抵御新兴的 DDoS 攻击。 另一种选择是将 Azure Front Door 与 Web 应用程序防火墙一起使用。 Azure Front Door 提供平台级保护来抵御网络级 DDoS 攻击。 有关详细信息,请参阅 Azure 服务的安全基线

在本文中,学习如何:

先决条件

实现 Terraform 代码

注意

本文中的示例代码位于 Azure Terraform GitHub 存储库中。 你可以查看包含当前和以前 Terraform 版本的测试结果的日志文件。

有关更多示例,请参阅演示如何使用 Terraform 管理 Azure 资源的文章和示例代码

  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 时遇到的常见问题

后续步骤