将“azurerm_cdn_frontdoor_origin”重新用于现有azure前门配置中的全新“azurerm_cdn_frontdoor_route”

mwngjboj  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(96)

简而言之,我想:

  • "azurerm_cdn_frontdoor_route"添加到*现有Azure前门配置 *

由于terraform specification所需参数之一为cdn_frontdoor_origin_ids
[*(必需)此前门路由将链接到的一个或多个前门源资源ID *]

已完成的工作:

因此,我的第一个意图是通过专用的tf数据源从现有的源**查询必要的信息,但不幸的是,azurerm tf提供程序似乎不支持azurerm_cdn_frontdoor_origin资源的数据源。

P.s所以,再一次,问题,困扰着我,如何管理这样的设置

如有任何建议,将不胜感激

34gzjxbg

34gzjxbg1#

我最初使用以下代码创建了原点和其他:

resource "azurerm_dns_zone" "example" {
  name                = "examplekavv.com"
  resource_group_name = data.azurerm_resource_group.example.name
}

resource "azurerm_cdn_frontdoor_profile" "example" {
  name                = "exampleprofile"
  resource_group_name = data.azurerm_resource_group.example.name
   sku_name            = "Standard_AzureFrontDoor"
}

resource "azurerm_cdn_frontdoor_origin_group" "example" {
  name                     = "example-origin-group"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.example.id
  session_affinity_enabled = true

  restore_traffic_time_to_healed_or_new_endpoint_in_minutes = 10

  health_probe {
    interval_in_seconds = 240
    path                = "/healthProbe"
    protocol            = "Https"
    request_type        = "HEAD"
  }

  load_balancing {
    additional_latency_in_milliseconds = 0
    sample_size                        = 16
    successful_samples_required        = 3
  }
}

output "cdngroup" {
  value = azurerm_cdn_frontdoor_origin_group.example
  
}

data "azurerm_cdn_frontdoor_origin_group" "all_groups" {
  name                     = "example-origin-group"
  resource_group_name = data.azurerm_resource_group.example.name
  profile_name = azurerm_cdn_frontdoor_profile.example.name
  //cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.example.id
}

output "cdn_origin_groupname" {
  value = data.azurerm_cdn_frontdoor_origin_group.all_groups.health_probe
}

resource "azurerm_cdn_frontdoor_origin" "example" {
  name                          = "example-origin"
  cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.example.id
  enabled                       = true

  certificate_name_check_enabled = false

  host_name          = "contoso.com"
  http_port          = 80
  https_port         = 443
  origin_host_header = "www.contoso.com"
  priority           = 1
  weight             = 1
}
resource "azurerm_cdn_frontdoor_endpoint" "example" {
  name                     = "example-endpoint"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.example.id
  
}

data "azurerm_cdn_frontdoor_endpoint" "all_endpoints" {
  name                = azurerm_cdn_frontdoor_endpoint.example.name
  profile_name = azurerm_cdn_frontdoor_profile.example.name
  resource_group_name =  data.azurerm_resource_group.example.name
}

output "frontDoorEndpointHostName" {
  value = data.azurerm_cdn_frontdoor_endpoint.all_endpoints.host_name
}


resource "azurerm_cdn_frontdoor_custom_domain" "contoso" {
  name                     = "contoso-custom-domain"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.example.id
  dns_zone_id              = azurerm_dns_zone.example.id
  host_name                = join(".", ["contoso", azurerm_dns_zone.example.name])

  tls {
    certificate_type    = "ManagedCertificate"
    minimum_tls_version = "TLS12"
  }
}

resource "azurerm_cdn_frontdoor_custom_domain" "fabrikam" {
  name                     = "fabrikam-custom-domain"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.example.id
  dns_zone_id              = azurerm_dns_zone.example.id
  host_name                = join(".", ["fabrikam", azurerm_dns_zone.example.name])

  tls {
    certificate_type    = "ManagedCertificate"
    minimum_tls_version = "TLS12"
  }
}

resource "azurerm_cdn_frontdoor_rule_set" "example" {
  name                     = "kaexampleruleset"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.example.id
}

resource "azurerm_cdn_frontdoor_rule" "example" {
  depends_on = [azurerm_cdn_frontdoor_origin_group.example, azurerm_cdn_frontdoor_origin.example]

  name                      = "examplerule"
  cdn_frontdoor_rule_set_id = azurerm_cdn_frontdoor_rule_set.example.id
  order                     = 1
  behavior_on_match         = "Continue"

  actions {
    route_configuration_override_action {
      cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.example.id
      forwarding_protocol           = "HttpsOnly"
      query_string_caching_behavior = "IncludeSpecifiedQueryStrings"
      query_string_parameters       = ["foo", "clientIp={client_ip}"]
      compression_enabled           = true
      cache_behavior                = "OverrideIfOriginMissing"
      cache_duration                = "365.23:59:59"
    }

    url_redirect_action {
      redirect_type        = "PermanentRedirect"
      redirect_protocol    = "MatchRequest"
      query_string         = "clientIp={client_ip}"
      destination_path     = "/exampleredirection"
      destination_hostname = "contoso.com"
      destination_fragment = "UrlRedirect"
    }
  }

  conditions {
    host_name_condition {
      operator         = "Equal"
      negate_condition = false
      match_values     = ["www.contoso.com", "images.contoso.com", "video.contoso.com"]
      transforms       = ["Lowercase", "Trim"]
    }

    is_device_condition {
      operator         = "Equal"
      negate_condition = false
      match_values     = ["Mobile"]
    }

    post_args_condition {
      post_args_name = "customerName"
      operator       = "BeginsWith"
      match_values   = ["J", "K"]
      transforms     = ["Uppercase"]
    }

    request_method_condition {
      operator         = "Equal"
      negate_condition = false
      match_values     = ["DELETE"]
    }

    url_filename_condition {
      operator         = "Equal"
      negate_condition = false
      match_values     = ["media.mp4"]
      transforms       = ["Lowercase", "RemoveNulls", "Trim"]
    }
  }
}

resource "azurerm_cdn_frontdoor_route" "example" {
  name                          = "example-route"
  cdn_frontdoor_endpoint_id     = azurerm_cdn_frontdoor_endpoint.example.id
  cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.example.id
  cdn_frontdoor_origin_ids      = [azurerm_cdn_frontdoor_origin.example.id]
  cdn_frontdoor_rule_set_ids    = [azurerm_cdn_frontdoor_rule_set.example.id]
  enabled                       = true

  forwarding_protocol    = "HttpsOnly"
  https_redirect_enabled = true
  patterns_to_match      = ["/*"]
  supported_protocols    = ["Http", "Https"]

  cdn_frontdoor_custom_domain_ids = [azurerm_cdn_frontdoor_custom_domain.contoso.id, azurerm_cdn_frontdoor_custom_domain.fabrikam.id]
  link_to_default_domain          = false

  cache {
    query_string_caching_behavior = "IgnoreSpecifiedQueryStrings"
    query_strings                 = ["account", "settings"]
    compression_enabled           = true
    content_types_to_compress     = ["text/html", "text/javascript", "text/xml"]
  }
}

resource "azurerm_cdn_frontdoor_custom_domain_association" "contoso" {
  cdn_frontdoor_custom_domain_id = azurerm_cdn_frontdoor_custom_domain.contoso.id
  cdn_frontdoor_route_ids        = [azurerm_cdn_frontdoor_route.example.id]
}

resource "azurerm_cdn_frontdoor_custom_domain_association" "fabrikam" {
  cdn_frontdoor_custom_domain_id = azurerm_cdn_frontdoor_custom_domain.fabrikam.id
  cdn_frontdoor_route_ids        = [azurerm_cdn_frontdoor_route.example.id]
}

我已经尝试了创建数据源的cdn前门起源几种方式即;

data "azurerm_cdn_frontdoor_origin" "example" {
      name = azurerm_cdn_frontdoor_origin.example.name
    }

但正如文档所建议的,正如你所说的,该块没有数据源。

  • 所以,我找到的唯一方法是使用起源组的现有起源的资源ID:
  • 从来源组的来源页面记录来源的名称。

创建资源Id,格式如下:
来源ResourceId:/subscriptions/<subscriptionId>/resourceGroups/<resourcegroup name>/providers/Microsoft.Cdn/profiles/<yourCDNprofilename>/originGroups/<origin-group-name>/origins/<desired-origin name>

  • 在我的案例中:*
    一米一米一

根据需要更改protocols和cdn custom domains,否则您可能会面临以下类型的错误,因为它们可以具有相同的配置:

resource "azurerm_cdn_frontdoor_custom_domain" "contoso" {
  name                     = "contoso-custom-domain"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.example.id
  dns_zone_id              = azurerm_dns_zone.example.id
  host_name                = join(".", ["contoso", azurerm_dns_zone.example.name])

  tls {
    certificate_type    = "ManagedCertificate"
    minimum_tls_version = "TLS12"
  }
}

resource "azurerm_cdn_frontdoor_custom_domain" "kavya" {
  name                     = "kavya-custom-domain"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.example.id
  dns_zone_id              = azurerm_dns_zone.example.id
  host_name                = join(".", ["kavya", azurerm_dns_zone.example.name])

  tls {
    certificate_type    = "ManagedCertificate"
    minimum_tls_version = "TLS12"
  }
}

resource "azurerm_cdn_frontdoor_custom_domain" "fabrikam" {
  name                     = "fabrikam-custom-domain"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.example.id
  dns_zone_id              = azurerm_dns_zone.example.id
  host_name                = join(".", ["fabrikam", azurerm_dns_zone.example.name])

  tls {
    certificate_type    = "ManagedCertificate"
    minimum_tls_version = "TLS12"
  }
}

resource "azurerm_cdn_frontdoor_rule_set" "example" {
  name                     = "kaexampleruleset"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.example.id
}

Error: creating Front Door Route: (Route Name "example-route3" / Afd Endpoint Name "example-endpoint" / Profile Name "exampleprofile" / Resource Group "xx"): cdn.RoutesClient#Create: Failure sending request: StatusCode=400 -- Original Error: Code="BadRequest" Message="The route domains, paths and protocols configuration has a conflict. More information: Domain: /subscriptions/xxx-2b83a074c23f/resourceGroups/xxxxxxe/providers/Microsoft.Cdn/profiles/exampleprofile/customDomains/fabrikam-custom-domain, Path pattern: /*, Protocol: Https cannot be added to Route example-route3 as this combination already exists in Endpoint example-endpoint, Route example-route.."
│   on main.tf line 321, in resource "azurerm_cdn_frontdoor_route" "example3":
│  321: resource "azurerm_cdn_frontdoor_route" "example3" {

我已经使用现有的源创建了azurerm_cdn_frontdoor_route,如下所示:

  • 示例:*
resource "azurerm_cdn_frontdoor_route" "example3" {
  name                          = "example-route3"
  cdn_frontdoor_endpoint_id     = azurerm_cdn_frontdoor_endpoint.example.id
  cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.example.id
  cdn_frontdoor_origin_ids      = ["/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Cdn/profiles/exampleprofile/originGroups/example-origin-group/origins/example-origin"]
  cdn_frontdoor_rule_set_ids    = [azurerm_cdn_frontdoor_rule_set.example.id]
  enabled                       = true

  forwarding_protocol    = "HttpsOnly"
  https_redirect_enabled = true
  patterns_to_match      = ["/*"]
  supported_protocols    = ["Http","Https"]

  cdn_frontdoor_custom_domain_ids = [azurerm_cdn_frontdoor_custom_domain.<anyname>.id]
  link_to_default_domain          = false

  cache {
    query_string_caching_behavior = "IgnoreSpecifiedQueryStrings"
    query_strings                 = ["account", "settings"]
    compression_enabled           = true
    content_types_to_compress     = [ "text/xml"]
  }
}

已创建两条原点相同的管线。

参考:cdn_frontdoor_origin_group | | Resources | hashicorp/azurerm | Terraform Registry

相关问题