Azure / bicep

Bicep is a declarative language for describing and deploying Azure resources
MIT License
3.26k stars 755 forks source link

Using the resourceGroup function that references a parameter, omits the resourcegroup from being outputted in the compiled ARM template. #15651

Open mwillebrandscreates opened 4 days ago

mwillebrandscreates commented 4 days ago

Bicep version Bicep CLI version 0.31.92 (b06509316a)

Describe the bug Using the resourceGroup function that references a parameter, omits the resourcegroup from being outputted in the compiled ARM template.

To Reproduce Create the following bicep files: main.bicep

targetScope = 'subscription'
param testParam bool

module myModule 'module.bicep' = {
  name: 'myModule'
  scope: testParam ? resourceGroup('rg-a') : resourceGroup('rg-b')
}

module.bicep

targetScope = 'resourceGroup'

resource storage 'Microsoft.Storage/storageAccounts@2023-05-01' = {
  kind: 'BlobStorage'
  location: 'West Europe'
  name: 'storage-${uniqueString('scopetest')}'
  sku: {
    name: 'Standard_LRS'
  }
}

Run az bicep build --file main.bicep Open the compiled ARM template and note that there's no resourcegroup parameter that contains an expression (there's no resourcegroup parameter at all), and thus the resource will be deployed on subscription level.

Additional context This might be related to https://github.com/Azure/bicep/pull/15570 and https://github.com/Azure/bicep/issues/15517. These issues should be solved in the latest version, but do not seem to fix the fact that the resourcegroup is omitted in the compiled ARM template. When not directly referencing a parameter but a static value (like true) the resourcegroup is added to the ARM template.

GABRIELNGBTUC commented 3 days ago

Same solution as there https://github.com/Azure/bicep/issues/15270#issuecomment-2401399279 (Do the ternary in a variable and reference your variable inside the resourceGroup() call).

Also please upvote this https://github.com/Azure/bicep/issues/1876 so that we can get the Bicep or ARM team to unblock this.

mwillebrandscreates commented 3 days ago

@GABRIELNGBTUC you just saved my life. For the bicep team, as long as the example above isn't supported, is it possible to make sure that the linter returns an error when specifying the above scenario?