Azure FrontDoor 的 terraform 模块中的嵌套循环 [英] Nested loops within terraform module for Azure FrontDoor

查看:59
本文介绍了Azure FrontDoor 的 terraform 模块中的嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试部署 Azure Front Doors 及其自定义 https 配置资源的列表.我有一个 Azure Front Door 资源列表,这些资源像这个伪代码一样部署.它们可以正常工作(尽管没有自定义 https 配置)

I'm trying to deploy a list of Azure Front Doors and their custom https configuration resources. I have a list of Azure Front Door resources which are deployed like this pseudo code. They work correctly (although without custom https configuration)

resource "azurerm_frontdoor" "front_door" {
  count = length(local.frontdoors)
... config
}

然后我尝试添加一些 terraform 来创建自定义 https 配置,如此处所述并使用terraform azure frontdoor custom https config docs 以下片段:

I then try and add some terraform to create the custom https configuration as described here and useterraform azure frontdoor custom https config docs the following fragment:

resource "azurerm_frontdoor_custom_https_configuration" "custom_https_configuration" {
  count                             = length(local.frontdoors)
  for_each                          = { for frontend in azurerm_frontdoor.front_door[count.index].frontend_endpoint : frontend.id => frontend_id }
  frontend_endpoint_id              = each.value.frontend_id
  custom_https_provisioning_enabled = each.key != "front_door" ? local.frontend_https_configurations[each.key].custom_https_provisioning_enabled : false
  dynamic "custom_https_configuration" {
    for_each = (each.key != "front_door" ? local.frontend_https_configurations[each.key].custom_https_provisioning_enabled : false) ? [1] : []
    content {
      certificate_source                         = "AzureKeyVault"
      azure_key_vault_certificate_secret_name    = XXXX
      azure_key_vault_certificate_secret_version = XXXX
      azure_key_vault_certificate_vault_id       = XXXX
    }
  }
}

我收到了这个语法错误:

I'm getting this syntax error:

错误:count"的组合无效.和for_each"

Error: Invalid combination of "count" and "for_each"

如果我尝试删除计数,并改用 for_each 结构:

if i try and remove the count, and use the for_each structure instead:

resource "azurerm_frontdoor_custom_https_configuration" "custom_https_configuration" {
  for_each                          = { 
      for frontdoor in azurerm_frontdoor.front_door :
      [
        for key, value in frontdoor.frontend_endpoint: value.frontend.id => frontend_id
      ]
  }
  frontend_endpoint_id              = each.value.frontend_id
  custom_https_provisioning_enabled = each.key != "front_door" ? local.frontend_https_configurations[each.key].custom_https_provisioning_enabled : false
  dynamic "custom_https_configuration" {
    for_each = (each.key != "front_door" ? local.frontend_https_configurations[each.key].custom_https_provisioning_enabled : false) ? [1] : []
    content {
      certificate_source                         = "AzureKeyVault"
      azure_key_vault_certificate_secret_name    = XXXX
      azure_key_vault_certificate_secret_version = XXXX
      azure_key_vault_certificate_vault_id       = XXXX
    }
  }
}

我得到了这个错误:

错误:'for' 表达式无效

Error: Invalid 'for' expression

在 main.tf 第 25 行,在资源azurerm_frontdoor_custom_https_configuration"中custom_https_configuration":第173话174:用于 azurerm_frontdoor.front_door 中的前门:175:[176:对于键,frontdoor.frontend_endpoint中的值:value.frontend.id =>前端_id177:]178:}

on main.tf line 25, in resource "azurerm_frontdoor_custom_https_configuration" "custom_https_configuration": 173: for_each = { 174: for frontdoor in azurerm_frontdoor.front_door : 175: [ 176: for key, value in frontdoor.frontend_endpoint: value.frontend.id => frontend_id 177: ] 178: }

构建对象时需要键表达式.

Key expression is required when building an object.

我怎样才能有一个嵌套循环,以便我可以成功部署一个 f

How can i have a nested loop so that i can successfully deploy a f

推荐答案

当需要嵌套 for 循环时,需要使用 flatten 函数(用于列表)或 merge 函数结合 ... 列表扩展运算符(用于地图).

When you need to nest for loops, you need to use the flatten function (for lists) or the merge function in combination with the ... list expansion operator (for maps).

基本上是这样的:

// To make a list
for_each = flatten([
  for idx1, val1 in var.list1:
  [
    for idx2, val2 in val2.list_field:
      // Here is where you construct whatever value/object for each element
  ]
])

// To make a list
for_each = merge([
  for key1, val1 in var.map1:
  {
    for key2, val2 in val1.map_field:
    // Some key/value pair, such as:
    "${key1}-${key2}" => val2
  }
]...)

您的地图理解键/值也颠倒了.试试这个:

You also have your map comprehension key/value reversed. Try this:

for_each = merge([ 
      for idx, frontdoor in azurerm_frontdoor.front_door :
      {
        for key, value in frontdoor.frontend_endpoints: 
        "${idx}-${key}" => {
           endpoint_key = key
           endpoint_id = value
        }
      }
  ]...)

现在在您的资源中,您可以使用 each.value.endpoint_keyeach.value.endpoint_id.

And now within your resource, you can use each.value.endpoint_key and each.value.endpoint_id.

这篇关于Azure FrontDoor 的 terraform 模块中的嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆