t.amsing
t.amsing

Reputation: 189

Azure ARM role assignment for System Assigned Managed Identity fails the first run

My goal is to deploy a logic app with a system managed identity and a role assignment for that identity. Preferably, this is done in one ARM template.

I have a setup that fails the first run, but succeeds successive runs. Correct me if I'm wrong, but I think that the reason for this is that the deployment of the role assignment happens before the managed identity of the logic app is "ready", hence the following error I get the first time that I deploy the template. I don't get this error the second time I deploy the template, probably because the Identity already exists at that time.

{
  "code": "DeploymentFailed",
  "message": "At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.",
  "details": [
    {
      "code": "PrincipalNotFound",
      "message": "Principal *** does not exist in the directory ***."
    }
  ]
}

My template (removed logic app definition for brevity). In this case the identity of the logic app requires access to a storage account which is located in another resource group.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "logicAppName": {
            "type": "string"
        },
        "storageAccountResourceGroup": {
            "type": "String"
        },
        "storageAccountName": {
            "type": "String"
        }
    },
    "variables": {
        "logicAppResourceId": "[resourceId('Microsoft.Logic/workflows', parameters('logicAppName'))]",
        "Storage Blob Data Contributor": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')]"
    },
    "resources": [
        {
            "type": "Microsoft.Logic/workflows",
            "apiVersion": "2017-07-01",
            "name": "[parameters('logicAppName')]",
            "location": "[resourceGroup().location]",
            "identity": {
                "type": "SystemAssigned"
            },
            "properties": {
                "state": "Enabled",
                "definition": {
                    ...    
                }
            }
        },
        {
            "type": "Microsoft.Resources/deployments",
            "name": "[concat('RoleAssignment-', parameters('logicAppName'))]",
            "apiVersion": "2020-06-01",
            "resourceGroup": "[parameters('storageAccountResourceGroup')]",
            "subscriptionId": "[subscription().subscriptionId]",
            "dependsOn": [
                "[parameters('logicAppName')]"
            ],
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "resources": [
                        {
                            "apiVersion": "2018-09-01-preview",
                            "type": "Microsoft.Storage/storageAccounts/providers/roleAssignments",
                            "name": "[concat(parameters('storageAccountName'), '/Microsoft.Authorization/', guid(subscription().subscriptionId, parameters('logicAppName')))]",
                            "properties": {
                                "roleDefinitionId": "[variables('Storage Blob Data Contributor')]",
                                "principalId": "[reference(variables('logicAppResourceId'), '2019-05-01', 'Full').identity.principalId]"
                            }
                        }
                    ]
                }
            }
        }
    ]
}

As you can see in the template, I added a dependsOn on the logic app itself. However that doesn't seem to be sufficient.

Does anyone have a solution for this?

Thank you!

Upvotes: 6

Views: 3515

Answers (1)

t.amsing
t.amsing

Reputation: 189

I found the answer here: https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-template#new-service-principal

Deployment works consistently after adding "principalType": "ServicePrincipal"

Upvotes: 6

Related Questions