在Azure中使用Terraform创建多源终端组

43次阅读
没有评论

问题描述

在使用Terraform在Azure中创建CDN终端时遇到了问题。他希望能够为多个不同区域的存储账户(blob)创建多个源,并为每个终端启用多源功能。在使用动态块创建多个源时,他遇到了一个错误,错误信息指出需要设置DefaultOriginGroup,但是他在Terraform的文档中找不到关于创建或设置DefaultOriginGroup的任何信息。

解决方案

请注意以下操作可能因版本差异而有所不同。以下为一种可能的解决方案。

使用DefaultOriginGroup启用多源功能

在Azure中使用Terraform创建多源终端组可以通过设置DefaultOriginGroup来实现。以下是创建多源终端组的步骤:

  1. 确保你的Terraform代码中引用了正确的Provider和版本。

  2. 根据你的需求,将下面的代码段添加到你的Terraform配置文件中,用于创建CDN终端组和多个源:

resource "azurerm_cdn_profile" "example" {
  name       = "examplecdnprofile"
  location   = "Global"
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_cdn_endpoint" "example" {
  name                          = "examplecdnendpoint"
  profile_name                  = azurerm_cdn_profile.example.name
  location                      = "global"
  querystring_caching_behaviour = "IgnoreQueryString"
  is_http_allowed               = true
  is_compression_enabled        = false

  dynamic "origin" {
    for_each = var.azure_locations_cli
    content {
      name      = "${origin.value}-origin"
      host_name = "${replace(var.storage_account_name, "%%LOCATION%%", origin.value)}.blob.core.windows.net"
    }
  }

  dynamic "origin_group" {
    for_each = var.azure_locations_cli
    content {
      name = "${origin_group.value}-group"

      dynamic "origin" {
        for_each = [azurerm_cdn_endpoint.example[origin_group.key]]
        content {
          name = origin.value.name
        }
      }
    }
  }

  default_origin_group = azurerm_cdn_endpoint.example[origin_group.key].origin_group[origin_group.key].name

  tags = var.cdn_tags
  depends_on = [azurerm_cdn_profile.example]
}

在上面的代码中,我们首先创建了一个CDN profile和一个CDN endpoint。动态块origin用于创建多个源,每个源的名称和主机名根据不同的区域进行设置。

然后,我们使用动态块origin_group来创建多个源组,每个源组关联一个源。通过设置default_origin_group属性,我们将默认的源组与终端关联,从而启用了多源功能。

  1. 根据你的项目需求,调整变量和其他配置,然后运行terraform initterraform apply来应用配置。

注意事项

在上述解决方案中,我们使用了default_origin_group来启用多源功能。然而,根据用户评论,目前可能还没有官方支持的方法来创建多个CDN源或设置DefaultOriginGroup。如果以上方法在当前版本中无法正常工作,你可以考虑以下替代方案:
– 关注GitHub问题页面(https://github.com/hashicorp/terraform-provider-azurerm/issues/10771),以获取官方支持的最新信息。
– 考虑使用Azure CLI与Terraform结合,通过本地执行命令来实现多源的创建和设置。

请注意,由于Terraform和Azure的版本差异以及提供商的支持情况可能会发生变化,因此在尝试任何解决方案之前,请务必查阅官方文档和相关资源,以确保你的操作是基于最新和正确的信息。

这是一种可能的解决方案,但由于Terraform和Azure的不断更新,具体的操作步骤可能会因版本变化而有所不同。在尝试任何操作之前,请务必查阅官方文档和相关资源,以确保你的操作是基于最新和正确的信息。

正文完