Explain AWS IAM roles, policies, and why roles are safer than access keys
Quick Summary: An IAM Policy is a JSON document declaring allowed or denied actions on AWS resources, whereas an IAM Role is an assumable identity issuing short-lived temporary STS credentials automatically rotated without static access keys.
Direct Interview Answer
IAM Policies evaluate permissions using explicit deny → explicit allow → default implicit deny logic. An explicit Deny always overrides any Allow.
IAM Roles define a Trust Policy (who can assume the role—EC2, Lambda, ECS, or cross-account principals) and Permission Policies (what the role can call), eliminating hardcoded long-lived AWS access keys.
Detailed Technical Explanation
On EC2 or EKS, Instance Profiles / IRSA automatically fetch rotating short-lived tokens from AWS STS, meaning even if application logs leak temporarily, stale credentials expire cleanly.
Practical Walkthrough
Least-privilege S3 bucket write policy scoped strictly to one invoice prefix.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:PutObject", "s3:GetObject"],
"Resource": "arn:aws:s3:::mockattempt-invoices-prod/2026/*"
}
]
}
Only PutObject and GetObject permitted strictly inside the 2026/* prefix.
Real-World Production Use Case
Granting ECS Fargate microservices least-privilege access to S3 uploads and Secrets Manager without placing AWS_ACCESS_KEY_ID inside .env files.
Common Interview Mistakes to Avoid
- Granting wildcard "*" on both Action and Resource inside production roles.
- Confusing IAM Role Permissions Policy (what it can do) with Trust Policy (who can assume it).