0

I am trying to create multiple event hubs in a single namespace but I am not able to do so.

I have tried a single event hub which I am able to create.

When I am trying to create multiple event hubs passing all the event hub names in the variable and passing the same in the resource section is not able to create the consumer group

Here is the snippet for creating single event-hub

    {
    "$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.
Ravi kant Gautam
  • 333
  • 2
  • 23
  • Do you want to create those with a consumer group only? @Ravi kant Gautam – Jahnavi Aug 08 '23 at 09:00
  • anything is fine till I am able to access all the event hubs with the same connection string due to that I am trying to create the multiple event-hub in a single namespace – Ravi kant Gautam Aug 09 '23 at 12:11

1 Answers1

1

Referring to MSDoc template, I tried creating two events in a single namespace by adding event hub providers and dependsOn block as below and was able to achieve it successfully.

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
  ]
  
}

Deployment succeeded:

enter image description here

Portal view:

enter image description here

Note: It is possible to setup three to four event hubs in a single namespace in the above way, but if you want to create "n" hubs, you must use a for loop. Put all of the event hubs in an array or list and traverse them with a for loop, as detailed in this MSDoc.

Jahnavi
  • 3,076
  • 1
  • 3
  • 10