azureazure-notificationhubazure-bicep

Bicep - Can't get connection string from Notification hub through listkeys Error BCP182


I am trying to get an array of Notification hub connection strings however I always got an error message from listKeys expression

BCP182: : This expression is being used in the for-body of the variable "notificationHubConfig", which requires values that can be calculated at the start of the deployment

Here is my code. First I have an array call notificationHubs in my environment yaml file

notificationhubs:
'["tenantId1:nameSpaceNotificationHub1:Hubname1","tenantId2:nameSpaceNotificationHub2:Hubname2"]'

In module bicep file. First I get the namespaces of Notification Hubs. They are all existing resource. Then I create an array of notifition hub connection string through listKeys function. And the error happen due to the listKeys expression.

resource namespace 'Microsoft.NotificationHubs/namespaces@2017-04-01' existing = [for i in range(0, length(notificationhubs)): {
  name: '${split(notificationhubs[i], ':')[1]}'
  scope: resourceGroup('${notificationHubResourceGroupName}')
}]

var notificationHubConfig = [for i in range(0, length(notificationhubs)): {
  name: notificationhubs[i]
  value: '${listKeys('${namespace[i].id}/AuthorizationRules/RootManageSharedAccessKey', namespace[i].apiVersion).primaryConnectionString}'
}]

I have tested all the inputs of the expression and it seem correct (for example namespace[i].id, namespace[i].apiVersion ...etc). Could anyone can help please. Thank you in advance


Solution

  • The bicep logic that replaces variables in your case does not work for loops.

    There is an open issue it on GitHub: Variable substitution logic does not work for variables with loop expressions