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

如何基于Terraform带列表的对象Map批量创建EventHub消费组

批量创建EventHub消费组的Terraform配置方案

第一步:修正变量配置匹配问题

你的my.auto.tfvars中使用的键是consumer_group,但variables.tf里定义的是consumer_groups,需要统一修正tfvars里的键名,否则Terraform无法识别消费组列表:

eventhub_entityname = {
  firstdemo = {
    entity_partition_count = 1
    entity_message_retention = 1
    storage_capture_needed = true
    capture_encoding = "Avro"
    consumer_groups = ["test1", "test2"] # 修正键名为consumer_groups
  }
  seconddemo ={
    entity_partition_count = 1
    entity_message_retention = 1
    storage_capture_needed = true
    capture_encoding = "Avro"
    consumer_groups = ["test3"] # 修正键名为consumer_groups
  }
}

第二步:批量创建EventHub资源

先基于变量中的配置批量创建EventHub实例,消费组依赖这些EventHub:

resource "azurerm_eventhub" "main" {
  for_each             = var.eventhub_entityname
  name                 = each.key
  namespace_name       = azurerm_eventhub_namespace.example.name
  resource_group_name  = azurerm_resource_group.example.name
  partition_count      = each.value.entity_partition_count
  message_retention    = each.value.entity_message_retention
  status               = each.value.entity_status != null ? each.value.entity_status : "Active"

  capture_description {
    enabled            = each.value.storage_capture_needed
    encoding           = each.value.capture_encoding
    interval_in_seconds = each.value.capture_interval_in_seconds != null ? each.value.capture_interval_in_seconds : 300
    skip_empty_archives = each.value.capture_skip_empty_archives != null ? each.value.capture_skip_empty_archives : false
    size_limit_in_bytes = each.value.capture_size_limit_in_bytes != null ? each.value.capture_size_limit_in_bytes : 314572800

    destination {
      name                = "EventHubArchive.AzureBlockBlob"
      storage_account_id  = azurerm_storage_account.example.id # 替换为你的存储账户ID
      blob_container_name = "capture-container" # 替换为你的容器名称
    }
  }
}

第三步:批量创建消费组资源

通过嵌套遍历,将每个EventHub对应的消费组列表展开,生成唯一的消费组资源:

resource "azurerm_eventhub_consumer_group" "main" {
  # 生成扁平的资源键:eventhub名称-消费组名称,确保每个资源唯一
  for_each = {
    for eventhub_name, eventhub_config in var.eventhub_entityname :
    for group_name in eventhub_config.consumer_groups != null ? eventhub_config.consumer_groups : [] :
    "${eventhub_name}-${group_name}" => {
      eventhub_name = eventhub_name
      group_name    = group_name
    }
  }

  name                = each.value.group_name
  namespace_name      = azurerm_eventhub_namespace.example.name
  eventhub_name       = each.value.eventhub_name
  resource_group_name = azurerm_resource_group.example.name
  user_metadata       = "metadata-for-${each.value.group_name}"
}

说明

  • 上述代码中,azurerm_eventhub_namespace.example、azurerm_resource_group.example、azurerm_storage_account.example需替换为你实际定义的对应资源名称
  • 消费组资源的for_each通过嵌套循环,将每个EventHub的消费组列表展开为独立的资源项,确保每个消费组都是唯一的Terraform资源
  • 若某个EventHub未配置consumer_groups,则不会为其创建任何消费组

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 11:57:34