Assign Multiple RoleAssignments at Different Scopes #5926
-
I want to create user ID (Managed Identities) and assign them multiple rbac at different scopes. For instance: ID A would have Owner and Contributor roles at rg-app In order to achieve this,
The main question: For example: targetScope = 'subscription'
// ...
resource vnet 'Microsoft.Network/virtualNetworks@2021-02-01' existing = {
name: 'vnet'
scope: resourceGroup(subscription().id, 'rg_hub_n')
}
module idDeployment 'components/id/id.bicep' = {
name: 'id-aks-deploy'
scope: appRg
params: {
id_n: id_n
tags: tags
}
}
// only allows rg scope
module idRoleAssignmentDeployment 'components/id/roleAssignments.bicep' = [for id_role in id_roles_arr : {
name: 'id-role-assignment-deploy-${id_role}'
scope: resourceGroup(id_scope_obj.sub, id_scope_obj.rg)
params: {
name: guid(subscription().id, env, id_role)
principalId: idDeployment.outputs.principalId
roleDefinitionId: '/subscriptions/${id_scope_obj.sub}/providers/Microsoft.Authorization/roleDefinitions/${id_role}'
}
dependsOn: [
idDeployment
]
}]
// The root resource scope must match that of the Bicep file. To deploy a resource to a different root scope, use a module.
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2020-08-01-preview' = {
name: 'some random unique name'
scope: vnet
properties: {
principalId: 'principalId'
roleDefinitionId: 'roleDefinitionId'
principalType: 'ServicePrincipal'
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
This is possible... I would recommend to read over this thread of some of the complexities. Also this blog which I believe was based on some of the discussions in that thread via @JFolberth https://blog.johnfolberth.com/nested-loops-in-azure-bicep-with-an-rbac-example/ Then please share back any questions or follows up that you have here. Essentially to do a role assignment, you deploy into the Scope of the resource E.g. the resource group, So if you have 2 resource groups, you need to have a module to deploy into the scope of those resources groups Etc. Here is also an alternate less complex scenario on this than the above thread. Since it's just a single resource. |
Beta Was this translation helpful? Give feedback.
-
Hi Team, |
Beta Was this translation helpful? Give feedback.
This is possible... I would recommend to read over this thread of some of the complexities.
#5678
Also this blog which I believe was based on some of the discussions in that thread via @JFolberth
https://blog.johnfolberth.com/nested-loops-in-azure-bicep-with-an-rbac-example/
Then please share back any questions or follows up that you have here.
Essentially to do a role assignment, you deploy into the Scope of the resource E.g. the resource group,
Then you use an existing reference, then you can do the role assignment at that scope of that resource.
So if you have 2 resource groups, you need to have a module to deploy into the scope of those resources groups Etc.
Here is also an alternate …