|
| 1 | +--- |
| 2 | +title: "Quickstart: Add an Azure role assignment using an Azure Resource Manager template - Azure RBAC" |
| 3 | +description: Learn how to grant access to Azure resources for a user at resource group scope using Azure Resource Manager templates and Azure role-based access control (Azure RBAC). |
| 4 | +services: role-based-access-control,azure-resource-manager |
| 5 | +author: rolyon |
| 6 | +manager: mtillman |
| 7 | +ms.service: role-based-access-control |
| 8 | +ms.topic: quickstart |
| 9 | +ms.custom: subject-armqs |
| 10 | +ms.workload: identity |
| 11 | +ms.date: 05/21/2020 |
| 12 | +ms.author: rolyon |
| 13 | + |
| 14 | +#Customer intent: As a new user, I want to see how to grant access to resources by using Azure Resource Manager template so that I can start automating role assignment processes. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +# Quickstart: Add an Azure role assignment using an Azure Resource Manager template |
| 19 | + |
| 20 | +[Azure role-based access control (Azure RBAC)](overview.md) is the way that you manage access to Azure resources. In this quickstart, you create a resource group and grant a user access to create and manage virtual machines in the resource group. This quickstart uses a Resource Manager template to grant the access. |
| 21 | + |
| 22 | +[!INCLUDE [About Azure Resource Manager](../../includes/resource-manager-quickstart-introduction.md)] |
| 23 | + |
| 24 | +If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | + |
| 28 | +To add role assignments, you must have: |
| 29 | + |
| 30 | +* `Microsoft.Authorization/roleAssignments/write` and `Microsoft.Authorization/roleAssignments/delete` permissions, such as [User Access Administrator](built-in-roles.md#user-access-administrator) or [Owner](built-in-roles.md#owner) |
| 31 | + |
| 32 | +## Create a role assignment |
| 33 | + |
| 34 | +To add a role assignment, you must specify three elements: security principal, role definition, and scope. For this quickstart, the security principal is you or another user in your directory, the role definition is [Virtual Machine Contributor](built-in-roles.md#virtual-machine-contributor), and the scope is a resource group that you specify. |
| 35 | + |
| 36 | +### Review the template |
| 37 | + |
| 38 | +The template used in this quickstart is from [Azure Quickstart templates](https://azure.microsoft.com/resources/templates/101-rbac-builtinrole-resourcegroup/). The template has three parameters and a resources section. In the resources section, notice that it has the three elements of a role assignment: security principal, role definition, and scope. |
| 39 | + |
| 40 | +:::code language="json" source="~/quickstart-templates/101-rbac-builtinrole-resourcegroup/azuredeploy.json" highlight="30-32"::: |
| 41 | + |
| 42 | +### Deploy the template |
| 43 | + |
| 44 | +1. Sign in to the [Azure portal](https://portal.azure.com). |
| 45 | + |
| 46 | +1. Determine your email address that is associated with your Azure subscription. Or determine the email address of another user in your directory. |
| 47 | + |
| 48 | +1. Open Azure Cloud Shell for PowerShell. |
| 49 | + |
| 50 | +1. Copy and paste the following script into Cloud Shell. |
| 51 | + |
| 52 | + ```azurepowershell |
| 53 | + $resourceGroupName = Read-Host -Prompt "Enter a resource group name (i.e. ExampleGrouprg)" |
| 54 | + $emailAddress = Read-Host -Prompt "Enter an email address for a user in your directory" |
| 55 | + $location = Read-Host -Prompt "Enter a location (i.e. centralus)" |
| 56 | + |
| 57 | + $roleAssignmentName = New-Guid |
| 58 | + $principalId = (Get-AzAdUser -Mail $emailAddress).id |
| 59 | + $roleDefinitionId = (Get-AzRoleDefinition -name "Virtual Machine Contributor").id |
| 60 | + $templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-rbac-builtinrole-resourcegroup/azuredeploy.json" |
| 61 | + |
| 62 | + New-AzResourceGroup -Name $resourceGroupName -Location $location |
| 63 | + New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -roleAssignmentName $roleAssignmentName -roleDefinitionID $roleDefinitionId -principalId $principalId |
| 64 | + ``` |
| 65 | +
|
| 66 | +1. Enter a resource group name such as ExampleGrouprg. |
| 67 | +
|
| 68 | +1. Enter an email address for yourself or another user in your directory. |
| 69 | +
|
| 70 | +1. Enter a location for the resource group such as centralus. |
| 71 | +
|
| 72 | +1. If necessary, press Enter to run the New-AzResourceGroupDeployment command. |
| 73 | +
|
| 74 | + The [New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup) command creates a new resource group and the [New-AzResourceGroupDeployment](/powershell/module/az.resources/new-azresourcegroupdeployment) command deploys the template to add the role assignment. |
| 75 | +
|
| 76 | + You should see output similar to the following: |
| 77 | +
|
| 78 | + ```azurepowershell |
| 79 | + PS> New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -roleAssignmentName $roleAssignmentName -roleDefinitionID $roleDefinitionId -principalId $principalId |
| 80 | + |
| 81 | + DeploymentName : azuredeploy |
| 82 | + ResourceGroupName : ExampleGrouprg |
| 83 | + ProvisioningState : Succeeded |
| 84 | + Timestamp : 5/22/2020 9:01:30 PM |
| 85 | + Mode : Incremental |
| 86 | + TemplateLink : |
| 87 | + Uri : https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-rbac-builtinrole-resourcegroup/azuredeploy.json |
| 88 | + ContentVersion : 1.0.0.0 |
| 89 | + |
| 90 | + Parameters : |
| 91 | + Name Type Value |
| 92 | + ==================== ========================= ========== |
| 93 | + roleAssignmentName String {roleAssignmentName} |
| 94 | + roleDefinitionID String 9980e02c-c2be-4d73-94e8-173b1dc7cf3c |
| 95 | + principalId String {principalId} |
| 96 | + |
| 97 | + Outputs : |
| 98 | + DeploymentDebugLogLevel : |
| 99 | + ``` |
| 100 | +
|
| 101 | +## Review deployed resources |
| 102 | +
|
| 103 | +1. In the Azure portal, open the resource group you created. |
| 104 | +
|
| 105 | +1. In the left menu, click **Access control (IAM)**. |
| 106 | +
|
| 107 | +1. Click the **Role assignments** tab. |
| 108 | +
|
| 109 | +1. Verify that the **Virtual Machine Contributor** role is assigned to the user you specified. |
| 110 | +
|
| 111 | +  |
| 112 | +
|
| 113 | +## Clean up resources |
| 114 | +
|
| 115 | +To remove the role assignment and resource group you created, follow these steps. |
| 116 | +
|
| 117 | +1. Copy and paste the following script into Cloud Shell. |
| 118 | +
|
| 119 | + ```azurepowershell |
| 120 | + $emailAddress = Read-Host -Prompt "Enter the email address of the user with the role assignment to remove" |
| 121 | + $resourceGroupName = Read-Host -Prompt "Enter the resource group name to remove (i.e. ExampleGrouprg)" |
| 122 | + |
| 123 | + $principalId = (Get-AzAdUser -Mail $emailAddress).id |
| 124 | + |
| 125 | + Remove-AzRoleAssignment -ObjectId $principalId -RoleDefinitionName "Virtual Machine Contributor" -ResourceGroupName $resourceGroupName |
| 126 | + Remove-AzResourceGroup -Name $resourceGroupName |
| 127 | + ``` |
| 128 | + |
| 129 | +1. Enter the email address of the user with the role assignment to remove. |
| 130 | +
|
| 131 | +1. Enter the resource group name to remove such as ExampleGrouprg. |
| 132 | +
|
| 133 | +1. If necessary, press Enter to run the Remove-AzResourceGroup command. |
| 134 | +
|
| 135 | +1. Enter **Y** to confirm that you want to remove the resource group. |
| 136 | +
|
| 137 | +## Next steps |
| 138 | +
|
| 139 | +> [!div class="nextstepaction"] |
| 140 | +> [Tutorial: Grant a user access to Azure resources using Azure PowerShell](tutorial-role-assignments-user-powershell.md) |
0 commit comments