Granting Azure RBAC roles to managed identity for a resource
TypeScriptTo grant Azure RBAC (Role-Based Access Control) roles to a managed identity for a specific Azure resource using Pulumi, you will be creating an instance of a managed identity and then assigning a role to it for the desired scope, which could be an Azure resource or subscription.
In this example, I will create a managed identity and an Azure resource group, and then I will demonstrate how to assign the 'Reader' role to the managed identity at the resource group scope.
Firstly, let’s understand the key resources we will be using:
azure-native.managedidentity.UserAssignedIdentity
: This resource is used to create a managed identity in Azure.azure-native.authorization.RoleAssignment
: This resource is used to assign a role to a principal (like a managed identity) for a particular scope.
Here’s how you can accomplish this:
import * as pulumi from "@pulumi/pulumi"; import * as azure from "@pulumi/azure-native"; // Create an Azure resource group const resourceGroup = new azure.resources.ResourceGroup("myResourceGroup"); // Create a managed identity const managedIdentity = new azure.managedidentity.UserAssignedIdentity("myManagedIdentity", { resourceGroupName: resourceGroup.name, }); // The ID of the built-in 'Reader' role const readerRoleId = 'acdd72a7-3385-48ef-bd42-f606fba81ae7'; // Assign the 'Reader' role to the managed identity for the resource group scope const roleAssignment = new azure.authorization.RoleAssignment("roleAssignment", { principalId: managedIdentity.principalId, // The scope at which the role assignment is applied scope: resourceGroup.id, // The role definition ID for the 'Reader' role roleDefinitionId: `/subscriptions/${pulumi.config.require("azure:subscriptionId")}/providers/Microsoft.Authorization/roleDefinitions/${readerRoleId}`, }); // Export the resource group name and managed identity ID export const resourceGroupName = resourceGroup.name; export const managedIdentityId = managedIdentity.id;
Remember to replace
"azure:subscriptionId"
inpulumi.config.require
with your actual Azure subscription ID.This program does the following:
- It imports the necessary modules from Pulumi’s Azure provider.
- It creates a new resource group to which we will assign the RBAC role.
- It creates a new managed identity.
- It creates a role assignment, using the built-in 'Reader' role, for the managed identity so that it can read all resources in the scope of the created resource group.
- It exports the resource group name and managed identity ID for use in other places or for reference.
After deploying this program with Pulumi, the managed identity will have read access to all resources in the specified resource group. You can change the role and scope as needed to fit your specific requirements.
Make sure you have the
@pulumi/azure-native
package installed in your project and your Azure credentials configured for Pulumi.# Install the necessary Pulumi Azure package npm install @pulumi/azure-native
To run this program:
- Save the code into a file named
index.ts
. - Ensure you are logged in to Pulumi via
pulumi login
. - Run
pulumi up
to deploy the stack.
You will be prompted to review the changes before they are applied to your Azure subscription. After approval, Pulumi will provision the resources and the role assignment as specified.
Rewrite this program in C#
C#Below is the C# version of the provided Pulumi TypeScript program which creates an Azure Resource Group, a managed identity, and assigns the 'Reader' role to the identity:
using Pulumi; using Pulumi.AzureNative.Authorization; using Pulumi.AzureNative.Resources; using Pulumi.AzureNative.ManagedIdentity; using System.Threading.Tasks; class Program { static Task<int> Main() => Deployment.RunAsync<MyStack>(); } class MyStack : Stack { public MyStack() { // Create an Azure resource group var resourceGroup = new ResourceGroup("myResourceGroup"); // Create a managed identity var managedIdentity = new UserAssignedIdentity("myManagedIdentity", new UserAssignedIdentityArgs { ResourceGroupName = resourceGroup.Name, }); // The ID of the built-in 'Reader' role var readerRoleId = "acdd72a7-3385-48ef-bd42-f606fba81ae7"; // Assign the 'Reader' role to the managed identity for the resource group scope var roleAssignment = new RoleAssignment("roleAssignment", new RoleAssignmentArgs { PrincipalId = managedIdentity.PrincipalId, Scope = resourceGroup.Id, RoleDefinitionId = $"/subscriptions/{Deployment.Instance.SubscriptionId}/providers/Microsoft.Authorization/roleDefinitions/{readerRoleId}", }); // Export the resource group name and managed identity ID this.ResourceGroupName = resourceGroup.Name; this.ManagedIdentityId = managedIdentity.Id; } [Output] public Output<string> ResourceGroupName { get; set; } [Output] public Output<string> ManagedIdentityId { get; set; } }