Assigning Service Principals to Groups and Roles with the Azure CLI

The more I use Azure the more often I find myself needing to assign various managed identities / service principals to various groups and roles, and while that can be done in the Portal, it's cumbersome and I'd prefer to automate it.

So in this post I'll sharing a few Azure CLI commands that should prove useful whenever you're configuring Service Principals.

Getting a service principal's object id

Suppose you know the name of the service principal, but not the "object id", which is required for assigning it to groups and roles. You can use a filter with the az ad sp list command to find that service principal and then a query to pick out just the object id.

Note that you should avoid trying to use the query parameter to find the matching name, as that will likely not find it as it only applies to the first page of results .

Note that the object id is different from the app id. If you do need the app id for any reason you just need to change the query parameter:

Adding to a group

Suppose we want to add the service principal to a group. We need the group id to do that, and if we need to look it up, we can do so with the az ad group list command and using a filter .

Then the az ad group member add command allows us to add the object id of our service principal to the group.

Creating a role assignment

If we want to create a role assignment, then as well as knowing the user we're assigning the role to and the name of the role, we also need to provide a " scope " for that to apply to. This is typically a long / delimited path to an Azure resource. So for a KeyVault it might look like this:

You can of course construct this string yourself, but actually this is quite often just the "ID" of the resource as returned by the Azure CLI. So we could get the above value with the following command:

And now that we have the scope, we can simply use the az role assignment create to assign the role to our service principal, and we can pass the role name directly (in this example it's "Key Vault Administrator"):

Hope this proves useful to you.

az role assignment create scope

Using the Azure CLI To Update and Manage User Permissions

The Azure CLI is a great tool for scripting updates to user permissions. In this guide, we'll walk through the basics of roles and groups and the common commands.

az role assignment create scope

If you need to make user permission updates in an automated way, the Azure CLI can be a great option. In this article, we will be outlining the two main sources of user permissions, roles and groups, and how they intersect.

With a combination of roles and groups, you can maintain granular permissions across many different projects while adhering to the principle of least privilege .

Understanding Roles with Azure RBAC

Azure roles are a flexible way to designate user permissions. With Azure RBAC (role-based access control), you can unlock access to certain resources and actions by assigning a user to a certain role, which comes with an accompanying set of permissions.

These are some examples of common built-in roles: 

  • Contributor: Can create and manage Azure resources
  • Owner: Access to all resources and can extend access to others
  • Reader: Can view only existing Azure resources
  • User Access Administrator: Can manage access to Azure resources

You can narrow access further by assigning a user with a role in relation to a specific scope (e.g. resource group, application id, etc.). If you need a unique combination of permissions and expect to have similar use cases in the future, you can also create custom roles by providing either a JSON role definition file or a PSRoleDefinition object as input.

azure logo

Assigning Roles with the Azure CLI

You will likely need to update someone’s role if they are new to your organization or have been assigned to a new project; or inversely, if they are leaving or no longer need access. Here are the steps for making these changes with the Azure CLI.

Adding a Role to a User

To assign a role to a user in Azure, you can use the “ az role assignment create ” command. You have to specify three components, the assignee, the role, and the resource groups or scope of access. In the following example, we’re assigning Reader access (role definition) to user John Smith for the scope of a certain resource group.

Removing a Role from a User

Next, to remove the role from the same user, we would use the “ az role assignment delete ” command. This command uses the exact same parameters:

These commands should enable you to make these role updates manually, or script a repeatable workflow for new employees or new projects.

Understanding Groups in Azure 

In GCP or AWS, Identify Access Management (IAM) groups are a way to extend access and authorization services/APIs to a team. Groups in Azure serve the same purpose, but Azure is slightly different in that groups are created directly using Azure’s Active Directory (AD). 

You can create a new group using the command “ az ad group create ” , and specify a display name and a mail nickname. Here’s an example:

Management of IAM groups in Azure involves the same kinds of tasks you would perform in typical user groups, whether it’s adding or deleting individual users, giving them specific levels of IAM permissions, or managing groups of users as a whole, among many others.

For example, you can assign a group with a certain role for a certain scope or resource group. To do this, you’ll first need to get the object ID for the group using this command:

The object ID will be a string of numbers in this format:

“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”

Now that you have the group ID, you can use the “ az role assignment create ” command to assign a role to that group:

The combination of user roles and group roles allows for organizations to have a flexible and secure solution for permissions as your company grows or projects change.

az role assignment create scope

Adding or Removing a Users to Groups

If you want to add a user to a group, you’ll need to run the “ az ad group member add ”   command. You will need to plug in values for a group parameter (either the specific group id or display name) and a member-id parameter.

Here is an example of that command:

Removing a member from a group uses the same parameters, and uses the “ az ad group member remove ” command instead:

You can also use the same parameters with the “ az ad group member check ” command to check whether the member was removed from the group.

Automate Permission Updates with Blink

Most likely, as your organization grows, changing and updating permissions and policies will take up more time. Instead of having to look up the specific command for each of these actions, you could use a low-code tool like Blink to handle tasks like this in a couple clicks.

Get started with Blink today to see how easy automation can be.

Automate your security operations everywhere.

Blink is secure, decentralized, and cloud-native. 
Get modern cloud and security operations today.

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

What is the default RBAC scope used when assigning a role in Azure with the CLI?

This is the documentation for the az role assignment create command: https://docs.microsoft.com/en-us/cli/azure/role/assignment?view=azure-cli-latest#az-role-assignment-create

--score is an optional parameter. This is what the documentation says about it:

Scope at which the role assignment or definition applies to, e.g., /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333, /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup, or /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM.

As you can see, it doesn't say what the default value for this parameter is. I can't find it anywhere, so I found myself forced to ask here.

Adrian's user avatar

  • I can't select my own answer as the correct one because I have to wait 2 days, but that's the correct answer anyways. –  Adrian Jan 22, 2022 at 4:25

https://docs.microsoft.com/en-us/azure/role-based-access-control/role-assignments-cli#step-4-assign-role

Apparently, when the --scope parameter is not provided its value depends on whether the --resource-group parameter is provided or not. If you provide that parameter, then it's like if you specified the resource group scope. Else, the subscription scope is assumed.

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged azure entra-id azure-cli ..

  • The Overflow Blog
  • You should keep a developer’s journal
  • Would you board a plane safety-tested by GenAI?
  • Featured on Meta
  • Our Partnership with OpenAI
  • What deliverables would you like to see out of a working group?

Hot Network Questions

  • Reference request for Bessel function of the second kind with matrix argument
  • Square root symbols that don't match
  • Why Faonte called "small" and "big" dg-nerves?
  • Is it legal to use GPL code in plugins of a proprietary app?
  • Story about a huge project in Hell?
  • Baker's percentage symbol -- % in a loaf using TikZ
  • Is there a problem with having a combined login/register screen?
  • Should I remove a reference after applying to job?
  • What is the name of the grammatical function of "there" in "there is"?
  • Is this a solvable Numberlink?
  • What is "soda asbestos"?
  • Why does Professer Binns always call students by different names?
  • Polygon and trace arrows
  • Array aligned with top of item label
  • Cold Bee and Old Beer
  • How do floating plants get their trace nutrients?
  • Does using electricity from the grid in the U.S. result in more greenhouse gas emissions than using propane?
  • Alpha Texture Brush isn't working?
  • Why would aliens use Kaiju to cause an apocalypse instead of more effective weapons?
  • Why don't electrons occupy infinite degenerate states with the same energy?
  • Why do people believe that John the Baptist and Jesus never knew one another?
  • How to Solve a Linear System of Equations with Absolute Values
  • Any trick to installing a double electric wall oven "whip" so it doesn't bunch up but slides backwards when the oven is pushed back into the cabinet?
  • Do I have a cleaner way to assign a parametrized json string to a bash variable?

az role assignment create scope

New-AzRoleAssignment

In this Azure PowerShell article, we will discuss the syntax and usage of the New-AzRoleAssignment Azure PowerShell cmdlet with examples.

Table of Contents

Wrapping Up

New-AzRoleAssignment is a very good PowerShell command that you can use to assign an RBAC role to a specific principal for a specified scope. Let’s discuss the Syntax of the New-AzRoleAssignment Azure PowerShell cmdlet.

Below is the syntax of the New-AzRoleAssignment PowerShell command.

Now, let’s discuss some examples of how to use the New-AzRoleAssignment PowerShell command.

You can execute the below Azure PowerShell command to grant a contributor role to the user “[email protected]” under the resource group “MyNewResGrp” scope, and the role assignment is available for delegation.

Below Azure PowerShell cmdlet can help you to grant owner access to the user “[email protected]” to the “TsInfoweb” website.

You may also like following the articles below

  • Get-AzRoleAssignment
  • Get-AzRoleDefinition

In this Azure PowerShell article, we discussed the syntax and usage of the New-AzRoleAssignment Azure PowerShell cmdlet with examples. Thanks for reading this article !!!

Microsoft Azure

I am Rajkishore, and I have over 14 years of experience in Microsoft Azure and AWS, with good experience in Azure Functions, Storage, Virtual Machine, Logic Apps, PowerShell Commands, CLI Commands, Machine Learning, AI, Azure Cognitive Services, DevOps, etc. Not only that, I do have good real-time experience in designing and developing cloud-native data integrations on Azure or AWS, etc. I hope you will learn from these practical Azure tutorials. Read more .

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Role Assignments - Create

Create or update a role assignment by scope and name.

URI Parameters

Request body, permissions.

To call this API, you must be assigned a role that has the following permissions. For more information, see Azure built-in roles .

Microsoft.Authorization/roleAssignments/write

Azure Active Directory OAuth2 Flow

Type: oauth2 Flow: implicit Authorization URL: https://login.microsoftonline.com/common/oauth2/authorize

Create role assignment for resource

Sample request.

To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue

Sample Response

Create role assignment for resource group, create role assignment for subscription, definitions, error additional info.

The resource management error additional info.

Error Detail

The error detail.

Error Response

Error response

Principal Type

The principal type of the assigned principal ID.

Role Assignment

Role Assignments

Role Assignment Create Parameters

Role assignment create parameters.

Additional resources

Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

az role assignment create --assignee "${PRINCIPAL_ID}" --role 'Contributor' --scope "{RESOURCE_GROUP_ID}" #24701

@jiasli

djohn99 commented Nov 21, 2022

@ghost

yonzhan commented Nov 22, 2022

Sorry, something went wrong.

@yonzhan

jiasli commented Nov 22, 2022

@RakeshMohanMSFT

djohn99 commented Nov 22, 2022 • edited

@djohn99

jiasli commented Dec 19, 2022 • edited

No branches or pull requests

@jiasli

IMAGES

  1. What is Azure role-based access control (Azure RBAC)?

    az role assignment create scope

  2. Assign Azure AD roles at different scopes

    az role assignment create scope

  3. Assign Azure roles using Azure Resource Manager templates

    az role assignment create scope

  4. Create custom roles in Azure AD role-based access control

    az role assignment create scope

  5. What is Azure role-based access control (Azure RBAC)?

    az role assignment create scope

  6. Assign Azure roles using the Azure portal

    az role assignment create scope

VIDEO

  1. Colorado woman finishes astronaut training

  2. DO NOT MORTGAGE THE PLANS OF GOD || MORNING WATCH PROPHETIC PRAYERS WITH PROPHETESS AKUWAA

  3. How To Live the Kingdom Life

  4. EDUBED-109(B) Unit-1 Guidance Programme, Concept, Principles, Need, Scope, Significance, Types

  5. Your Best Images from 2023!

  6. Brief explain important aspects of super critical fluid chromatography. PGDAC MCH-002

COMMENTS

  1. az role assignment

    az role assignment create: Create a new role assignment for a user, group, or service principal. Core GA az role assignment delete: Delete role assignments. Core GA az role assignment list: List role assignments. Core GA az role assignment list-changelogs: List changelogs for role assignments. Core GA az role assignment update

  2. Assign Azure roles using Azure CLI

    Step 1: Determine who needs access. You can assign a role to a user, group, service principal, or managed identity. To assign a role, you might need to specify the unique ID of the object. The ID has the format: 11111111-1111-1111-1111-111111111111. You can get the ID using the Azure portal or Azure CLI. User.

  3. Understand scope for Azure RBAC

    Scope and ARM templates. A role assignment is a special type in Azure Resource Manager called an extension resource. An extension resource is a resource that adds to another resource's capabilities. They always exist as an extension (like a child) of another resource. For example, a role assignment at subscription scope is an extension resource ...

  4. How to assign role to an Azure service principal from different

    steps taken. create service principal without assignment. az ad sp create-for-rbac -n sp-terraform-001 --skip-assignment. assign contributor role for current sp for current subscription. az role assignment create --assignee <appid> --role Contributor --scope /subscriptions/<sub-id>. *assign contributor role to current sp for a different ...

  5. role-assignments-cli.md

    To address this scenario, you should specify the principal type when creating the role assignment. To assign a role, use az role assignment create, specify a value for --assignee-object-id, and then set --assignee-principal-type to ServicePrincipal.

  6. Assigning Service Principals to Groups and Roles with the Azure CLI

    Adding to a group. Suppose we want to add the service principal to a group. We need the group id to do that, and if we need to look it up, we can do so with the az ad group list command and using a filter. --query "[].id" -o tsv. Then the az ad group member add command allows us to add the object id of our service principal to the group.

  7. Using the Azure CLI To Update and Manage User Permissions

    To assign a role to a user in Azure, you can use the "az role assignment create" command. You have to specify three components, the assignee, the role, and the resource groups or scope of access. In the following example, we're assigning Reader access (role definition) to user John Smith for the scope of a certain resource group.

  8. entra id

    Apparently, when the --scope parameter is not provided its value depends on whether the --resource-group parameter is provided or not. If you provide that parameter, then it's like if you specified the resource group scope.

  9. New-AzRoleAssignment

    New-AzRoleAssignment is a very good PowerShell command that you can use to assign an RBAC role to a specific principal for a specified scope. Let's discuss the Syntax of the New-AzRoleAssignment Azure PowerShell cmdlet. Syntax. Below is the syntax of the New-AzRoleAssignment PowerShell command.

  10. Assign Azure roles using Azure PowerShell

    To assign a role consists of three elements: security principal, role definition, and scope. Step 1: Determine who needs access. You can assign a role to a user, group, service principal, or managed identity. To assign a role, you might need to specify the unique ID of the object. The ID has the format: 11111111-1111-1111-1111-111111111111. You ...

  11. Assign Multiple RoleAssignments at Different Scopes

    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 ...

  12. Should be able to assign a role at scope of "/" (root) #24536

    az role assignment create --role 'Owner' --assignee ${principal_id} --scope "/" Actual Behaviour. ... # The following resource will create a role assigment at root scope ("/") for the service principal created by this module. # It is required to use the az api provider, because the azurerm_role_assignment resource of azurerm provider does not ...

  13. Creating a Service Principal with the Azure CLI

    Now that we have an AD application, we can create our service principal with az ad sp create-for-rbac (RBAC stands for role based access control). We need to supply an application id and password, so we could create it like this: # choose a password for our service principal. spPassword= "My5erv1c3Pr1ncip@l1!"

  14. Perform Role Assignments on Azure Resources from Azure Pipelines

    az storage account create -n testroleassignmentsa--resource-group ado-role-assignment-test-rg--location westus --sku Standard_LRS The output of the above command is shown below.

  15. Azure CLI を使用して Azure ロールを割り当てる

    Azure ロールベースのアクセス制御 (Azure RBAC) は、Azure のリソースに対するアクセスを管理するために使用する承認システムです。. アクセス権を付与するには、特定のスコープでユーザー、グループ、サービス プリンシパル、またはマネージド ID にロールを ...

  16. New-AzRoleAssignment (Az.Resources)

    Use the New-AzRoleAssignment command to grant access. Access is granted by assigning the appropriate RBAC role to them at the right scope. To grant access to the entire subscription, assign a role at the subscription scope. To grant access to a specific resource group within a subscription, assign a role at the resource group scope. The subject of the assignment must be specified. To specify a ...

  17. az role assignment create scope default to subscription if empty

    az feedback auto-generates most of the information requested below, as of CLI version 2.0.62 Describe the bug A clear and concise description of what the bug is. Summary Using azure-cli (in actual version of 2.0.79) and az role assignmen...

  18. Role Assignments

    Learn more about Authorization service - Create or update a role assignment by scope and name.

  19. Cannot assign Azure Role for cosmos db

    8. The RBAC on Azure Cosmosdb is built similar to Azure RBAC where there is more granular control on the data operations instead of Azure resource management. The permission model describes in detail. During implementations where SDK are used to interact with the dataplane (e.g, writing data or reading data), the application should be provided ...

  20. az role assignment create --assignee "${PRINCIPAL_ID}" --role ...

    Please review and update as needed. Describe the bug Command Name az role assignment create Errors: 'RESOURCE_GROUP_ID' Traceback (most recent call last): python3/dis... Skip to content. Navigation Menu ... az role assignment create --assignee {} --role {} --scope {} Expected Behavior Environment Summary. Linux-5.15.-1023-azure-x86_64-with ...