matt_lethargic
matt_lethargic

Reputation: 2796

Azure ARM Role Assignment different Resource Group

I'm trying to create an ARM template that has a VM, I want the VM to have AcrPull role assignment to a Container Registry that is in a different resource group. I'm setting the scope property to the ID of the ACR (i got this from https://resources.azure.com).

{
      "apiVersion": "2017-09-01",
      "type": "Microsoft.Authorization/roleAssignments",
      "name": "[guid(resourceGroup().id, 'stuff')]",
      "properties": {
        "roleDefinitionId": "[variables(parameters('builtInRoleType'))]",
        "principalId": "[reference(variables('vmResourceId'), '2017-12-01', 'Full').identity.principalId]",
        "scope": "[concat(/subscriptions/abc123-x123-4bef-84ec-1716390f231b/resourceGroups/someresroucegroupname/providers/Microsoft.ContainerRegistry/registries/someacrname"   
      },
      "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]"
      ]
    }

I keep getting an error:

"The request to create role assignment '694829aa-cee5-4fe3-bf94-3ae372c14f26' is not valid. Role 
assignment scope '/subscriptions/abc123-x123-4bef-84ec-1716390f231b' must match the scope specified on
the URI '/subscriptions/abc123-x123-4bef-84ec-1716390f231b/resourcegroups/myresourcegroupname'.

Is this even possible?

Upvotes: 4

Views: 3070

Answers (2)

Arno Pretorius
Arno Pretorius

Reputation: 1

It can work with the "type": "Microsoft.Authorization/roleAssignments" Your name just needs to be something like "name": "[guid(resourceId('Microsoft.Web/sites', parameters('webAppName')), variables('AcrPullId'), resourceGroup().id)]"

webAppName: WebApp name allowing to do the pull AcrPullId: variable created with the MS existing roleId for ACRpull

Upvotes: 0

4c74356b41
4c74356b41

Reputation: 72191

you need to wrap this with a nested template call like so:

{
    "type": "Microsoft.Resources/deployments",
    "name": "nested-role-assignment",
    "apiVersion": "2017-05-10",
    "resourceGroup": "your_resource_group",
    "subscriptionId": "your_subscription_id",
    "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]"
    ],
    "properties": {
        "mode": "Incremental",
        "template": {
            "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "resources": [
                {
                    "apiVersion": "2017-09-01",
                    "type": "Microsoft.Authorization/roleAssignments",
                    "name": "[guid(resourceGroup().id, 'stuff')]",
                    "properties": {
                        "roleDefinitionId": "[variables(parameters('builtInRoleType'))]",
                        "principalId": "[reference(variables('vmResourceId'), '2017-12-01', 'Full').identity.principalId]",
                        "scope": "[concat(/subscriptions/abc123-x123-4bef-84ec-1716390f231b/resourceGroups/someresroucegroupname/providers/Microsoft.ContainerRegistry/registries/someacrname"
                    }
                }
            ]
        }
    }
}

so, effectively, deploy this directly to the resource group where ACR resides.

ps. my name\type combination:

"type": "Microsoft.ContainerRegistry/registries/providers/roleAssignments",
"name": "[concat('ACR_NAME_GOES_HERE/Microsoft.Authorization/', guid(resourceGroup().id, 'acr'))]",

Upvotes: 6

Related Questions