我正在尝试为多个订阅配置多个 Azure 服务总线主题。我可以创建主题,但无法遍历订阅变量来创建订阅。
################
locals {
servicebus = {
"topic_1" = [{
subscription = ["subscription1", "subscription2", "subscription3"]
}],
"topic_2" = [{
subscription = ["subscription4", "subscription5", "subscription6"]
}],
"topic_3" = [{
subscription = ["subscription7", "subscription8", "subscription9"]
}]
}
service_bus = flatten([
for topicname, topic in local.servicebus : [
for subname in topic : {
name = topicname
subscription_name = subname.subscription
}
]
])
}
在 servicebus_subscription 资源块中,无法循环订阅名称,但是当我提供 index(each.value.subscription_name[0]) 时,它只创建一个订阅
############ Creating Servicebus Topic ###############################
module "servicebus_topic" {
source = "./servicebus/topic"
for_each = {
for sname in local.service_bus : sname.name => sname
}
name = each.key
resource_group_name = azurerm_resource_group.rg.name
namespace_name = module.servicebus_namespace.name
max_size_in_megabytes = "1024"
depends_on = [module.servicebus_namespace.name]
}
########## Creating Servicebus Subscription ###############################
resource "azurerm_servicebus_subscription" "sbs" {
for_each = {
for sname in local.service_bus : sname.name => sname
}
name = each.value.subscription_name
topic_name = module.servicebus_topic[each.value.name].name
namespace_name = module.servicebus_namespace.name
resource_group_name = azurerm_resource_group.rg.name
max_delivery_count = "10"
}
错误:
Error: Incorrect attribute value type
│
│ on servicebus.tf line 77, in resource "azurerm_servicebus_subscription" "sbs":
│ 77: name = each.value.subscription_name
│ ├────────────────
│ │ each.value.subscription_name is tuple with 3 elements
│
│ Inappropriate value for attribute "name": string required.
╵
╷
│ Error: Incorrect attribute value type
│
│ on servicebus.tf line 77, in resource "azurerm_servicebus_subscription" "sbs":
│ 77: name = each.value.subscription_name
│ ├────────────────
│ │ each.value.subscription_name is tuple with 3 elements
│
│ Inappropriate value for attribute "name": string required.
使用 Terraform 控制台的局部变量调试输出:
> local.service_bus
[
{
"name" = "topic_1"
"subscription_name" = [
"subscription1",
"subscription2",
"subscription3",
]
},
{
"name" = "topic_2"
"subscription_name" = [
"subscription4",
"subscription5",
"subscription16",
]
},
]
答案 0 :(得分:1)
你们很亲近。应该是:
service_bus = merge([
for topicname, topic in local.servicebus : {
for subname in topic[0].subscription :
"${topicname}-${subname}" => {
name = topicname
subscription_name = subname
}
}
]...)
然后
resource "azurerm_servicebus_subscription" "sbs" {
for_each = local.service_bus
name = each.value.subscription_name
topic_name = module.servicebus_topic[each.value.name].name
namespace_name = module.servicebus_namespace.name
resource_group_name = azurerm_resource_group.rg.name
max_delivery_count = "10"
}