|
| 1 | +--- |
| 2 | +title: Create or update Azure custom roles using Bicep - Azure RBAC |
| 3 | +description: Learn how to create or update Azure custom roles using Bicep and Azure role-based access control (Azure RBAC). |
| 4 | +services: role-based-access-control,azure-resource-manager |
| 5 | +author: schaffererin |
| 6 | +ms.service: role-based-access-control |
| 7 | +ms.topic: how-to |
| 8 | +ms.workload: identity |
| 9 | +ms.date: 07/01/2022 |
| 10 | +ms.author: v-eschaffer |
| 11 | +ms.custom: devx-track-azurepowershell |
| 12 | + |
| 13 | +#Customer intent: As an IT admin, I want to create custom and/or roles using Bicep so that I can start automating custom role processes. |
| 14 | +--- |
| 15 | + |
| 16 | +# Create or update Azure custom roles using Bicep |
| 17 | + |
| 18 | +If the [Azure built-in roles](built-in-roles.md) don't meet the specific needs of your organization, you can create your own [custom roles](custom-roles.md). This article describes how to create or update a custom role using Bicep. |
| 19 | + |
| 20 | +[!INCLUDE [About Bicep](../../includes/resource-manager-quickstart-bicep-introduction.md)] |
| 21 | + |
| 22 | +To create a custom role, you specify a role name, role permissions, and where the role can be used. In this article, you create a role named _Custom Role - RG Reader_ with resource permissions that can be assigned at a subscription scope or lower. |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +To create a custom role, you must have permissions to create custom roles, such as [Owner](built-in-roles.md#owner) or [User Access Administrator](built-in-roles.md#user-access-administrator). |
| 27 | + |
| 28 | +You also must have an active Azure subscription. If you don't have one, you can create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 29 | + |
| 30 | +## Review the Bicep file |
| 31 | + |
| 32 | +The Bicep file used in this article is from [Azure Quickstart Templates](https://azure.microsoft.com/resources/templates/create-role-def). The Bicep file has four parameters and a resources section. The four parameters are: |
| 33 | + |
| 34 | +- Array of actions with a default value of `["Microsoft.Resources/subscriptions/resourceGroups/read"]`. |
| 35 | +- Array of `notActions` with an empty default value. |
| 36 | +- Role name with a default value of `Custom Role - RG Reader`. |
| 37 | +- Role description with a default value of `Subscription Level Deployment of a Role Definition`. |
| 38 | + |
| 39 | +The scope where this custom role can be assigned is set to the current subscription. |
| 40 | + |
| 41 | +:::code language="bicep" source="~/quickstart-templates/subscription-deployments/create-role-def/main.bicep"::: |
| 42 | + |
| 43 | +The resource defined in the Bicep file is: |
| 44 | + |
| 45 | +- [Microsoft.Authorization/roleDefinitions](/azure/templates/Microsoft.Authorization/roleDefinitions) |
| 46 | + |
| 47 | +## Deploy the Bicep file |
| 48 | + |
| 49 | +1. Save the Bicep file as **main.bicep** to your local computer. |
| 50 | +1. Deploy the Bicep file using either Azure CLI or Azure PowerShell. |
| 51 | + |
| 52 | + # [CLI](#tab/CLI) |
| 53 | + |
| 54 | + ```azurecli-interactive |
| 55 | + $myActions='("Microsoft.Resources/resources/read","Microsoft.Resources/subscriptions/resourceGroups/read")' |
| 56 | +
|
| 57 | + az deployment sub create --location eastus --name customRole --template-file main.bicep --parameters actions=$myActions |
| 58 | + ``` |
| 59 | +
|
| 60 | + # [PowerShell](#tab/PowerShell) |
| 61 | +
|
| 62 | + ```azurepowershell-interactive |
| 63 | + $myActions = @("Microsoft.Resources/resources/read","Microsoft.Resources/subscriptions/resourceGroups/read") |
| 64 | +
|
| 65 | + New-AzSubscriptionDeployment -Location eastus -Name customRole -TemplateFile ./main.bicep -actions $myActions |
| 66 | + ``` |
| 67 | +
|
| 68 | + --- |
| 69 | +
|
| 70 | + > [!NOTE] |
| 71 | + > Create a variable called **myActions** and then pass that variable. Replace the sample actions with the actions for the roleDefinition. |
| 72 | +
|
| 73 | + When the deployment finishes, you should see a message indicating the deployment succeeded. |
| 74 | +
|
| 75 | +## Review deployed resources |
| 76 | +
|
| 77 | +Use the Azure portal, Azure CLI, or Azure PowerShell to verify that the custom role was created. |
| 78 | +
|
| 79 | +# [CLI](#tab/CLI) |
| 80 | +
|
| 81 | +```azurecli-interactive |
| 82 | +az role definition list --name "Custom Role - RG Reader" |
| 83 | +``` |
| 84 | + |
| 85 | +# [PowerShell](#tab/PowerShell) |
| 86 | + |
| 87 | +```azurepowershell-interactive |
| 88 | +Get-AzRoleDefinition "Custom Role - RG Reader" |
| 89 | +``` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## Update a custom role |
| 94 | + |
| 95 | +Similar to creating a custom role, you can update an existing custom role using Bicep. To update a custom role, you need to specify the role you want to update. |
| 96 | + |
| 97 | +Here are the changes you would need to make to the previous Bicep file to update the custom role. |
| 98 | + |
| 99 | +1. Include the role ID as a parameter. |
| 100 | + |
| 101 | + ```bicep |
| 102 | + ... |
| 103 | + @description('ID of the role definition') |
| 104 | + param roleDefName string |
| 105 | + ... |
| 106 | +
|
| 107 | + ``` |
| 108 | +
|
| 109 | +2. Remove the roleDefName variable. You'll get a warning if you have a parameter and variable with the same name. |
| 110 | +3. Use Azure CLI or Azure PowerShell to get the roleDefName. |
| 111 | +
|
| 112 | + # [CLI](#tab/CLI) |
| 113 | +
|
| 114 | + ```azurecli-interactive |
| 115 | + az role definition list --name "Custom Role - RG Reader" |
| 116 | + ``` |
| 117 | +
|
| 118 | + # [PowerShell](#tab/PowerShell) |
| 119 | +
|
| 120 | + ```azurepowershell-interactive |
| 121 | + Get-AzRoleDefinition -Name "Custom Role - RG Reader" |
| 122 | + ``` |
| 123 | +
|
| 124 | +--- |
| 125 | +
|
| 126 | +4. Use Azure CLI or Azure PowerShell to deploy the updated Bicep file, replacing **\<name-id\>** with the roleDefName, and replacing the sample actions with the updated actions for the roleDefinition. |
| 127 | +
|
| 128 | + # [CLI](#tab/CLI) |
| 129 | +
|
| 130 | + ```azurecli-interactive |
| 131 | + $myActions='("Microsoft.Resources/resources/read","Microsoft.Resources/subscriptions/resourceGroups/read")' |
| 132 | +
|
| 133 | + az deployment sub create --location eastus --name customrole --template-file main.bicep --parameters actions=$myActions roleDefName="name-id" roleName="Custom Role - RG Reader" |
| 134 | + ``` |
| 135 | +
|
| 136 | + # [PowerShell](#tab/PowerShell) |
| 137 | +
|
| 138 | + ```azurepowershell-interactive |
| 139 | + $myActions = @(""Microsoft.Resources/resources/read","Microsoft.Resources/subscriptions/resourceGroups/read"") |
| 140 | +
|
| 141 | + New-AzSubscriptionDeployment -Location eastus -Name customrole -TemplateFile ./main.bicep -actions $myActions -roleDefName "name-id" -roleName "Custom Role - RG Reader" |
| 142 | + ``` |
| 143 | +
|
| 144 | + --- |
| 145 | +
|
| 146 | + > [!NOTE] |
| 147 | + > It may take several minutes for the updated role definition to be propagated. |
| 148 | +
|
| 149 | +## Clean up resources |
| 150 | +
|
| 151 | +When no longer needed, use the Azure portal, Azure CLI, or Azure PowerShell to remove the custom role. |
| 152 | +
|
| 153 | +# [CLI](#tab/CLI) |
| 154 | +
|
| 155 | +```azurecli-interactive |
| 156 | +az role definition delete --name "Custom Role - RG Reader" |
| 157 | +``` |
| 158 | + |
| 159 | +# [PowerShell](#tab/PowerShell) |
| 160 | + |
| 161 | +```azurepowershell-interactive |
| 162 | +Remove-AzRoleDefinition -Name "Custom Role - RG Reader" |
| 163 | +``` |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## Next steps |
| 168 | + |
| 169 | +- [Understand Azure role definitions](role-definitions.md) |
| 170 | +- [Bicep documentation](../azure-resource-manager/bicep/overview.md) |
0 commit comments