Policy Variables and Dynamic Policies
What you'll learn: How to write one reusable policy that automatically adapts to different users and contexts instead of creating hundreds of nearly identical policies.
The Problem: Policy Explosion
Imagine you have 100 users. If you hard-code each user's name into their policy (e.g., "Alice can access only Alice's folder"), you'd need 100 separate policies. That's a maintenance nightmare.
What Are Policy Variables?
Policy variables are placeholders in your IAM policies that get replaced with actual values at request time. They reference:
- User attributes (like username or account ID)
- Request context (like the time of day, source IP, or requested resource)
Think of them like mail-merge fields in a form letter. Instead of writing a unique letter for every recipient, you write one template with ${FirstName} placeholders that automatically fill in when sent.
How They Work
When someone makes a request, the IAM system looks up condition keys (like aws:username or ${aws:userid}) and dynamically substitutes them into the policy at evaluation time.
Example pattern (conceptual):
- Policy says: "Allow access to folder
/${aws:username}/*" - When Alice requests access, it becomes: "Allow access to folder
/alice/*" - When Bob requests access, it becomes: "Allow access to folder
/bob/*"
One policy scales to thousands of users automatically.
Common Use Cases
- Personal resource isolation: Each user accesses only their own storage bucket prefix
- Time-based access: Different permissions during business hours vs after-hours
- Location-aware policies: Restrict actions based on source IP or region
Key Takeaway: Policy variables transform hard-coded values into dynamic placeholders that reference user identity or request context, letting you write one flexible policy instead of thousands of static ones.