IAM roles concept in AWS - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using IAM roles, it is important to understand how the time to assume a role changes as you add more roles or policies.
We want to know how the number of roles or policies affects the time it takes to get permissions.
Analyze the time complexity of assuming multiple IAM roles in sequence.
# Assume a role
aws sts assume-role --role-arn arn:aws:iam::123456789012:role/ExampleRole --role-session-name Session1
# Use the temporary credentials to assume another role
aws sts assume-role --role-arn arn:aws:iam::123456789012:role/AnotherRole --role-session-name Session2
# Repeat for n roles
This sequence shows assuming one IAM role after another, using temporary credentials from the previous role.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: The
sts assume-roleAPI call to get temporary credentials. - How many times: Once for each role you want to assume in the chain.
Each additional role you assume adds one more API call to the sequence.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 assume-role calls |
| 100 | 100 assume-role calls |
| 1000 | 1000 assume-role calls |
Pattern observation: The number of API calls grows directly with the number of roles assumed.
Time Complexity: O(n)
This means the time to assume roles grows linearly with the number of roles you chain together.
[X] Wrong: "Assuming multiple roles happens all at once, so time does not increase with more roles."
[OK] Correct: Each role must be assumed one after another, so each adds time and API calls.
Understanding how IAM role chaining affects time helps you design secure and efficient permission flows in cloud environments.
"What if we used a single role with multiple policies instead of chaining roles? How would the time complexity change?"
Practice
Solution
Step 1: Understand IAM role purpose
An IAM role allows AWS entities to assume permissions temporarily without needing permanent credentials like passwords.Step 2: Compare options
Only To grant permissions to entities without sharing long-term credentials correctly describes this purpose. Options B, C, and D describe unrelated AWS features.Final Answer:
To grant permissions to entities without sharing long-term credentials -> Option DQuick Check:
IAM roles = temporary permissions without passwords [OK]
- Confusing roles with user accounts
- Thinking roles store passwords
- Mixing roles with AWS services like EC2
Solution
Step 1: Identify trust policy structure
A trust policy allows a trusted entity (like EC2) to assume the role using sts:AssumeRole action.Step 2: Check each option
{ "Statement": [{ "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com" }, "Action": "sts:AssumeRole" }] } correctly allows EC2 service to assume the role. { "Statement": [{ "Effect": "Deny", "Principal": { "AWS": "*" }, "Action": "sts:AssumeRole" }] } denies all, which is invalid for trust. { "Statement": [{ "Effect": "Allow", "Principal": { "User": "admin" }, "Action": "iam:CreateUser" }] } uses wrong action and principal. { "Statement": [{ "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "iam:PassRole" }] } uses wrong action (iam:PassRole) for trust.Final Answer:
{ "Statement": [{ "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com" }, "Action": "sts:AssumeRole" }] } -> Option BQuick Check:
Trust policy must allow sts:AssumeRole to a service [OK]
- Using iam:PassRole instead of sts:AssumeRole
- Denying all principals in trust policy
- Specifying user instead of service in Principal
{ "Statement": [{ "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" }] }Which AWS service can assume this role?
Solution
Step 1: Read the Principal service
The Principal is "lambda.amazonaws.com", which means AWS Lambda service is trusted.Step 2: Match service to options
AWS Lambda functions matches AWS Lambda functions. EC2, S3, and IAM users are different entities and not trusted here.Final Answer:
AWS Lambda functions -> Option AQuick Check:
Principal service = lambda.amazonaws.com means Lambda [OK]
- Confusing service names like ec2.amazonaws.com vs lambda.amazonaws.com
- Thinking S3 buckets can assume roles
- Assuming IAM users are trusted by default
{ "Statement": [{ "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com" }, "Action": "iam:PassRole" }] }Why can't EC2 instances assume this role?
Solution
Step 1: Identify the required action in trust policy
The trust policy must allow the action sts:AssumeRole for the trusted entity to assume the role.Step 2: Analyze the given policy
The policy uses iam:PassRole, which is incorrect for trust. This prevents EC2 from assuming the role.Final Answer:
Because the action should be sts:AssumeRole, not iam:PassRole -> Option CQuick Check:
Trust policy action must be sts:AssumeRole [OK]
- Using iam:PassRole instead of sts:AssumeRole
- Changing Effect to Deny by mistake
- Believing EC2 cannot assume roles
Solution
Step 1: Identify trust policy requirements
The trust policy must allow the Lambda service (lambda.amazonaws.com) to assume the role.Step 2: Identify permissions policy requirements
The role's permissions policy must grant access to S3 buckets for the Lambda function.Step 3: Evaluate options
A trust policy allowing lambda.amazonaws.com to assume the role and an IAM permissions policy granting S3 access correctly pairs the trust policy for Lambda and permissions for S3. Other options have incorrect principals or deny access.Final Answer:
A trust policy allowing lambda.amazonaws.com to assume the role and an IAM permissions policy granting S3 access -> Option AQuick Check:
Trust policy + permissions policy = role works [OK]
- Allowing wrong service in trust policy
- Confusing permissions policy with trust policy
- Denying all principals in trust policy
