我正在尝试部署Azure Front Doors及其自定义https配置资源的列表。我有一个Azure Front Door资源的列表,这些资源像这个伪代码一样部署。它们工作正常(尽管没有自定义https配置(
resource "azurerm_frontdoor" "front_door" {
count = length(local.frontdoors)
... config
}
然后,我尝试添加一些terraform来创建这里描述的自定义https配置,并使用terraform azure frontdoor自定义https config文档如下片段:
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
}
}
}
我得到这个语法错误:
错误:"的组合无效;计数";以及";for_each";
如果我尝试删除计数,并使用for_each结构:
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"表达式无效
在主.tf行25上;azurerm_frondoor_custom_https_configuration"custom_https_configuration":173:for_each={174:用于azurer_frontdoor.front_door中的前门:175:[176:对于钥匙,frontdoor.frontend_endpoint:value.frontend.id=>frontend_id177:]178:}
生成对象时需要键表达式。
我如何才能有一个嵌套的循环,这样我才能成功地部署一个f
当您需要嵌套for
循环时,您需要使用flat函数(用于列表(或merge函数与...
列表扩展运算符(用于映射(相结合。
基本上是这样的:
// 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
}
]...)
您的地图理解键/值也已反转。试试这个:
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_key
和each.value.endpoint_id
。