Good day,
We are struggling horribly with our infrastructure-as-code journey. We have set up an Azure Container Apps environment and written a bicep script which sets up all needed resource (app with managed identity, key vault, app registration, etc) which all works fine. Problem is, the new app's managed identity needs to be granted a custom role in an Enterprise App, let's call it ExternalAPIProxy.
Granting custom roles is not something which is possible directly via Bicep, it seems, so we have added a step to run an azure client command in order to grant the role. And this is where things have come to an abrupt stop. We seems to be completely unable to set up the DevOps Service Principal running the script correctly in order to grant the role (this is one of many attempts, some involving PowerShell, but all lead to the same issue):
az rest
--method POST
--uri "https://graph.microsoft.com/v1.0/servicePrincipals/$managedIdentity/appRoleAssignments"
--headers 'Content-Type=application/json'
--body "{"principalId": "$managedIdentity", "resourceId": "$externalApiProxyId"}"
This leads to an error:
Forbidden({"error":{"code":"Authorization_RequestDenied","message":"Insufficient privileges to complete the operation.","innerError":{"date":"2024-02-07T13:42:38","request-id":"3a5d779a-3243-4684-9058-c3002837016f","client-request-id":"3a5d779a-3243-4684-9058-c3002837016f"}}})
Which role assignment does the service principal need to be able to grant a custom role? Is there a better way to do this?
We have tried many different ways of adding the role, but seem to get stuck on more or less the same step - which service/identity/user needs what kind of permission to grant custom application roles to the newly created app in azure container service? Fwiw, it's a bog standard .net rest API being deployed, but I do not think that's important in this context.




If you want to use a Service Principal as authorization to run the related command lines to assign custom role to a managed identity on an enterprise app, the Service Principal should have the Privileged Role Administrator or Global Administrator role at the Azure AD (Microsoft Entra ID) level.
For more details, see the following documentation: