Coder Perfect

Take the Shared Service Bus. Using Bicep to Program AccessKey

Problem

Bicep is what I’m utilizing to generate Azure resources. A service bus is one of these resources, and it is defined as follows:

resource service_bus 'Microsoft.ServiceBus/namespaces@2021-01-01-preview' = {
  name: '${service_bus_name}${uniqueString(service_bus_name)}'
  location: resourceGroup().location
  sku: {
    name: 'Standard'
    tier: 'Standard'
  }
  properties: {}
}

I then want to use this service bus in another resource, and this is the connection string I now have:

name: 'AzureWebJobsServiceBus'
value: 'Endpoint=sb://${service_bus.name}.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<hardcoded_key>'          

I’m not sure how I’m going to get around the hard programmed key. I tried using listKeys in the following way:

SharedAccessKey=${listKeys(service_bus.id, service_bus.apiVersion).value[0].primaryKey}

But this doesn’t work, and variations on it don’t work either.

Asked by gatapia

Solution #1

If my memory serves me correctly, the Service Bus listKeys bicep function corresponds to Namespaces – Authorization Rules – List Keys.

In the listKeys method call, you must specify AuthorizationRules/authorizationRuleName. RootManageSharedAccessKey can be used for authorizationRuleName.

I’m not sure if this would work (my first time playing around with bicep), but you can give it a try:

var listKeysEndpoint = '${service_bus.id}/AuthorizationRules/RootManageSharedAccessKey'
SharedAccessKey=${listKeys(listKeysEndpoint, service_bus.apiVersion).primaryKey}

Answered by Gaurav Mantri

Solution #2

Alternatively, you might try:

var serviceBusEndpoint = '${service_bus.id}/AuthorizationRules/RootManageSharedAccessKey'
var serviceBusConnectionString = listKeys(serviceBusEndpoint, service_bus.apiVersion).primaryConnectionString

to obtain the Service Bus connection string

Answered by duren

Post is based on https://stackoverflow.com/questions/68404000/get-the-service-bus-sharedaccesskey-programatically-using-bicep