如何使用 bicep 在 azure eventHub 的网络部分 select 和添加 VNet 的多个子网

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

如何使用 azure bicep select 在 azure eventHub 资源的网络部分中添加 VNet 的多个子网。

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

使用上面的代码,我只能使用 azure bicep 将 VNet 的一个特定子网添加到网络部分中 azure EventHub 资源的 VNet 条目。

如何使用 azure bicep 将 VNet 的所有子网和 add/select 全部提取到网络部分中 azure EventHub 资源的 VNet 条目。

您必须对 subnet 块和 virtual-network 规则块使用 for 循环,如下所示:

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

输出: