azure - 无法使用 ARM 模板在一个命名空间中创建多个事件中心

标签 azure azure-resource-manager azure-eventhub

我正在尝试在单个命名空间中创建多个事件中心,但我无法这样做。

我尝试了一个我能够创建的事件中心。

当我尝试创建多个事件中心并在变量中传递所有事件中心名称并在资源部分中传递相同名称时,无法创建使用者组

这是创建单个事件中心的代码片段

    {
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "metadata": {
      "_generator": {
        "name": "bicep",
        "version": "0.19.5.34762",
        "templateHash": "1037450281519309886"
      }
    },
    "parameters": {
      "namespaceName": {
        "type": "string",
        "defaultValue": "arm-deployed-event-hub-namespace",
        "metadata": {
          "description": "Name of EventHub namespace"
        }
      },
      "eventhubSku": {
        "type": "string",
        "defaultValue": "Standard",
        "allowedValues": [
          "Basic",
          "Standard"
        ],
        "metadata": {
          "description": "The messaging tier for service Bus namespace"
        }
      },
      "skuCapacity": {
        "type": "int",
        "defaultValue": 1,
        "allowedValues": [
          1,
          2,
          4
        ],
        "metadata": {
          "description": "MessagingUnits for premium namespace"
        }
      },
      "eventHubName": {
        "type": "string",
        "defaultValue": "arm-deployed_4g_att_cu_om",
        "metadata": {
          "description": "Name of Event Hub"
        }
      },
      "consumerGroupName": {
        "type": "string",
        "defaultValue": "arm-deployed-event-hub-consumer-group",
        "metadata": {
          "description": "Name of Consumer Group"
        }
      }
    },
    "variables": {
    },
    
    "resources": [
      
      {
        "type": "Microsoft.EventHub/namespaces",
        "apiVersion": "2022-10-01-preview",
        "name": "[parameters('namespaceName')]",
        "location": "[parameters('location')]",
        "sku": {
          "name": "[parameters('eventhubSku')]",
          "tier": "[parameters('eventhubSku')]",
          "capacity": "[parameters('skuCapacity')]"
        },
        "tags": {
          "tag1": "value1",
          "tag2": "value2"
        },
        "properties": {}
      },
      {
        "type": "Microsoft.EventHub/namespaces/eventhubs",
        "apiVersion": "2022-10-01-preview",
        "name": "[format('{0}/{1}', parameters('namespaceName'), parameters('eventHubName'))]",
        "properties": {},
        "dependsOn": [
          "[resourceId('Microsoft.EventHub/namespaces', parameters('namespaceName'))]"
        ]
      },
      {
        "type": "Microsoft.EventHub/namespaces/eventhubs/consumergroups",
        "apiVersion": "2022-10-01-preview",
        "name": "[format('{0}/{1}/{2}', parameters('namespaceName'), parameters('eventHubName'), parameters('consumerGroupName'))]",
        "properties": {
          "userMetadata": "User Metadata goes here"
        },
        "dependsOn": [
          "[resourceId('Microsoft.EventHub/namespaces/eventhubs', parameters('namespaceName'), parameters('eventHubName'))]"
        ]
      }
    ]}
  
The above code is working properly and I am able to create the resource.


Snippet for creating multiple event-hub in a single space.

``` {
   "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   "parameters": {},
   "variables": {
      "eventHubNames": [
         "myeventhub1",
         "myeventhub2"
         // Add more names as needed
      ],
      "namespaceName": "myeventhubnamespace"
   },
   "resources": [
      {
         "type": "Microsoft.EventHub/namespaces/eventhubs",
         "apiVersion": "2021-06-01-preview",
         "name": "[format('{0}/{1}', variables('namespaceName'), variables('eventHubNames')[copyIndex()])]",
         "copy": {
            "name": "eventHubLoop",
            "count": "[length(variables('eventHubNames'))]"
         },
         "location": "[resourceGroup().location]",
         "properties": {
            "messageRetentionInDays": 1,
            "partitionCount": 4
         }
      }
   ],
   "outputs": {
      "eventHubNames": {
         "type": "array",
         "value": "[variables('eventHubNames')]"
      }
   }
}


I have tried different approaches as well but I am getting stuck at the consumer group resource block and without the consumer block I am not able to create the resource.
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.15.31.15270",
      "templateHash": "12923377345559355625"
    }
  },
  "parameters": {
    "namespaceName": {
      "type": "string",
      "defaultValue": "chronos-event-hub-namespace",
      "metadata": {
        "description": "Name of EventHub namespace"
      }
    },
    "eventhubSku": {
      "type": "string",
      "defaultValue": "Standard",
      "allowedValues": [
        "Basic",
        "Standard"
      ],
      "metadata": {
        "description": "The messaging tier for service Bus namespace"
      }
    },
    "skuCapacity": {
      "type": "int",
      "defaultValue": 1,
      "allowedValues": [
        1,
        2,
        4
      ],
      "metadata": {
        "description": "MessagingUnits for premium namespace"
      }
    },
    // "eventHubName": {
    //   "type": "string",
    //   "defaultValue": "chronos_4g_event_hub_name",
    //   "metadata": {
    //     "description": "Name of Event Hub"
    //   }
    // },
    "consumerGroupName": {
      "type": "string",
      "defaultValue": "chronos-event-hub-consumer-group",
      "metadata": {
        "description": "Name of Consumer Group"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
    
  },
  "variables": {
    "namespaceName": "chronos-event-hub-namespace",
    "eventHubs": [
         {
            "name": "myeventhub1",
            "consumerGroups": [
               "group1",
               "group2"
            ]
         },
         {
            "name": "myeventhub2",
            "consumerGroups": [
               "group3",
               "group4"
            ]
         }
         // Add more Event Hubs with consumer groups as needed
      ]
  },
  "resources": [
      {
         "type": "Microsoft.EventHub/namespaces",
         "apiVersion": "2017-04-01",
         "name": "[variables('eventHubNamespaceName')]",
         "location": "[resourceGroup().location]",
         "sku": {
            "name": "Standard",
            "tier": "Standard"
         }
      },
      {
         "type": "Microsoft.EventHub/namespaces/eventhubs",
         "apiVersion": "2018-01-01-preview",
         "name": "[format('{0}/{1}', variables('eventHubNamespaceName'), variables('eventHubs')[copyIndex()].name)]",
         "copy": {
            "name": "eventHubLoop",
            "count": "[length(variables('eventHubs'))]"
         },
         "dependsOn": [
            "[resourceId('Microsoft.EventHub/namespaces', variables('eventHubNamespaceName'))]"
         ],
         "properties": {
            "messageRetentionInDays": 1,
            "partitionCount": 4
         }
      },
      {
         "type": "Microsoft.EventHub/namespaces/eventhubs/consumergroups",
         "apiVersion": "2017-04-01",
         "name": "[format('{0}/{1}/{2}', variables('eventHubNamespaceName'), variables('eventHubs')[copyIndex()].name, variables('eventHubs')[copyIndex()].consumerGroups[innerCopyIndex()])]",
         "copy": {
            "name": "consumerGroupLoop",
            "count": "[length(variables('eventHubs')[copyIndex()].consumerGroups)]",
            "mode": "inner"
         },
         "dependsOn": [
            "[resourceId('Microsoft.EventHub/namespaces/eventhubs', variables('eventHubNamespaceName'), variables('eventHubs')[copyIndex()].name)]"
         ]
      }
   ],
  "outputs": {
      "eventHubName": {
         "type": "array",
         "value": "[variables('eventHubs')]"
      }
   }
}


I am not sure how to define the consumer group resource section so that I can create multiple event-hub in a single namespace.

最佳答案

引用MSDoc模板中,我尝试通过添加 event hub 提供程序和 dependsOn block 在单个命名空间中创建两个事件,如下所示,并且能够成功实现。

param location string = resourceGroup().location
param Sku string = 'Standard'

var eventHubNamespaceName = 'projectNamens'
var eventHubName1 = 'neweventsample'
var eventHubName2 = 'neweventsample2'

resource eventHubNamespace 'Microsoft.EventHub/namespaces@2021-11-01' = {
  name: eventHubNamespaceName
  location: location
  sku: {
    name: Sku
    tier: Sku
    capacity: 1
  }
  properties: {
    isAutoInflateEnabled: false
    maximumThroughputUnits: 0
  }
}

resource eventHub 'Microsoft.EventHub/namespaces/eventhubs@2021-11-01' = {
  parent: eventHubNamespace
  name: eventHubName1
  properties: {
    messageRetentionInDays: 7
    partitionCount: 1
  }
}

resource eventHubnew 'Microsoft.EventHub/namespaces/eventhubs@2021-11-01' = {
  parent: eventHubNamespace
  name: eventHubName2
  properties: {
    messageRetentionInDays: 7
    partitionCount: 1
  }
  dependsOn: [
  eventHubNamespace
  ]
  
}

部署成功:

enter image description here

门户 View :

enter image description here

注意:可以按照上述方式在单个命名空间中设置三到四个个事件中心,但如果您想创建“n”个中心,您可以必须使用for 循环。将所有事件中心放入数组或列表中,并使用 for 循环 遍历它们,如本 MSDoc 中所述。 .

关于azure - 无法使用 ARM 模板在一个命名空间中创建多个事件中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76857754/

相关文章:

c# - Windows 服务总线检查性能计数器

java - 在 JSON 配置文件中注入(inject) Spring Boot 应用程序属性

powershell - 使用 Powershell 的 Azure 资源管理器 IP 安全限制

Azure 事件中心 : What is a 'Field Gateway' ?

azure - Azure Functions 失败时重试输出服务总线消息

azure - 使用 userAgent 列对 Azure CDN 访问日志进行 KQL 查询,按设备类型进行排序和计数

用于操作 Azure Functions 的 Azure ARM REST API?

Azure-Bicep:如何将 blob 容器添加到存储帐户

azure - 能否在不丢失检查点的情况下从 Microsoft.Azure.EventHubs 库迁移到 Azure.Messaging.EventHubs?

python - Azure 事件中心 Python SDK