bicep icon indicating copy to clipboard operation
bicep copied to clipboard

Can property names, esp userAssignedIdentities, be dynamic?

Open lr90 opened this issue 6 years ago • 5 comments

Using this as an example of assigning userAssignedIdentities to a resource the JSON structure looks like:

  "identity": {
    "type": "userAssigned",
    "userAssignedIdentities": {
      "/subscriptions/01234567-89AB-CDEF-0123-456789ABCDEF/resourceGroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myID": {}
    }

Using Bicep I can use the literal value of the userAssignedIdentity like:

    identity: {
        type: 'userAssigned'
        userAssignedIdentities: {
          '/subscriptions/01234567-89AB-CDEF-0123-456789ABCDEF/resourcegroups/myResourceGroup/providers/microsoft.managedidentity/userassignedidentities/myID':{}
        }
    }

but given I've created the identity earlier in the same bicep file using:

resource mi 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30'={
  name: 'myID'
  location: location
}

it seems natural to be able to reference this resource, perhaps like this:

    identity: {
        type: 'userAssigned'
        userAssignedIdentities: {
          mi.id :{}
        }
    }

However this gives a BCP018 error parsing the '.' in the property name.

Of course if I've missed a trick please advise, always learning, but hard coding the identity into my bicep file feels so wrong

Cheers

lr90 avatar Nov 04 '20 11:11 lr90

You need to use string interpolation syntax here:

    identity: {
        type: 'userAssigned'
        userAssignedIdentities: {
          '${mi.id}' :{}
        }
    }

and if the managed identity already exists, you can do:

var uamiId = resourceId(subId, rgName, 'Microsoft.ManagedIdentity/userAssignedIdentities', uamiName)

resource dScript 'Microsoft.Resources/deploymentScripts@2019-10-01-preview' = {
  name: 'scriptWithStorage'
  location: location
  kind: 'AzureCLI'
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '${uamiId}': { }
    }
  }
  ...

from this example: https://github.com/Azure/bicep/blob/main/docs/examples/101/deployment-script-with-storage/main.bicep#L27

alex-frankel avatar Nov 04 '20 16:11 alex-frankel

Thanks Alex but while that looks perfect it isn't working for me :(

Using the example I get this warning in VSCode BCP067

while attempting a build give me this

/home/trevor/source/bicep/dScript.bicep(27,7) : Error BCP067: String interpolation in not supported in object keys.

I'm running Bicep CLI version 0.1.37-alpha (f88c6d95ad)

lr90 avatar Nov 04 '20 17:11 lr90

ohh, it's because you are on an old version. If you install 0.1.226 you should be good to go

alex-frankel avatar Nov 04 '20 17:11 alex-frankel

OK, build now works but I'm getting errors in VS Code (launched from within WSL) but that's a different issue and I see it comes with helpful reporting tools

lr90 avatar Nov 04 '20 18:11 lr90

Sounds good - looks like I can close this

alex-frankel avatar Nov 04 '20 18:11 alex-frankel