How to refer a parent in module when using loop for creating resources? #13826
-
So I'm trying to write something that will allow me to create topics and subscriptions in a loop and while I think I've achieved that thanks to code founded here, I'm facing some problems and I'n not sure how to resolve them. It looks like sometimes bicep fails because it try to create subscription when topics creation is not yet finished. When I re-run my pipeline, it works just fine. I guess this is the reason:
The error I get is: I'm not sure how to avoid this error. Is there a way to create particular subscription only if corresponding topic was created? I guess I cannot simply add Main.bicepparam:
Here is the code for main.bicep
module file:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You can and I don't see why you wouldn't do this at this point. Changing to the following is valid bicep and would fix the issue for now @description('Name of the Service Bus namespace')
param serviceBusNamespaceName string
@description('Location for all resources.')
param location string = resourceGroup().location
param topicsAndSubscriptions array
resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2022-01-01-preview' = {
name: serviceBusNamespaceName
location: location
sku: {
name: 'Premium'
}
properties: {}
}
resource serviceBusTopic 'Microsoft.ServiceBus/namespaces/topics@2022-10-01-preview' = [
for topic in topicsAndSubscriptions: {
parent: serviceBusNamespace
name: topic.name
properties: {}
}
]
module subs 'modules/subscription.bicep' = [
for (topic, i) in topicsAndSubscriptions: {
name: '${topic.name}-subs'
params: {
servicebusNamespaceName: serviceBusNamespaceName
topicName: topic.name
subscriptions: topic.subscriptions
}
dependsOn: [
serviceBusTopic[i]
]
}
] |
Beta Was this translation helpful? Give feedback.
-
You already got great suggestions, but I would suggest to consolidate the topics and subscriptions deployment. In the Bicep template below, you see the combined the infrastructure for topics and subscriptions into a module. By doing this, some of the dependency and loop complexity is resolved because it will be handled by the Azure Resource Manager instead. Main bicep template: @description('Name of the Service Bus namespace')
param serviceBusNamespaceName string
@description('Location for all resources.')
param location string = resourceGroup().location
param topicsAndSubscriptions array
resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2022-01-01-preview' = {
name: serviceBusNamespaceName
location: location
sku: {
name: 'Premium'
}
properties: {}
}
module subs 'topicsAndSubscriptions.module.bicep' = [
for topic in topicsAndSubscriptions: {
name: '${topic.name}-subs'
params: {
servicebusNamespaceName: serviceBusNamespace.name
topicName: topic.name
subscriptions: topic.subscriptions
}
}
] Topics and Subscriptions module: param servicebusNamespaceName string
param topicName string
param subscriptions array = ['asubscription', 'anotherone']
resource serviceBusTopic 'Microsoft.ServiceBus/namespaces/topics@2022-10-01-preview' = {
name: '${servicebusNamespaceName}/${topicName}'
properties: {}
resource topicSubscription 'subscriptions' = [
for subscriptionName in subscriptions: {
name: subscriptionName
properties: {}
}
]
} In the Let me know if you have questions 💪 |
Beta Was this translation helpful? Give feedback.
You already got great suggestions, but I would suggest to consolidate the topics and subscriptions deployment. In the Bicep template below, you see the combined the infrastructure for topics and subscriptions into a module. By doing this, some of the dependency and loop complexity is resolved because it will be handled by the Azure Resource Manager instead.
Main bicep template: