Skip to content

Commit 92ae7e2

Browse files
authored
Merge pull request #266343 from rolyon/rolyon-rbac-custom-roles-bicep
[Azure RBAC] Improved custom role creation using Bicep
2 parents 99a8eea + db6c236 commit 92ae7e2

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

articles/role-based-access-control/custom-roles-bicep.md

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: rolyon
66
manager: amycolannino
77
ms.service: role-based-access-control
88
ms.topic: how-to
9-
ms.date: 12/01/2023
9+
ms.date: 02/15/2024
1010
ms.author: rolyon
1111
ms.custom: devx-track-azurepowershell, devx-track-azurecli, devx-track-bicep
1212
#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.
@@ -37,6 +37,8 @@ The Bicep file used in this article is from [Azure Quickstart Templates](https:/
3737

3838
The scope where this custom role can be assigned is set to the current subscription.
3939

40+
A custom role requires a unique ID. The ID can be generated with the [guid()](../azure-resource-manager/bicep/bicep-functions-string.md#guid) function. Since a custom role also requires a [unique display name](custom-roles.md#custom-role-properties) for the tenant, you can use the role name as a parameter for the `guid()` function to create a [deterministic GUID](../azure-resource-manager/bicep/scenarios-rbac.md#name). A deterministic GUID is useful if you later need to update the custom role using the same Bicep file.
41+
4042
:::code language="bicep" source="~/quickstart-templates/subscription-deployments/create-role-def/main.bicep":::
4143

4244
The resource defined in the Bicep file is:
@@ -46,29 +48,39 @@ The resource defined in the Bicep file is:
4648
## Deploy the Bicep file
4749

4850
1. Save the Bicep file as **main.bicep** to your local computer.
49-
1. Deploy the Bicep file using either Azure CLI or Azure PowerShell.
51+
52+
1. Create a variable named **myActions** with the actions for the roleDefinition.
5053

5154
# [CLI](#tab/CLI)
5255

5356
```azurecli-interactive
54-
$myActions='("Microsoft.Resources/resources/read","Microsoft.Resources/subscriptions/resourceGroups/read")'
55-
56-
az deployment sub create --location eastus --name customRole --template-file main.bicep --parameters actions=$myActions
57+
$myActions='["Microsoft.Resources/subscriptions/resourceGroups/read"]'
5758
```
5859
5960
# [PowerShell](#tab/PowerShell)
6061
6162
```azurepowershell-interactive
62-
$myActions = @("Microsoft.Resources/resources/read","Microsoft.Resources/subscriptions/resourceGroups/read")
63+
$myActions = @("Microsoft.Resources/subscriptions/resourceGroups/read")
64+
```
65+
66+
---
67+
68+
1. Deploy the Bicep file using either Azure CLI or Azure PowerShell.
69+
70+
# [CLI](#tab/CLI)
71+
72+
```azurecli-interactive
73+
az deployment sub create --location eastus --name customRole --template-file ./main.bicep --parameters actions=$myActions
74+
```
75+
76+
# [PowerShell](#tab/PowerShell)
6377
78+
```azurepowershell-interactive
6479
New-AzSubscriptionDeployment -Location eastus -Name customRole -TemplateFile ./main.bicep -actions $myActions
6580
```
6681
6782
---
6883
69-
> [!NOTE]
70-
> Create a variable called **myActions** and then pass that variable. Replace the sample actions with the actions for the roleDefinition.
71-
7284
When the deployment finishes, you should see a message indicating the deployment succeeded.
7385
7486
## Review deployed resources
@@ -91,59 +103,42 @@ Get-AzRoleDefinition "Custom Role - RG Reader"
91103

92104
## Update a custom role
93105

94-
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.
106+
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. If you previously created the custom role in Bicep with a unique role ID that is [deterministic](../azure-resource-manager/bicep/scenarios-rbac.md#name), you can use the same Bicep file and specify the custom role by just using the display name.
95107

96-
Here are the changes you would need to make to the previous Bicep file to update the custom role.
97-
98-
1. Include the role ID as a parameter.
99-
100-
```bicep
101-
...
102-
@description('ID of the role definition')
103-
param roleDefName string
104-
...
105-
106-
```
107-
108-
2. Remove the roleDefName variable. You'll get a warning if you have a parameter and variable with the same name.
109-
3. Use Azure CLI or Azure PowerShell to get the roleDefName.
108+
1. Specify the updated actions.
110109

111110
# [CLI](#tab/CLI)
112111

113112
```azurecli-interactive
114-
az role definition list --name "Custom Role - RG Reader"
113+
$myActions='["Microsoft.Resources/resources/read","Microsoft.Resources/subscriptions/resourceGroups/read"]'
115114
```
116115
117116
# [PowerShell](#tab/PowerShell)
118117
119118
```azurepowershell-interactive
120-
Get-AzRoleDefinition -Name "Custom Role - RG Reader"
119+
$myActions = @(""Microsoft.Resources/resources/read","Microsoft.Resources/subscriptions/resourceGroups/read"")
121120
```
122121
123-
---
122+
---
124123
125-
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.
124+
1. Use Azure CLI or Azure PowerShell to update the custom role.
126125
127126
# [CLI](#tab/CLI)
128127
129128
```azurecli-interactive
130-
$myActions='("Microsoft.Resources/resources/read","Microsoft.Resources/subscriptions/resourceGroups/read")'
131-
132-
az deployment sub create --location eastus --name customrole --template-file main.bicep --parameters actions=$myActions roleDefName="name-id" roleName="Custom Role - RG Reader"
129+
az deployment sub create --location eastus --name customrole --template-file ./main.bicep --parameters actions=$myActions roleName="Custom Role - RG Reader"
133130
```
134131
135132
# [PowerShell](#tab/PowerShell)
136133
137134
```azurepowershell-interactive
138-
$myActions = @(""Microsoft.Resources/resources/read","Microsoft.Resources/subscriptions/resourceGroups/read"")
139-
140-
New-AzSubscriptionDeployment -Location eastus -Name customrole -TemplateFile ./main.bicep -actions $myActions -roleDefName "name-id" -roleName "Custom Role - RG Reader"
135+
New-AzSubscriptionDeployment -Location eastus -Name customrole -TemplateFile ./main.bicep -actions $myActions -roleName "Custom Role - RG Reader"
141136
```
142137
143138
---
144139
145140
> [!NOTE]
146-
> It may take several minutes for the updated role definition to be propagated.
141+
> It may take several minutes for the updated custom role to be propagated.
147142
148143
## Clean up resources
149144

0 commit comments

Comments
 (0)