How to setup EventHub disasterRecoveryConfigs using Azure bicep?

Viewed 116

I have created a module 'resources.bicep' to create event hub namespace in two regions.

resource eventHubNamespace 'Microsoft.EventHub/namespaces@2021-11-01' = {
  name: resourceName
  location: location
  sku: {
    name:'Standard'
    tier:'Standard'
    capacity:1
  }
}

resource eventHub 'Microsoft.EventHub/namespaces/eventhubs@2021-11-01' = if (shortRegion == 'wus2') {
  name: 'city-temprature'
  parent: eventHubNamespace
  properties: {
    messageRetentionInDays: 1
    partitionCount: 2
  }
}

From the parent bicep file I run the module as

module weatherWest 'resources.bicep' = {
  name:'westResources'
  scope:resourceGroup('${name}-wus2')
  params: {
    name: name
    shortRegion: 'wus2'
    location: 'westus2'
  }
}

module weatherEast 'resources.bicep' = {
  name:'eastResources'
  scope:resourceGroup('${name}-eus2')
  params: {
    name: name
    shortRegion: 'eus2'
    location: 'eastus2'
  }
} 

How do I setup the GeoPairing? I have not found a way to call Microsoft.EventHub/namespaces/disasterRecoveryConfigs@2021-11-01 from the parent bicep file.

Code is located in this branch https://github.com/xavierjohn/SearchIndexDisasterRecoverNearRealTime/blob/bicep/bicep/weatherResources.bicep

2 Answers

Per the docs,

you need to specify a parent resource. You can look up an existing resource with the existing keyword.

Something along these lines should work.

resource primary 'Microsoft.EventHub/namespaces@2021-11-01' existing = {
  name: 'primaryEventHubName'

resource secondary 'Microsoft.EventHub/namespaces@2021-11-01' existing = {
  name: 'secondaryEventHubName'

resource symbolicname 'Microsoft.EventHub/namespaces/disasterRecoveryConfigs@2021-11-01' = {
  name: 'foo'
  parent: primary
  properties: {
    alternateName: 'string'
    partnerNamespace: secondary.id
  }
}

I got help from the Azure Bicep team and currently there is no way to pass a resource as output but they are working on a proposal. For now there is a trick that will work so till the elegant solution comes out, use existing and set dependsOn on the Geo Pairing fragment. The end code looks like below.

module allResources 'resources.bicep' = [for location in locations : {
  name:'allResources'
  scope:resourceGroup('${name}-${location.shortRegion}')
  params: {
    name: name
    shortRegion: location.shortRegion
    location: location.region
  }
}]

resource primaryEventHubNamespace 'Microsoft.EventHub/namespaces@2021-11-01' existing = {
  name: '${name}wus2'
}

resource disasterRecoveryConfigs 'Microsoft.EventHub/namespaces/disasterRecoveryConfigs@2021-11-01' = {
  name: name
  parent: primaryEventHubNamespace
  properties: {
    partnerNamespace: resourceId('${name}-eus2', 'Microsoft.EventHub/namespaces', '${name}eus2')
  }
  dependsOn: [
    allResources
  ]
}
Related