You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform读取YAML列表值创建Azure Service Bus订阅失败问题

Azure Service Bus订阅创建失败问题排查与解决

问题背景

使用Terraform结合YAML配置部署Azure Service Bus,已成功创建命名空间、网络规则、命名空间授权规则及Topic,但通过YAML定义每个Topic下的多个订阅并由Terraform批量创建Subscription资源时失败。

错误信息

Error: Incorrect attribute value type
│ 
│   on main.tf line 215, in resource "azurerm_servicebus_subscription" "subscription":
│  215:   name       = each.value.servicebus_subscription
│     ├────────────────
│     │ each.value.servicebus_subscription is tuple with 2 elements
│ 
│ Inappropriate value for attribute "name": string required.

配置文件

con.yaml

#-------
servicebus:
  - servicebus_topic: tesTopic1
    #enable_partitioning: "true"
    servicebus_subscription: ['test-service1', 'test-service1']

  - servicebus_topic: testTopic2
    servicebus_subscription: ['test-db1', 'test-service2']

pub_sub.tf(原错误代码)

resource "azurerm_servicebus_subscription" "subscription" {
  for_each = { for subscriptions in local.service_bus_conf : subscriptions.servicebus_topic=> subscriptions}
  depends_on = [azurerm_servicebus_topic.topic]
  name       = each.value.servicebus_subscription
  topic_id   = data.azurerm_servicebus_topic.topic[each.value.servicebus_topic].id
}

解决方案

错误原因

azurerm_servicebus_subscription的name属性要求传入字符串,但当前代码中each.value.servicebus_subscription是一个数组(tuple),类型不匹配导致报错。同时原for_each逻辑是按Topic分组,每个元素包含该Topic下的所有订阅数组,无法直接映射到单个订阅资源。

修正代码

修改for_each逻辑,将所有订阅项展开,为每个订阅生成唯一的资源实例:

resource "azurerm_servicebus_subscription" "subscription" {
  # 用"Topic名称/订阅名称"作为唯一键,确保每个订阅对应独立资源
  for_each = {
    for topic in local.service_bus_conf :
    for sub_name in topic.servicebus_subscription :
    "${topic.servicebus_topic}/${sub_name}" => {
      topic_name = topic.servicebus_topic
      sub_name   = sub_name
    }
  }

  depends_on = [azurerm_servicebus_topic.topic]
  name       = each.value.sub_name
  topic_id   = data.azurerm_servicebus_topic.topic[each.value.topic_name].id
}

额外修正

YAML配置中testTopic2下的servicebus_subscription:['test-db1', 'test-service2']缺少冒号后的空格,需改为servicebus_subscription: ['test-db1', 'test-service2'],避免YAML解析异常。

内容的提问来源于stack exchange,提问作者Ikteaja hsan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 08:30:52