'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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source