Lab 04: Solution - Portal Walkthrough
Only refer to this after attempting the tasks yourself!
Pre-Lab Setup
Create Resource Group
- Go to Resource groups → + Create
- Name:
rg-identity-lab - Region: East US
- Create
Create Key Vault
Go to Key vaults → + Create
Configure:
- Resource group:
rg-identity-lab - Key vault name:
kv-identity-lab-+ random numbers - Region: East US
- Pricing tier: Standard
- Resource group:
Next: Access configuration
Permission model: Azure role-based access control ← Important!
Review + create → Create
Go to the Key Vault → Secrets → + Generate/Import
Name:
DatabasePassword, Value:SuperSecret123!Create
Create Storage Account
Go to Storage accounts → + Create
Configure:
- Resource group:
rg-identity-lab - Storage account name:
stidentitylab+ random numbers - Region: East US
- Redundancy: LRS
- Resource group:
Create
Go to storage account → Containers → + Container
Name:
app-dataCreate
Create Virtual Machine
- Go to Virtual machines → + Create → Azure virtual machine
- Configure Basics:
- Resource group:
rg-identity-lab - Name:
vm-app-server - Region: East US
- Image: Windows Server 2022 Datacenter (or Ubuntu 22.04)
- Size: Standard_B2s
- Username/Password: Set for management
- Resource group:
- Networking:
- Public inbound ports: None (or RDP/SSH if you want to connect)
- Review + create → Create
Task 1: Enable System-Assigned Managed Identity
Go to Virtual machines → Click
vm-app-serverIn left menu, scroll down to Settings → Click Identity
You'll see two tabs: System assigned and User assigned
On System assigned tab:
- Status: Click On
- Click Save
- Click Yes to confirm
Wait a few seconds - an Object (principal) ID appears
- This is a GUID like:
a1b2c3d4-e5f6-7890-abcd-ef1234567890 - Copy this ID (you'll need it for Task 2)
- This is a GUID like:
Task 2: Grant VM Identity Access to Key Vault
Go to Key vaults → Click your Key Vault
Click Access control (IAM) in left menu
Click + Add → Add role assignment
Role tab:
- Search for
Key Vault Secrets User - Select it → Next
- Search for
Members tab:
- Assign access to: Managed identity
- Click + Select members
- In the right panel:
- Subscription: Your subscription
- Managed identity: Virtual machine
- Select:
vm-app-server
- Click Select
- Next
Review + assign → Review + assign
Verify Assignment
- Stay in IAM → Role assignments tab
- You should see:
- Principal:
vm-app-server - Role: Key Vault Secrets User
- Scope: This resource
- Principal:
Task 3: Understanding the Code Flow
When an application on the VM needs to access Key Vault:
1. Application calls IMDS endpoint
URL: http://169.254.169.254/metadata/identity/oauth2/token
2. IMDS returns an access token
(Azure handles authentication behind the scenes)
3. Application uses token to call Key Vault
Authorization: Bearer <token>
4. Key Vault checks the token and RBAC
- Token is valid ✓
- Identity has Key Vault Secrets User role ✓
- Access granted ✓No credentials are stored anywhere on the VM!
Task 4: Create User-Assigned Managed Identity
Search for Managed Identities in portal search bar
Click + Create
Configure:
- Subscription: Your subscription
- Resource group:
rg-identity-lab - Region: East US
- Name:
id-app-backend
Review + create → Create
Go to the created identity:
- Note the Object (principal) ID
- Note the Client ID (also called Application ID)
- Note the Resource ID (full Azure resource path)
Task 5: Assign User-Assigned Identity to VM
Go to Virtual machines →
vm-app-serverClick Identity in left menu
Click User assigned tab
Click + Add
In the right panel:
- Find and select
id-app-backend - Click Add
- Find and select
Wait for deployment to complete
Verify
- System assigned tab shows: Status On
- User assigned tab shows:
id-app-backend
The VM now has TWO identities it can use!
Task 6: Grant User-Assigned Identity Storage Access
Go to Storage accounts → Click your storage account
Click Access control (IAM)
Click + Add → Add role assignment
Role: Search for
Storage Blob Data Contributor→ Select → NextMembers:
- Assign access to: Managed identity
- Click + Select members
- Managed identity dropdown: User-assigned managed identity
- Select:
id-app-backend - Click Select
- Next
Review + assign → Review + assign
Task 7: Identity Type Comparison
| Feature | System-Assigned | User-Assigned |
|---|---|---|
| Created as | Part of resource | Standalone resource |
| Lifecycle | Deleted when resource is deleted | Independent - must delete separately |
| Shared across resources | No - one identity per resource | Yes - can assign to multiple resources |
| One resource, one identity | Yes - 1:1 relationship | No - many:many relationship |
| Use case | Simple scenarios, single resource | Complex scenarios, shared identity needed |
When to use System-Assigned:
- Simple deployments
- Each resource needs unique identity
- Want automatic cleanup
When to use User-Assigned:
- Multiple resources need same permissions
- Identity must survive resource deletion
- Blue-green deployments (identity persists across versions)
Task 9: Troubleshooting Guide
Step-by-Step Troubleshooting
1. Verify Identity is Enabled:
VM → Identity → System assigned → Status: On?
If Off, enable it and try again2. Get the Object ID:
VM → Identity → Object (principal) ID
Copy this value3. Check Role Assignment Exists:
Key Vault → Access control (IAM) → Role assignments
Look for the VM's Object ID in the list4. Verify Correct Role:
Common mistake: Key Vault Reader vs Key Vault Secrets User
- Key Vault Reader: View KV properties (NOT secrets)
- Key Vault Secrets User: Read secret VALUES5. Check Permission Model:
Key Vault → Access configuration
If "Vault access policy" is selected, RBAC won't work!
Change to "Azure role-based access control"6. Check Networking:
Key Vault → Networking
If firewall is enabled, ensure VM's VNet/subnet is allowedCommon Errors and Solutions
| Error | Cause | Solution |
|---|---|---|
| "Managed identity not found" | Identity not enabled | Enable system/user-assigned identity |
| "Forbidden" from Key Vault | Missing role or wrong role | Add Key Vault Secrets User role |
| "Access denied" | Access policy mode | Switch to RBAC mode |
| "Network error" | Firewall blocking | Add VM network to allowed networks |
| "Token expired" | Stale token | Get fresh token from IMDS |