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

Terraform Azure Event Hub:可选capture_description字段配置方案咨询

问题分析与修正方案

你的方案存在两个核心问题:变量定义未将capture_description设为可选,以及dynamic块的迭代逻辑有误。以下是具体修正步骤:

1. 修正变量定义:将capture_description设为可选

在Terraform 0.14及以上版本中,可通过optional()标记字段为可选,不提供时该字段值默认为null。修改后的变量定义如下:

variable "event_hubs" {
  description = "List of Event Hubs that will live in the namespace."
  type = list(object({
    name                = string
    message_retention   = number
    partition_count     = number
    consumer_groups     = list(string)
    # 标记为可选字段,不提供时自动为null
    capture_description = optional(object({
        enabled             = bool
        encoding            = string
        interval_in_seconds = number
        size_limit_in_bytes = number
        skip_empty_archives = bool
        destination         = object({
            name                = string
            archive_name_format = string
            blob_container_name = string
            storage_account_id  = string
        })
    }))
  }))
  default = []
}

2. 修正dynamic块的迭代逻辑

原代码中存在两处错误:

  • 错误引用each.capture_description,应改为each.value.capture_description(each.value才是当前迭代的事件中心对象)
  • 错误使用list(var.capture_description),应针对当前事件中心的capture_description生成列表
  • 原代码将destination写为map,而AzureRM provider要求它是嵌套块

修正后的dynamic块代码:

# Create the Event Hub(s)
resource "azurerm_eventhub" "ehub" {
  for_each = local.event_hubs

  name                = each.value.name
  partition_count     = each.value.partition_count
  message_retention   = each.value.message_retention
  namespace_name      = azurerm_eventhub_namespace.namespace.name
  resource_group_name = azurerm_eventhub_namespace.namespace.resource_group_name

  dynamic "capture_description" {
    # compact会自动过滤null值,仅当capture_description存在时生成块
    for_each = compact([each.value.capture_description])

    content {
      enabled             = capture_description.value.enabled
      encoding            = capture_description.value.encoding
      interval_in_seconds = capture_description.value.interval_in_seconds
      size_limit_in_bytes = capture_description.value.size_limit_in_bytes
      skip_empty_archives = capture_description.value.skip_empty_archives

      # destination是嵌套块,而非属性
      destination {
        name                = capture_description.value.destination.name
        archive_name_format = capture_description.value.destination.archive_name_format
        blob_container_name = capture_description.value.destination.blob_container_name
        storage_account_id  = capture_description.value.destination.storage_account_id
      }
    }
  }
}

3. 补充local变量处理(可选但推荐)

为了让for_each更稳定(避免列表索引变化导致资源重建),建议将事件中心列表转为以名称为键的map:

locals {
  event_hubs = { for eh in var.event_hubs : eh.name => eh }
}

测试示例

以下是混合配置的事件中心列表,部分启用捕获,部分不启用:

event_hubs = [
  {
    name              = "eh-with-capture"
    message_retention = 7
    partition_count   = 4
    consumer_groups   = ["$Default"]
    capture_description = {
      enabled             = true
      encoding            = "Avro"
      interval_in_seconds = 300
      size_limit_in_bytes = 10485760
      skip_empty_archives = false
      destination = {
        name                = "EventHubArchive.AzureBlockBlob"
        archive_name_format = "{Namespace}/{EventHub}/{PartitionId}/{Year}/{Month}/{Day}/{Hour}/{Minute}/{Second}"
        blob_container_name = "eventhub-captures"
        storage_account_id  = "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Storage/storageAccounts/xxx"
      }
    }
  },
  {
    name              = "eh-without-capture"
    message_retention = 7
    partition_count   = 2
    consumer_groups   = ["$Default", "custom-group"]
    # 不提供capture_description字段,自动跳过捕获配置
  }
]

内容的提问来源于stack exchange,提问作者Paweł Sopel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 04:35:31