0

How to select and add multiple subnets of a VNet in the networking section of an azure eventHub resource using azure bicep.

// Create an event hub namespace

var eventHubNamespaceName = 'evhns-demo1436'

resource eventHubNamespace 'Microsoft.EventHub/namespaces@2021-01-01-preview' = {
  name: eventHubNamespaceName
  location: resourceGroup().location
  sku: {
    name: 'Standard'
    tier: 'Standard'
    capacity: 1
  }
  properties: {
    zoneRedundant: true
  }
}

// Create an event hub inside the namespace

var eventHubName = 'evh-demo1436'

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

// Grant Listen and Send on our event hub

resource eventHubNamespaceName_eventHubName_ListenSend 'Microsoft.EventHub/namespaces/eventhubs/authorizationRules@2021-01-01-preview' = {
  parent: eventHubNamespaceName_eventHubName
  name: 'ListenSend'
  properties: {
    rights: [
      'Listen'
      'Send'
    ]
  }
  dependsOn: [
    eventHubNamespace
  ]
}

resource testVnet 'Microsoft.Network/virtualNetworks@2021-03-01' existing = {
  name: 'testvnet'
}


resource testsubnet 'Microsoft.Network/virtualNetworks/subnets@2021-03-01' existing = {
  parent: testVnet
  name: 'testsubnet'
}

resource enHubVnetRule 'Microsoft.EventHub/namespaces/virtualnetworkrules@2018-01-01-preview' =  { 
  name: 'vnetName'
  parent: eventHubNamespace
  properties: {
    virtualNetworkSubnetId: testsubnet.id
  }
}

With above code I can only add one particular subnet of a VNet to the VNet entry of azure EventHub resource in networking section using azure bicep.

How do I fetch all subnets of a VNet and add/select them all to the VNet entry of azure EventHub resource in networking section using azure bicep.

devops-admin
  • 1,447
  • 1
  • 15
  • 26

1 Answers1

1

You have to use for loop for the subnet block and virtual-network rule block like below:

// Create an event hub namespace
param eventHubNamespaceName string = 'evhns-demo1436'
resource eventHubNamespace 'Microsoft.EventHub/namespaces@2021-01-01-preview' = {
  name: eventHubNamespaceName
  location: resourceGroup().location
  sku: {
    name: 'Standard'
    tier: 'Standard'
    capacity: 1
  }
  properties: {
    zoneRedundant: true
  }
}

// Create an event hub inside the namespace

param eventHubName string = 'evh-demo1436'

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

// Grant Listen and Send on our event hub

resource eventHubNamespaceName_eventHubName_ListenSend 'Microsoft.EventHub/namespaces/eventhubs/authorizationRules@2021-01-01-preview' = {
  parent: eventHubNamespaceName_eventHubName
  name: 'ListenSend'
  properties: {
    rights: [
      'Listen'
      'Send'
    ]
  }
  dependsOn: [
    eventHubNamespace
  ]
}
param subnets array =[
   'test'
   'mysubnet'
]


param vnetname string = 'test-ansuman'
resource testVnet 'Microsoft.Network/virtualNetworks@2021-03-01' existing = {
  name: vnetname
}


resource testsubnet 'Microsoft.Network/virtualNetworks/subnets@2021-03-01' existing =[for subnet in subnets : {
  parent: testVnet
  name: subnet
}]

resource enHubVnetRule 'Microsoft.EventHub/namespaces/virtualnetworkrules@2018-01-01-preview' = [for (subnet,i) in subnets :{ 
  name: '${vnetname}-${subnet}'
  parent: eventHubNamespace
  properties: {
    virtualNetworkSubnetId:testsubnet[i].id
}
}]

Output:

enter image description here

enter image description here

enter image description here

Ansuman Bal
  • 9,705
  • 2
  • 10
  • 27