Custom Role Definitions

Earn 25 points (50 with Pro) in two steps

  1. ① Read through the lesson — each section gets a ✓ as you scroll through it.
  2. ② When every section has a ✓, tap Complete lesson.

0 of 12 read · keep scrolling

✦ See fewer ads and earn double points — 50 a lesson instead of 25 — with Pro

Implementing Access Management for Azure Resources: Custom Role Definitions

Introduction: Why Custom Roles Matter in Azure

In the world of cloud infrastructure, security is not just about keeping bad actors out; it is about ensuring that the people and services inside your environment have exactly the access they need—and nothing more. This concept is formally known as the Principle of Least Privilege (PoLP). When you first start working with Azure, you are introduced to Azure Role-Based Access Control (RBAC) through built-in roles like "Contributor," "Owner," or "Reader." While these roles cover many common scenarios, they are often too broad for mature organizations. If you give a developer the "Contributor" role, they can delete production databases, modify network configurations, or change security settings, even if they only need to deploy a specific web application.

Custom Role Definitions bridge this gap by allowing you to define granular permissions tailored to the specific functions of your team members or automated processes. By crafting custom roles, you move from a "one-size-fits-all" security model to a precise, auditable, and manageable framework. This lesson will guide you through the conceptual foundation of Azure RBAC, the anatomy of a role definition, the practical steps to create and assign these roles, and the best practices to keep your environment secure over the long term.

Not read yet

The Foundation: Understanding Azure RBAC Components

Before we dive into creating custom roles, we must understand the three core components of any access management decision in Azure: the Security Principal, the Role Definition, and the Scope.

  1. Security Principal: This is the "who." It can be a user (a human with an Azure AD identity), a group (a collection of users), a service principal (an application or automated script), or a managed identity (an identity assigned to an Azure resource like a Virtual Machine).
  2. Role Definition: This is the "what." It is a collection of permissions that define what actions are allowed or disallowed. A custom role definition is simply a JSON file that explicitly lists these operations.
  3. Scope: This is the "where." It defines the boundaries of the access. Azure resources are organized in a hierarchy: Management Groups, Subscriptions, Resource Groups, and individual Resources. An access assignment at a higher level (like a Subscription) inherits down to all child resources (like Resource Groups and VMs).

Callout: Built-in vs. Custom Roles Built-in roles are maintained by Microsoft and cover standard administrative tasks. They are easy to use but cannot be modified. Custom roles allow you to tailor permissions to your specific organizational needs, but they require ongoing maintenance. Use built-in roles whenever possible, and only create custom roles when the built-in roles are too permissive or do not provide the specific access required for a unique workflow.

Not read yet

The Anatomy of a Role Definition

A custom role in Azure is defined by a JSON structure. Understanding this structure is essential because it is the "source code" for your permissions. When you create a role via the Azure Portal, the portal actually generates this JSON behind the scenes.

A standard role definition JSON contains the following primary fields:

  • Name: The display name of the role (e.g., "Virtual Machine Operator").
  • IsCustom: A boolean value. For custom roles, this is always set to true.
  • Description: A brief summary of what the role does.
  • Actions: A list of strings representing the operations the role is allowed to perform.
  • NotActions: A list of strings representing operations that are excluded from the Actions list. This is useful for creating "Allow-all-except-this" roles.
  • AssignableScopes: A list of scopes (Subscription or Resource Group IDs) where this role can be assigned.

Example: A Minimal Role Definition

Let’s look at a simple example of a role that allows a user to start and stop a Virtual Machine, but nothing else.

{
  "Name": "VM Start-Stop Operator",
  "IsCustom": true,
  "Description": "Allows users to start and stop VMs but not delete or modify them.",
  "Actions": [
    "Microsoft.Compute/virtualMachines/start/action",
    "Microsoft.Compute/virtualMachines/stop/action",
    "Microsoft.Compute/virtualMachines/read"
  ],
  "NotActions": [],
  "AssignableScopes": [
    "/subscriptions/{your-subscription-id}"
  ]
}

In this example, the user can "read" the VM configuration (essential for the portal to show the VM) and perform the "start" and "stop" actions. They cannot delete the VM, change its size, or modify its network settings because those actions are not listed in the Actions array.

Not read yet

Step-by-Step: Creating a Custom Role

You can create custom roles using the Azure Portal, Azure CLI, or Azure PowerShell. For most administrators, the CLI or PowerShell approach is preferred because it allows you to store your role definitions in version control (like Git).

Method 1: Using Azure CLI

  1. Draft your JSON file: Create a file named vm-operator.json and paste the content provided in the previous section. Ensure you replace {your-subscription-id} with your actual Subscription ID.
  2. Run the creation command: Open your terminal or Cloud Shell and run:
    az role definition create --role-definition @vm-operator.json
    
  3. Verify the role: You can list your custom roles to ensure it was created correctly:
    az role definition list --custom-role-only true --output table
    

Method 2: Using the Azure Portal

  1. Navigate to your Subscription or Resource Group in the Azure Portal.
  2. Select Access control (IAM) from the left-hand menu.
  3. Click + Add and select Add custom role.
  4. Follow the wizard:
    • Basics: Give your role a name and description. You can choose to start from a JSON file or clone an existing role.
    • Permissions: Use the search bar to find specific resource providers (e.g., "Microsoft.Compute"). Select the actions you want to allow.
    • Assignable Scopes: Select the subscription or management group where this role will be available.
    • Review + Create: Verify your settings and click create.

Tip: Start by Cloning If you aren't sure which permissions are required for a task, find a built-in role that is almost correct (e.g., "Virtual Machine Contributor"). When you go to create a custom role, select "Start from a JSON" or "Clone," and then modify the existing definition. This saves you from having to look up every single action string in the Microsoft documentation.

Not read yet

Advanced Concepts: Understanding Actions and Data Actions

When defining permissions, you will encounter two types of operations: Actions and DataActions.

Actions are used for control plane operations. These are things that affect the Azure resource itself—like creating a virtual machine, updating a tag, or deleting a storage account. Most of the permissions you will manage fall into this category.

DataActions are used for data plane operations. These are things that happen inside the resource—like reading a blob in a storage account, sending a message to a Service Bus queue, or querying a SQL database.

It is vital to distinguish between these two because a user might have "Contributor" access to a Storage Account (allowing them to delete the account), but if you do not grant them DataActions, they will be unable to see the files stored inside the containers. Always check if your custom role needs data access before granting broader control plane permissions.

Not read yet

Best Practices for Custom Role Management

Managing custom roles can quickly become messy if you do not have a strategy. Follow these industry-standard practices to keep your environment clean and secure.

1. Use Infrastructure as Code (IaC)

Never create roles manually in the portal for production environments. Always define your roles in JSON files, store them in a Git repository, and use a CI/CD pipeline (like GitHub Actions or Azure DevOps) to deploy them. This provides an audit trail of who changed a role and why.

2. Name Roles Consistently

Use a naming convention that makes the purpose of the role immediately clear. For example, use prefixes like App- for application-specific roles, Ops- for operational roles, and Sec- for security-related roles. A name like App-WebStore-Deployer is much more informative than CustomRole1.

3. Keep Scopes as Small as Possible

While you can define an AssignableScope at the Subscription level, consider if the role is only needed for a specific Resource Group. Restricting the scope limits the "blast radius" if a user account is compromised. If a role is only for one project, assign it only to that project's Resource Group.

4. Review Roles Regularly

Permissions tend to grow over time—a concept known as "permission creep." Schedule a quarterly review of your custom roles. If a role hasn't been used in 90 days, or if the project it was created for has been decommissioned, delete it.

5. Leverage NotActions Carefully

NotActions is powerful, but it can be confusing to troubleshoot. If a user is denied an action because of a NotAction rule, it is often harder to diagnose than simply omitting the action from the Actions list. Use NotActions only when you need to specifically exclude a few actions from a very broad category (e.g., "Grant all Storage permissions except for deleting the account").

Not read yet

Common Pitfalls and How to Avoid Them

Even experienced Azure administrators fall into common traps when implementing custom roles. Here are the ones to watch out for:

  • The "Read" Permission Trap: Many users forget that to use the Azure Portal, a user often needs Microsoft.Resources/subscriptions/read or Microsoft.Resources/subscriptions/resourceGroups/read. If you create a custom role and the user sees "Access Denied" despite having the right actions, they likely lack the basic "read" permissions required to navigate the portal interface.
  • Over-reliance on Wildcards: You can use wildcards in your actions (e.g., Microsoft.Compute/virtualMachines/*). While this is convenient, it is a security risk. If Microsoft adds a new action to the Virtual Machines provider in the future, your role will automatically inherit that action, potentially granting permissions you didn't intend to provide. Always be explicit where possible.
  • Ignoring Inherited Deny Assignments: If you are using Azure Blueprints or managed policies, there might be "Deny Assignments" that override your custom role. If a user still cannot perform an action, check the "Deny assignments" tab in the IAM blade to see if a higher-level policy is blocking them.
  • Hardcoding IDs: When writing your JSON files, avoid hardcoding subscription IDs if you are deploying across multiple environments (Dev, Test, Prod). Use variables in your deployment scripts to inject the correct subscription ID at runtime.

Warning: The Dangers of Wildcards Using * as a wildcard in your role definition is a common shortcut that leads to security vulnerabilities. If you grant Microsoft.Storage/storageAccounts/*, you are granting every current and future action on storage accounts. If Microsoft releases a new feature that allows data export, your users will automatically have that permission. Always list specific actions to maintain control.

Not read yet

Comparison Table: Built-in vs. Custom Roles

Feature Built-in Roles Custom Roles
Creation Provided by Microsoft Created by you
Modification Cannot be changed Fully editable
Maintenance Managed by Microsoft Managed by your team
Granularity Broad, standardized Highly specific
Usage Recommended for general tasks Recommended for specific workflows
IaC Support Reference by ID Fully defined by JSON/Code

Troubleshooting Access Issues

When a user complains they cannot perform a task, the first step is to use the "Access Control (IAM) > Check access" tool in the Azure Portal. This tool allows you to select a user or service principal and see exactly which roles are assigned to them and what permissions they have.

If the "Check access" tool shows they have the correct role but they still cannot perform the action, consider these possibilities:

  1. Propagation Delay: Role assignments usually take effect within a few minutes, but in rare cases, it can take up to 30 minutes for the change to propagate across all Azure regions.
  2. Data Plane vs. Control Plane: As mentioned earlier, the user might have permissions to the resource but lacks the DataActions required to interact with the content inside the resource.
  3. Conditional Access: If your organization uses Azure AD Conditional Access policies (e.g., requiring Multi-Factor Authentication or specific IP ranges), the user might be blocked by these security policies even if their RBAC permissions are correct.
  4. Nested Groups: If the user is assigned the role via an Azure AD group, ensure the user is actually a member of that group and that the group is correctly synced if you are using a hybrid identity setup.

Not read yet

Practical Scenario: Creating a "Database Auditor" Role

Let's walk through a real-world scenario. Your security team needs to audit SQL databases. They need to see the database configuration, read the firewall rules, and view the diagnostic settings, but they must not be able to change any settings or access the data inside the database.

Step 1: Define the Actions

  • Microsoft.Sql/servers/read (See the server)
  • Microsoft.Sql/servers/databases/read (See the database)
  • Microsoft.Sql/servers/firewallRules/read (See firewall settings)
  • Microsoft.Insights/diagnosticSettings/read (See logging configuration)

Step 2: Create the JSON

{
  "Name": "SQL Database Auditor",
  "IsCustom": true,
  "Description": "Allows auditing of SQL Server configuration and firewall rules.",
  "Actions": [
    "Microsoft.Sql/servers/read",
    "Microsoft.Sql/servers/databases/read",
    "Microsoft.Sql/servers/firewallRules/read",
    "Microsoft.Insights/diagnosticSettings/read"
  ],
  "NotActions": [],
  "AssignableScopes": [
    "/subscriptions/{subscription-id}"
  ]
}

Step 3: Deployment You would save this as sql-auditor.json and deploy it via your pipeline. Now, when you assign this role to the security team, they can perform their audit without any risk of accidentally modifying the production environment.

Not read yet

Managing Role Assignments at Scale

As your organization grows, assigning roles one-by-one becomes impossible. Instead, focus on these strategies:

  • Group-Based Assignment: Never assign a role to an individual user account. Always assign the role to an Azure AD group and then add the user to that group. This makes it much easier to manage access when employees join or leave the team.
  • Management Group Assignments: If you have multiple subscriptions, assign roles at the Management Group level. This ensures that every subscription within that group inherits the custom role automatically, keeping your access model consistent across the entire enterprise.
  • Managed Identities: For applications running in Azure (like an App Service or a Function App), use Managed Identities. This removes the need for storing credentials (like service principal secrets) and allows you to assign your custom role directly to the application's identity.

Not read yet

Frequently Asked Questions (FAQ)

Q: Can I create a custom role that spans across multiple subscriptions? A: Yes. You can set the AssignableScopes to a Management Group. Any subscription that is a child of that Management Group will be able to use that custom role.

Q: How many custom roles can I have in a single directory? A: You can have up to 5,000 custom roles per Azure AD tenant. This is a very generous limit, but it is still good practice to clean up unused roles to keep your environment manageable.

Q: Can I use PowerShell to create a role? A: Yes. You can use the New-AzRoleDefinition cmdlet. The process is very similar to the CLI: you define the role object in PowerShell or import it from a JSON file and pass it to the cmdlet.

Q: What happens if I delete a custom role that is currently assigned to a user? A: If you delete a custom role definition, the assignments associated with that role will still exist, but they will become "orphaned" or invalid. You should always remove all assignments of a role before deleting the role definition itself.

Q: Is there any cost associated with using custom roles? A: No, Azure RBAC and custom roles are included in the base cost of your Azure subscription. There is no additional fee for creating or using custom roles.

Not read yet

Conclusion: Key Takeaways for Access Management

Implementing custom roles is a significant step toward a mature, secure cloud architecture. By moving away from broad, built-in roles and toward specific, task-oriented definitions, you reduce the risk of accidental configuration changes and improve your overall security posture.

Here are the key takeaways from this lesson:

  1. Prioritize Least Privilege: Always grant the minimum permissions necessary. Use custom roles to strip away unnecessary actions found in built-in roles.
  2. Use Infrastructure as Code: Store your role definitions as JSON files in version control. This ensures consistency and creates an audit trail for every change.
  3. Understand the Hierarchy: Be mindful of where you assign roles. Use Management Groups and Resource Groups to control the scope of access and prevent accidental over-permissioning.
  4. Distinguish Between Actions and DataActions: Ensure you understand whether your users need to manage the resource itself (control plane) or interact with the content inside the resource (data plane).
  5. Avoid Wildcards: Be explicit in your Actions list. Using * shortcuts creates security gaps and makes it difficult to predict what permissions your users have.
  6. Assign to Groups, Not Users: Manage access by adding users to Azure AD groups, and assign your custom roles to those groups. This simplifies administration and makes auditing much easier.
  7. Review and Clean Up: Periodically audit your roles and assignments. If a role is no longer needed, remove it to keep your environment clean and secure.

By following these principles, you will be able to manage Azure access with confidence, ensuring that your teams have the tools they need while your infrastructure remains protected from unintended changes. Access management is not a "set it and forget it" task; it is an ongoing process of refinement that evolves alongside your business.

Not read yet

Each section gets a ✓ as you scroll through it. Tap the button to jump to the next one.