Home > Back-end >  azure bicep: for-loop in variable definition not possible?
azure bicep: for-loop in variable definition not possible?

Time:04-25

I'm creating a bicep module like this:

param permission object

var keys = [for key in permission.keys: key]
var secretes= [for secret in permission.secrets: secret]
var certificates = [for certificate in permission.certificates: certificate]

where "permissions" is an object containing these 3 arrays.
This works nicely, but I would prefer something like this (one complex variable instead of 3 separate arrays):

var x = {
  keys: [for key in permission.keys: key]
  secrets: [for secret in permission.secrets: secret]   
  certificates: [for certificate in permission.certificates: certificate]
}

Syntactically this is not permitted. Interestingly, when doing the same thing within a resource definition, the same syntax is valid, e.g.:

resource EventHubAuthorization 'Microsoft.EventHub/namespaces/eventhubs/authorizationRules@2021-01-01-preview' = {
  name: '${EventHub.name}/${AuthorizationName}'
  properties: {
    rights: [for right in Rights: right]
  }
}

Is this simply how things are or am I missing something and what I want could be done - just differently?

br volker

CodePudding user response:

This is not supported according to the error message:

For-expressions are not supported in this context. For-expressions may be used as values of resource, module, variable, and output declarations, or values of resource and module properties.bicep(BCP138)

You could always do something like that:

param permission object

var keys = [for key in permission.keys: key]
var secrets= [for secret in permission.secrets: secret]
var certificates = [for certificate in permission.certificates: certificate]

var x = {
  keys: keys
  secrets: secrets
  certificates: certificates
}

But here the x variable seems to look like more or less the same as the permission parameter.

  • Related