5

How to output resource id in bicep, while creating the subnet how do we get the output string, virtual network syntax s shown below

resource virtualNetwork 'Microsoft.Network/virtualNetworks@2019-11-01' = {
  name: vnetName
  location: resourceGroup().location
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.0.0.0/16'
      ]
    }
    subnets: [
      {
        name: 'subnetpoc-1'
        properties: {
          addressPrefix: '10.0.3.0/24'
        }
      }
      {
        name: 'subnetnetpoc-2'
        properties: {
          addressPrefix: '10.0.4.0/24'
        }
      }
    ]
  }
}

// output subnet string = ""
Farrukh Normuradov
  • 1,032
  • 1
  • 11
  • 32
cloudcop
  • 957
  • 1
  • 9
  • 15

1 Answers1

12

You can use the resourceId function for that:

param vnetName string

resource virtualNetwork 'Microsoft.Network/virtualNetworks@2019-11-01' = {
  name: vnetName
  ...
}

// Return the 1st subnet id
output subnetId1 string = resourceId('Microsoft.Network/VirtualNetworks/subnets', vnetName, 'subnetpoc-1')

// Return the 2nd subnet id
output subnetId2 string = resourceId('Microsoft.Network/VirtualNetworks/subnets', vnetName, 'subnetpoc-2')

// Return as array
output subnetIdsArray array = [
  resourceId('Microsoft.Network/VirtualNetworks/subnets', vnetName, 'subnetpoc-1')
  resourceId('Microsoft.Network/VirtualNetworks/subnets', vnetName, 'subnetpoc-2')
]

// Return as object
output subnetIdsObject object = {
  subnetId1: resourceId('Microsoft.Network/VirtualNetworks/subnets', vnetName, 'subnetpoc-1')
  subnetId2: resourceId('Microsoft.Network/VirtualNetworks/subnets', vnetName, 'subnetpoc-2')
}



Thomas
  • 24,234
  • 6
  • 81
  • 125
  • Thank you, it does return the resourceid for a single subnet, but when i tried to get both the subnets id i get an error "'resourceId': the type 'Microsoft.Network/VirtualNetworks/subnets' requires '2' resource name argument(s)" resourceId(resourceType: string, resourceName0 : string, resourceName1 : string, resourceName2 : string, ... : string): string – cloudcop Oct 28 '21 at 17:26
  • output subnetId string = resourceId('Microsoft.Network/VirtualNetworks/subnets', vnetName, 'subnetpoc-1','subnetpoc-2') – cloudcop Oct 28 '21 at 17:26
  • Edited my answer – Thomas Oct 28 '21 at 18:01
  • Thank you Wonderful, – cloudcop Oct 28 '21 at 20:25
  • Please accept the answer if it s ok on your side – Thomas Oct 28 '21 at 22:31
  • Yes, thank you already did – cloudcop Nov 08 '21 at 17:15