【问题标题】:How do I create multiple topics/queues in multiple servicebuses with bicep?如何使用二头肌在多个服务总线中创建多个主题/队列?
【发布时间】:2022-10-13 15:15:00
【问题描述】:
在使用二头肌,更具体地说是数组时,我不太了解父组件和子组件之间的关系。
我得到的错误是:部署模板验证失败:'54'行和'9'列的资源'Microsoft.Resources/deployments/p6vklkczz4qlm'在模板中定义了多次。
错误很清楚我只是不明白我猜的解决方案。
主二头肌
param servicebuses array = [
'servicebus_dev'
'servicebus_acc'
'servicebus_prod'
]
resource servicebusNamespace 'Microsoft.ServiceBus/namespaces@2021-11-01' = [for servicebus in servicebuses: {
location: location
name: servicebus
sku:{
name: 'Standard'
}
}]
module topicModule 'topicsModule.bicep' = [for servicebus in servicebuses:{
name: uniqueString('topic')
params:{
parentResource: servicebus
}
}]
主题模块.二头肌
param topics array = [
'topic1'
'topic2'
'topic3'
]
param parentResource string
resource topicResource 'Microsoft.ServiceBus/namespaces/topics@2021-11-01' = [for topic in topics : {
name: topic
}]
【问题讨论】:
标签:
azure
azureservicebus
azure-resource-manager
azure-servicebus-topics
azure-bicep
【解决方案1】:
在模块中创建主题有点麻烦。您必须使用现有关键字获取命名空间,然后您可以向主题添加父关系以在给定命名空间中创建它。
resource servicebusNamespace 'Microsoft.ServiceBus/namespaces@2021-11-01' existing = {
name: parentResource
}
resource topicResource 'Microsoft.ServiceBus/namespaces/topics@2021-11-01' = [for topic in topics : {
parent: servicebusNamespace
name: topic
}]
然后你必须让你的 topicModules 名称依赖于所选的 servicebus,并为 servicebus 命名空间添加一个dependsOn,这样二头肌就会知道首先部署命名空间。
module topicModule 'topicsModule.bicep' = [for servicebus in servicebuses:{
name: uniqueString(servicebus)
dependsOn:[
servicebusNamespace
]
params:{
parentResource: servicebus
}
}]
我猜你用虚拟值替换了你的真实服务总线命名空间名称,但以防万一,确保使用更可能是全局唯一的名称并且不要使用 _ 字符,它不允许在服务名称中总线命名空间。
【解决方案2】:
除了接受的答案。
主题是服务命名空间的子资源,因此资源名称如下所示:
servicebus-namespace-name/topic-name
topicsModule.bicep 文件:
param servicebusName string
param topics array = [
'topic1'
'topic2'
'topic3'
]
resource topicResource 'Microsoft.ServiceBus/namespaces/topics@2021-11-01' = [for topic in topics: {
name: '${servicebusName}/${topic}'
}]
在主文件中,您可以像这样调用模块:
module topicModule 'topicsModule.bicep' = [for (servicebus, i) in servicebuses: {
name: uniqueString(servicebus)
params: {
servicebusName: servicebusNamespace[i].name
}
}]
这里不需要指定dependsOn,因为它是由二头肌在编译时自动生成的。