Zveratko
Zveratko

Reputation: 2810

Bicep module outputs wrong resource id

I have subscription scoped bicep deployment, including some modules to create content for resource group. In one module I create VNET and I try to get back its resource ID output vnetId string = vnet.id, on another module I'd like to use this value and I have set properly the dependsOn. But there is problem, seems the output ID doesn't contain resource groups part /subscriptions/<subs>/providers/Microsoft.Network/virtualNetworks/vnet-weu/subnets/portal

Does it have something with using subscription level deployment? (az deployment sub create ...)

UPDATE: I just found this remark, which does explain the missing resource group part, but does not explain why vnet.id in module with targetScope = 'resourceGroup' returns subscription resource id instead of "full" resource id.

UPDATE - CODE

network.bicep

targetScope = 'resourceGroup'

param vnetName string
param location string = resourceGroup().location

resource vnet 'Microsoft.Network/virtualNetworks@2020-11-01' = {
  name: vnetName
  location: location
  ... 
}

output vnetId string = vnet.id

main.bicep

targetScope = 'subscription'


module networkRgResources 'network.bicep' = {
  name: 'networkRgResources'
  scope: resourceGroup(networkRg.name)
  params: {
    location: location
    vnetName: vnetName
  }
}

module sharedRgResources 'shared.bicep' = {
  name: 'sharedRgResources'
  scope: resourceGroup(sharedRg.name)
  params: {
    vnetId: networkRgResources.outputs.vnetId
  }
  dependsOn: [
    networkRgResources
  ]
}

inside shared module the vnetId is without resource groups

UPDATE 2 I cannot simulate this, but seems I have some strange race conditions in my code, now it seems it is working fine.

Upvotes: 0

Views: 1896

Answers (1)

Kartik Bhiwapurkar
Kartik Bhiwapurkar

Reputation: 5165

• Using the ‘resourceId(resourceGroup().name, 'Microsoft.Network/virtualNetworks', vnetName)’ can be a solution. If you want to try a different approach, then you can use something like below: -

Vnetdeploy.bicep

 targetScope = 'subscription'
 resource rg 'Microsoft.Resources/resourceGroups@2021-01-01' existing = {
 name: 'xxxxx-xxxxxx'
  }
 module vnet './vnet.bicep' = {
  name: 'vnetdeployment'
 scope: rg
params: {
   vnetname: 'vnet2'
 }
  }

output vnetID string = vnet.outputs.virtualNetworkID

vnet.bicep. i.e., VNET Module

param vnetname string
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2020-08-01' = {
 name: vnetname
 location: 'EastUS'
 properties: {
  addressSpace: {
  addressPrefixes: [
    '10.0.2.0/24'
  ]
}
subnets: [
  {
    name: 'subnet1'
    properties: {
      addressPrefix: '10.0.2.0/27'
    }
  }
]
}
  }
 output virtualNetworkID string = virtualNetwork.id

Output: -

Bicep template output

Upvotes: 1

Related Questions