Bicep 中的模块之间可以使用 input 和 output 参数传递数据。因此,可以在父模块中定义事件中心名称空间,并在子模块中接收该值作为输入参数。以下是代码示例:
父模块:
param eventHubNamespaceName string = 'myEventHubNamespace'
resource eventHubNamespace 'Microsoft.EventHub/namespaces@2018-01-01-preview' = {
name: eventHubNamespaceName
location: resourceGroup().location
}
module eventHubModule './eventHub.bicep' = {
name: 'eventHubModule'
params: {
eventHubNamespaceId: eventHubNamespace.id
}
}
子模块(eventHub.bicep):
param eventHubNamespaceId string
// Derive the event hub namespace name from its ID (this may vary by resource type)
// In this example, the ID is in the form /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.EventHub/namespaces/{namespaceName}
var eventHubNamespaceName = split('/', eventHubNamespaceId)[8]
resource eventHub 'Microsoft.EventHub/namespaces/eventhubs@2018-01-01-preview' = {
name: '${eventHubNamespaceName}/myEventHub'
location: resourceGroup().location
dependsOn: [
eventHubNamespace
]
}
在此示例中,父模块定义了一个事件中心名称空间,并将其 ID 作为参数传递给子模块。子模块使用 ID 推导出名称空间名称,并使用该名称创建事件中心资源。此示例中还使用了 dependsOn 属性来确保在创建事件中心资源之前先创建名称空间资源。