-
Notifications
You must be signed in to change notification settings - Fork 757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditionally define module scope not working. #15270
Comments
Scopes must be constant at compile time so what is happening is that the first clause of your conditional is used at compile time. Since what you want is "Use the current subscription/resource group if they are not specified", you can simply use the last clause of your condition for the scope. param subscriptionId string = subscription().subscriptionId
param resourceGroupName string = resourceGroup().name
module mod 'nsg.bicep' = {
name: guidId
scope: resourceGroup(subscriptionId, resourceGroupName)
params: {
Location: resourceGroup().location
NsgName: ''
SecurityRules: [
]
}
}
This does not safe guard against providing only the subscription like the If you can guarantee that there is no resource group name overlaps between subscriptions, you could also write the following to safe guard against that case: param subscriptionId string = subscription().subscriptionId
param resourceGroupName string = resourceGroup().name
var subscriptionScope = resourceGroupName == resourceGroup().name && subscriptionId != subscription().subscriptionId ? subscription().subscriptionId : subscriptionId
module mod 'nsg.bicep' = {
name: guidId
scope: resourceGroup(subscriptionScope, resourceGroupName)
params: {
Location: resourceGroup().location
NsgName: ''
SecurityRules: [
]
}
}
There is also already an open issue for this if you could upvote it #1876 so that the bicep team is more likely to look into a fix. |
Thanks @GABRIELNGBTUC for providing the workarounds! Closing this as a duplicate of #1876. |
Bicep version
Bicep CLI version 0.30.3 (2f0e78d)
Describe the bug
I want to define the scope for a module conditionally based on the subscriptionId and rgName parameters. If both subscriptionId and rgName are defined, the scope should be resourceGroup(subscriptionId, rgName). If only rgName is defined, the scope should be resourceGroup(rgName). If neither parameter is defined, use the current scope, which is resourceGroup().
The below code should work but for some reason it is not. I returns the deployment scope resource group instead of provided subscriptionId and rgName parameters. I noticed a similar issue #7367 and a workaround is provided there but that is with only one paramater. Is ther a way to pass both subscriptionID and rgName as parameters, when the scope is in a different subscription.
To Reproduce
param subscription string = 'xxx-xxx-xxx-xx-x-xx'
param rgname string = 'test-rg'
resource vmexists 'Microsoft.ScVmm/virtualMachines@2023-04-01-preview' existing = {
name: 'test-vm'
scope: empty(subscription) && empty(rgname) ? resourceGroup() : empty(subscription) ? resourceGroup(rgname) : resourceGroup(subscription,rgname)
}
output resourceId string = vmexists.id
The text was updated successfully, but these errors were encountered: