Overview and What You Will Learn
In this lab, you will create both a System-Assigned and a User-Assigned Managed Identity, attach each to a different VM, and see how each identity type's lifecycle differs - one tied permanently to a single resource, the other reusable across several resources independently.
Why This Matters in Production
A developer at a fintech startup creates a traditional service principal with a client secret to let an application authenticate to Azure services, then has to store that secret somewhere, remember to rotate it before it expires, and worry about it leaking from a config file or log output. Managed Identity removes this entire category of concern - there is no secret to store, rotate, or leak, because none exists.
Core Principles
Azure offers two types of Managed Identity, and choosing between them depends on whether the identity's lifecycle should be tied to one specific resource or shared across several.
+------------------------------------------+| System-Assigned Managed Identity || Created and deleted automatically with || the resource it's attached to || One identity, tied to exactly one resource |+------------------------------------------+| User-Assigned Managed Identity || Created as its own independent resource || Can be attached to multiple VMs or services || simultaneously, and outlives any single one |+------------------------------------------+Both types eliminate the need to store any credential at all - the difference is purely about lifecycle and reusability, not about the underlying security model, which is identical for both.
Detailed Step-by-Step Practical Lab
- Create a Resource Group and a VM with a System-Assigned Managed Identity:
az group create --name rg-identity-lab-mumbai --location centralindia az vm create \ --resource-group rg-identity-lab-mumbai \ --name vm-system-identity \ --image Ubuntu2204 \ --size Standard_B1s \ --admin-username azureadmin \ --generate-ssh-keys \ --assign-identity- Confirm the System-Assigned identity was created and tied to this specific VM:
az vm show \ --resource-group rg-identity-lab-mumbai \ --name vm-system-identity \ --query "identity.principalId" --output tsv- Create a separate User-Assigned Managed Identity as its own independent resource:
az identity create \ --resource-group rg-identity-lab-mumbai \ --name uai-shared-identity- Attach that same User-Assigned identity to two different VMs, demonstrating its reusability:
az vm create \ --resource-group rg-identity-lab-mumbai \ --name vm-shared-a \ --image Ubuntu2204 \ --size Standard_B1s \ --admin-username azureadmin \ --generate-ssh-keys \ --assign-identity uai-shared-identity az vm create \ --resource-group rg-identity-lab-mumbai \ --name vm-shared-b \ --image Ubuntu2204 \ --size Standard_B1s \ --admin-username azureadmin \ --generate-ssh-keys \ --assign-identity uai-shared-identity- Confirm both VMs now share the same identity, unlike the System-Assigned example where each VM would have gotten its own unique identity:
az vm show --resource-group rg-identity-lab-mumbai --name vm-shared-a \ --query "identity.userAssignedIdentities" --output json az vm show --resource-group rg-identity-lab-mumbai --name vm-shared-b \ --query "identity.userAssignedIdentities" --output jsonNoteBoth commands should show a reference to the same
uai-shared-identityresource ID - confirming this identity is genuinely shared, rather than each VM getting its own separate identity the way System-Assigned would have created.
If
vm-system-identityis deleted, its System-Assigned identity is automatically deleted along with it. If either shared VM is deleted,uai-shared-identitycontinues to exist independently, still usable by the remaining VM or future resources.Clean up:
az group delete --name rg-identity-lab-mumbai --yes --no-waitaz identity delete --resource-group rg-identity-lab-mumbai --name uai-shared-identityProduction Best Practices & Common Pitfalls
Common MistakeCreating a traditional service principal with a client secret for a workload running entirely on Azure compute (VMs, App Service, AKS), when Managed Identity would provide the same access with no credential to store, rotate, or leak at all.
TipUse User-Assigned Managed Identity when several resources need to share the exact same set of permissions - like an entire fleet of VMs all needing read access to the same Key Vault. Use System-Assigned when the identity's purpose is genuinely tied to one specific resource's lifecycle.
- Managed Identity only works for Azure resources calling other Azure services - it does not help an application running outside Azure (like on a developer's laptop) authenticate to Azure services, which still needs a different authentication method for local development.
- Granting a Managed Identity access still goes through normal RBAC. Creating the identity itself grants no permissions automatically - you still need to explicitly assign it a role scoped to whatever resource it needs to access.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
az vm create --assign-identity |
Create a VM with a System-Assigned Managed Identity |
az identity create |
Create a standalone User-Assigned Managed Identity |
az vm create --assign-identity <name> |
Attach an existing User-Assigned identity to a VM |
az role assignment create --assignee <principal-id> |
Grant a Managed Identity access to a resource |