All guides

Azure managed identities: system-assigned or user-assigned

With a managed identity, an application on Azure gets Microsoft Entra tokens and manages no credentials. The two types differ in life cycle and in whether several resources can share one identity.

A person holding a phone above a laptop keyboard

What a managed identity is

A managed identity is an identity you assign to an Azure compute resource, such as a virtual machine, a virtual machine scale set, an Azure Kubernetes cluster or an App Service app. When you enable one, Azure creates a service principal of a special type for it in Microsoft Entra ID. Code on the resource retrieves a token for that identity, and the request needs no secret. The credentials are not accessible to you, and managed identities cost nothing extra.

System-assigned identities

You enable a system-assigned identity directly on a resource. It shares the life cycle of that resource: when the resource is deleted, Azure deletes the identity. Only that resource can use it, and its name is the name of the resource.

Shell

az vm identity assign --resource-group my-rg --name my-vm

Microsoft lists these cases for this type:

  • the workload is contained within a single Azure resource
  • you want the identity to be deleted when the resource is deleted
  • your logs must show the specific resource behind each action

User-assigned identities

A user-assigned identity is a standalone Azure resource with an independent life cycle. You can assign it to one or more resources, and you must delete it explicitly.

Shell

az identity create --resource-group my-rg --name my-app-identity

az vm identity assign --resource-group my-rg --name my-vm \
  --identities my-app-identity

The output of the first command includes a clientId and a principalId. Microsoft notes that an identity in a different resource group than the virtual machine is passed by its URL in place of its name.

Microsoft recommends this type when:

  • resources that carry out the same task need the same permissions, such as duplicated web servers: fewer role assignments are required
  • access is required before a resource is deployed, because the identity and its role assignments can be configured in advance
  • resources are created rapidly: many system-assigned identities created within a short time can pass the rate limit on Microsoft Entra object creation, and the request then fails with HTTP 429

One resource can carry a system-assigned identity and, alongside it, one or more user-assigned identities.

Assign a role

A new identity has to be authorized before it can access a target service. For Azure resources that is a role assignment. The role and the scope below are yours to choose; Storage Blob Data Reader lets the identity read and list containers and blobs.

Shell

az role assignment create \
  --assignee-object-id <principal-id> \
  --assignee-principal-type ServicePrincipal \
  --role "Storage Blob Data Reader" \
  --scope <storage-account-resource-id>

Microsoft's page says a role assignment made immediately after an identity is created can fail in some cases. Its fix is the form above: pass the object ID and set the principal type to ServicePrincipal.

Two access rules come from the same documentation:

  • The security boundary of the identity is the resource it is attached to. A user who can install or run code on that resource has access to every identity assigned to it.
  • Assigning a user-assigned identity to a resource needs the assign action on that identity, which the Managed Identity Operator role includes. A user who can assign the identity to another resource gets all of its permissions there.

How code gets a token

This is Microsoft's example for the Python library:

Python

from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient

# Acquire a credential object
credential = DefaultAzureCredential()

blob_service_client = BlobServiceClient(
    account_url="https://<my_account_name>.blob.core.windows.net",
    credential=credential
)

DefaultAzureCredential is a preconfigured chain. In the Python library it tries the environment credential first, then workload identity, then managed identity, then developer tools such as the Azure CLI.

For a user-assigned identity, the documented forms take its client ID: ManagedIdentityCredential(client_id=user_assigned_client_id), or the managed_identity_client_id keyword of DefaultAzureCredential.

When a resource has exactly one user-assigned identity and no system-assigned one, the metadata service defaults to it. If another is assigned later, requests that name no identity start failing. Microsoft recommends specifying the identity in every request.

Once the app is deployed to Azure, Microsoft's guidance is to replace DefaultAzureCredential with a specific credential such as ManagedIdentityCredential. Its reasons: a chain is harder to debug, and environment variables on the host can alter its behavior.

Limits to plan for

  • One directory. Managed identities do not support cross-directory scenarios.
  • Delayed permission changes. The back-end services cache managed identity tokens for around 24 hours. Microsoft states that a change to an identity's group or role membership can take several hours to take effect, and that a token refresh cannot be forced.
  • Moves. Moving a user-assigned identity to a different resource group is not supported. After a subscription moves to another directory, disable and re-enable system-assigned identities, and delete, re-create and reattach user-assigned ones.
  • Federated credentials. A user-assigned identity can have 20 at most.
  • Deletion. A deleted identity is soft-deleted for 30 days and cannot be restored. Tokens issued before the deletion stay valid until their original expiry.

Where to start

List the service principals of your managed identities:

Shell

az ad sp list --all --filter "servicePrincipalType eq 'ManagedIdentity'"

Then check three things:

  1. Stored secrets. With a managed identity there are no access keys or passwords to store. Look for workloads on Azure compute that still store one.
  2. Role scope. Grant the least permissions needed to perform the task.
  3. Leftover assignments. Role assignments are not deleted automatically when a managed identity is deleted. The portal shows them as "Identity not found". Delete them, because they count toward the subscription's role assignment limit.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides