【问题标题】:How to select and add multiple subnets of a VNet in networking section of an azure eventHub using bicep如何使用二头肌在 azure eventHub 的网络部分中选择和添加 VNet 的多个子网
【发布时间】:2021-11-01 20:16:24
【问题描述】:

如何使用 azure bicep 在 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 条目中。

如何获取 VNet 的所有子网并使用 azure bicep 将它们全部添加/选择到网络部分中 azure EventHub 资源的 VNet 条目。

【问题讨论】:

    标签: azure azure-bicep


    【解决方案1】:

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

    输出:

    【讨论】:

    • 谢谢老哥,你发的帖子已经成功了。如何在每个 VNet 中使用两个不同的 VNet 和不同的子网做同样的事情?
    • 我想在这种情况下您还必须为 vnet 添加一个 for 循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2022-07-06
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    相关资源
    最近更新 更多