Search code examples
azureazure-resource-managerazure-bicepazure-resource-group

How to get the id of a another resource group in bicep?


I have a resouce that is called using the uniquestring function of resourcegroup id, i.e.

var x = '${substring(uniqueString(resourceGroup().id), 0, 8)}'

However, I need to use/call this reource in another resource that exist in another resource group.

How can I exactly do that? resourceId() function doesn't seem to work to get the id of a resource group. or maybe I didn't understand how to use it.

I have also tried to type:

var x = '${substring(uniqueString(resourceGroup('anotherRG').id), 0, 8)}'

But this is also doesn't work and give an error.

Can you please help me?


Solution

  • Have you tried this? Bicep has a way of referencing existing resources.

    resource rg 'Microsoft.Resources/resourceGroups@2023-07-01' existing = {
      name: '<your resource group name>'
      scope: subscription('<your subscription ID>')
    }
    
    output rh string = rg.id
    

    Reference: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/existing-resource