IAM Roles and Policies — Least Privilege Principle
Fast recall
Quick revision
Core answer
Security awareness and understanding of AWS authentication/authorisation model
Using IAM user access keys in application code or environment variables instead of IAM roles, and using wildcard (*) actions in policies
Explain the difference between the task execution role (pulls image, writes logs) and the task role (what the app itself can do), and mention IAM policy conditions to restrict by resource ARN or tag
Interview-ready guide
Interview answer
IAM controls every API call in AWS — it handles both authentication (proving who you are) and authorization (determining what you're allowed to do). Understanding least-privilege IAM is essential because overly permissive roles are the most common AWS security finding.
IAM roles vs IAM users
- An IAM user has permanent credentials (access key + secret); an IAM role is assumed by a resource and provides temporary credentials via STS
- For ECS tasks, Lambda functions, and EC2 instances: always use IAM roles, never IAM users; the platform automatically rotates the temporary credentials, which means you don't store or manage long-lived secrets in the environment
Least privilege — the principle
- Least privilege means granting only the exact permissions a service needs and nothing more — which means an ECS task that writes to one S3 bucket should have
s3:PutObjecton that specific bucket ARN, nots3:*on* - Over-permissive roles are the single most exploited attack vector in AWS breaches; a compromised task with
s3:*exposes all your buckets; a compromised task withs3:PutObjecton one bucket exposes only that
IAM policy structure
- Policies have Effect (Allow/Deny), Action (the API operations), Resource (the specific ARN), and optional Condition —
aws:SourceIp,aws:RequestedRegion, ands3:prefixare common conditions that narrow access further - Start with AWS managed policies for common roles (e.g.,
AmazonECSTaskExecutionRolePolicy) and add custom policies for your specific resource ARNs
In Spring Boot on ECS
- Assign the IAM task role in the task definition; the AWS SDK automatically calls the ECS metadata endpoint to retrieve temporary credentials — no AWS key configuration in environment variables or application.properties
- Use
@Value("${aws.region}")from the ECS metadata, not hardcoded; the SDK's DefaultCredentialsProvider chain resolves credentials automatically
IAM Roles and Policies — Least Privilege Principle
Direct Answer
IAM (Identity and Access Management) controls who can do what on which AWS resources. Least privilege means granting only the permissions required to perform a specific task — nothing more. Spring Boot apps should use IAM roles attached to their compute (EC2 instance profile, ECS task role) rather than hardcoded access keys.
Common Mistakes & Pitfalls
Using IAM user access keys in application code or environment variables instead of IAM roles, and using wildcard (*) actions in policies
Check your understanding
Answer these without looking back at the article.
- How do you audit which IAM permissions your application is actually using?
- What is the difference between an IAM role's trust policy and its permission policy?
- How would you use IAM conditions to restrict S3 access to requests from your VPC only?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.