Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

What Is an AWS IAM Role and When to Use It

An IAM role is a temporary jacket. A person or a service puts it on, gets short-lived keys, does the job, and takes it off. No permanent access key stuck on a laptop.

In this chapter

  • Role vs user vs group
  • Trust policy vs permissions policy
  • Who uses roles: EC2, Lambda, humans
  • When a role beats an IAM user
  • Cross-account role idea

1. Everyday example first

At Infosys Pune, a visitor does not get a permanent office badge. Security gives a time-bound visitor pass for one floor. AWS roles work the same way — temporary permission, not a lifelong key.

The names and floors in this story are made up for learning.

An IAM role is an identity that has no long-lived password or access key of its own. Someone (or something) assumes the role and receives temporary credentials from STS.

IdentityLives how long?Typical use
IAM userUntil you delete itNamed human (older style)
GroupUntil you delete itBundle of policies for many users
RoleCredentials expire (minutes–hours)EC2, Lambda, SSO, cross-account

2. Two policies on every role

Read an IAM role as two separate decisions. The trust policy controls which principal can enter the role. A permissions policy controls what a role session can do after it enters. Suppose only the EC2 service can assume a role and the role allows one report read in S3. The task code on the authorized EC2 instance can use its temporary role credentials for that read. Granting report access on the role does not make an unrelated Lambda function able to assume it.

A session is a time-limited use of the role with temporary credentials. Several sessions can use the same role. Changing or ending one session is different from deleting the role. To trace a permission test, start by checking the caller identity from inside the workload that made the request. Then compare its action and resource with the allowed permissions.

Beginners mix these up in interviews. One answers “who may wear the jacket”. The other answers “what the jacket can do”.

  • Trust policy (resource-based on the role): who can call sts:AssumeRole — e.g. EC2, Lambda, another account, SAML/OIDC.
  • Permissions policy (identity): Allow/Deny actions on resources — e.g. s3:GetObject on one bucket.
  • Without trust, nobody can assume. Without permissions, the assumed session can do almost nothing useful.
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": { "Service": "ec2.amazonaws.com" },
    "Action": "sts:AssumeRole"
  }]
}

That trust snippet says: only the EC2 service may assume this role. Attach a separate permissions policy for S3/DynamoDB/etc.

3. When to use a role

  1. EC2 / ECS / Lambda — never put access keys in code or on disk. Attach a role.
  2. Humans via Identity Center (SSO) — user picks a permission set; AWS issues temporary role credentials.
  3. Cross-account — Account A trusts Account B’s admin (or a role) to assume a role in A.
  4. Break-glass admin — rare high-power role with MFA and logging, not daily AdministratorAccess on a user.

Ravindra Bagale's Tip

Students often create an IAM user with AdministratorAccess and paste keys into .env. Prefer a role (or Identity Center). Keys leak; role sessions expire.

Common errors: cause and safe next check

An assumed role cannot do everything the original user could do

For requests made with role-session credentials, the role’s permissions apply instead of simply adding the original user’s rights. Identify the session and inspect its required actions. Do not assume that an administrator who switches roles remains an administrator inside a narrow role.

A role is created but EC2 still cannot select it

An EC2 role must be carried by an instance profile. Creating a role and creating that container are separate API/CLI actions, although the IAM console normally handles both for an EC2 use case. Check the intended profile association rather than creating a permanent access key.

The role policy was changed but an Allow still fails

The request may also be limited by an applicable boundary or organization policy, or match an explicit Deny. Read the error and applicable policy conditions before asking for a change. Temporary credentials do not bypass those controls.

Practical questions and full answers

Does the role disappear when a session expires?

No. The role is the reusable IAM identity; the credentials issued for one session have a limited lifetime. Expiry of one session is not deletion of the role. A later authorized session can receive new temporary credentials.

Does changing roles in a browser switch my existing terminal too?

Do not assume so. A terminal chooses credentials from its own profile, environment and other providers. Verify its active identity separately. A browser role selection is not proof of the identity used by an already-open CLI process.

Is iam:PassRole the same as assuming a role?

No. PassRole lets an authorized caller assign an approved role to an AWS service. Assuming a role creates a role session with temporary credentials. A deployer can need PassRole while the service later assumes the role to do its own work.

4. Interview angles

Q. User vs role?

User has long-lived creds; role is assumed for temporary creds.

Q. Trust vs permissions?

Trust = who may assume; permissions = what the session can do.

Q. Why roles on EC2?

Instance metadata hands rotating creds; no keys on disk.

Q. Can a role have a password?

No. You assume it; you do not “log in as the role” with a password.

Q. Cross-account role?

Trust policy Principal is another account (or role ARN).

Try at home

  1. Open IAM → Roles and read the trust policy of any lab role you already have.
  2. Next: learn STS AssumeRole and session tokens.

In short

  1. A role is temporary credentials via AssumeRole.
  2. Trust policy ≠ permissions policy.
  3. Prefer roles over long-lived keys for compute.
  4. Identity Center uses roles under the hood for humans.

Check which role the code actually received

Compare aws sts get-caller-identity from your local profile and from the lab workload. An assumed-role ARN includes the session name. Keep credential values out of the output you share.

Interview check Why can an EC2 role exist but not be selectable for an instance?

EC2 associates an instance profile containing the role. When using the API or CLI, creating and attaching that profile are separate operations.

Reference documentation

Practise this chapter

Interview questions and answers

How would you explain a role to a learner who only knows IAM users?

A role is an AWS identity with a trust rule and permissions that authorized callers can use for a limited session. The session gets temporary credentials. The person or service asking to use the role and the session later accessing AWS resources are separate requests to check.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. A role permits S3 reads but trusts only EC2. Can an unrelated Lambda function assume it just because it needs the same read?

Answer and explanation

C. No, its service also needs to satisfy the role’s trust

Role permissions and the ability to assume the role are separate decisions.

2. What does expiration mean for one role session?

Answer and explanation

B. Its temporary credentials stop working

A session is time-limited. The role remains available to future authorized assumptions.

3. Your original admin identity assumes a read-only role. Which credentials govern the next call using the new session?

Answer and explanation

B. The read-only session’s applicable access

AWS evaluates the new request under the role session credentials you used.

Trace AWS role trust, assignment and file access

Before you start

  • Use two synthetic callers, EC2 and Lambda, and an imaginary report role that trusts EC2 only. No cloud access is required.
  1. Draw one line from the EC2 service to the role and label it assume. Draw a second line from the new role session to the sample S3 object and label it read.

    Expected result: The first line tests trust. The second tests what the role session may do.

  2. Read this policy outcome: the role allows the sample object read. Decide what happens if Lambda tries to assume the same role.

    Expected result: The S3 permission does not satisfy the Lambda service principal’s missing trust.

  3. Keep EC2 authorized to assume and change the requested resource to an unrelated object. Diagnose which line fails and name what an AWS caller-identity read should show in an actual lab.

    Expected result: The session can be issued but the unrelated object read is denied in the scenario. Caller identity names the active role session.

If the result differs

  • If the trust and permissions were written as a single vague rule, write down which principal is evaluated and which AWS action each line represents.

Finish and clean up

  • No identities or permissions need to be changed.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Aurangabad Pharma is a pharmaceuticals business in Chhatrapati Sambhajinagar. Dr. Farhan Shaikh looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Medium Trust policy for an EC2 role

The trust policy says WHO may take the role.

Write a trust policy that lets ec2.amazonaws.com assume the role.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Trust policy for a Lambda role

The trust policy says WHO may take the role.

Write a trust policy that lets lambda.amazonaws.com assume the role.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Attach a policy to a role

The role needs permissions: attach a policy to say what it may do.

Attach arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess to role abadpharma-ec2-role.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Let a function write logs

Without these actions, Lambda logs never appear in CloudWatch.

Allow logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents on arn:aws:logs:*:*:* only.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Assume a role

AssumeRole gives you short-lived keys for another role, often in another account.

Assume role arn:aws:iam::112055250461:role/Auditor with session name farhan-audit.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Trust policy for an ECS task role

The trust policy says WHO may take the role.

Write a trust policy that lets ecs-tasks.amazonaws.com assume the role.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Let another account assume a role

Cross-account roles are safer than sharing access keys.

Write a trust policy that lets account 111122223333 assume this role.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Allow PassRole for one role

To launch EC2 with a role, the user needs iam:PassRole on that role.

Allow iam:PassRole on arn:aws:iam::112055250461:role/abadpharma-ec2-role only.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Put the role in the instance profile

The profile is empty until you add the role to it.

Add role abadpharma-ec2-role to instance profile abadpharma-ec2-profile.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Create an instance profile

EC2 uses a role through an instance profile, a small wrapper around the role.

Create an instance profile named abadpharma-ec2-profile.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Create a role with a trust policy

A role is an identity that services or people "assume". The trust policy says who may assume it.

Create a role named abadpharma-ec2-role using the trust policy in trust.json.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Give a running EC2 its role

With the profile attached, apps on the server get temporary keys automatically.

Associate instance profile abadpharma-ec2-profile with instance i-01a40525502111a2b.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: What is an IAM role?. At Infosys Pune a visitor gets a time-bound pass for one floor — not a lifelong office badge. Compute needs the same pattern. An IAM role is an identity with no long-lived password or access key. A caller assumes it and STS returns temporary credentials. Create the role, attach a permissions policy, set a trust policy for who may assume, then let EC2/Lambda/CLI assume it. An EC2 instance profile role lets the instance call S3 with rotating keys from the metadata service — no keys on disk.
Revision card: What is an IAM role? · Download PNG · Open card page
Revision card: Trust vs permissions policy. Mixing “who may wear the jacket” with “what the jacket allows” causes AccessDenied you cannot debug. Trust policy (resource-based on the role) lists who may AssumeRole. Permissions policy lists Actions/Resources the session may call. Edit trust Principal (user/role/service ARN or account). Attach customer-managed permissions separately. Trust Principal = your IAM user; permissions allow s3:GetObject on one lab bucket only.
Revision card: Trust vs permissions policy · Download PNG · Open card page
Revision card: When to use a role. Long-lived access keys on EC2 or in GitHub are how student accounts get hijacked. Prefer roles for EC2, Lambda, cross-account access, and humans via Identity Center. Use IAM users sparingly for break-glass or old tools. Attach an instance profile / execution role / permission set instead of creating an access key. Lambda reads DynamoDB with its execution role; you never paste a secret key into the function code.
Revision card: When to use a role · Download PNG · Open card page
Comparison: IAM role vs IAM user. IAM role: No long-lived password/key; assumed for temporary STS credentials. IAM user: Named identity with password and/or long-lived access keys until deleted.
Comparison: IAM role vs IAM user · Download PNG · Open card page
Interview card: User vs role — what is the difference?. An IAM user is a named long-lived identity (password and/or access keys until you delete them). An IAM role has no long-lived password/key; you assume it for temporary STS credentials. Long-lived keys on disks and in Git history are a common breach path. Roles rotate automatically via STS and shrink blast radius when leaked. EC2 and Lambda use roles (instance profile / execution role). Humans increasingly use Identity Center, which also assumes roles under the hood.
Interview card: User vs role — what is the difference? · Download PNG · Open card page
Interview card: Trust policy vs permissions policy?. Trust policy answers who may call sts:AssumeRole on this role. Permissions policy answers what API actions the resulting session may perform on which resources. A perfect S3 permissions policy still fails if trust Principal does not include the caller. Wrong trust is a top lab AccessDenied cause. Example: trust Principal = arn:aws:iam::111:user/ravi; permissions Resource = arn:aws:s3:::lab-bucket/* with s3:GetObject.
Interview card: Trust policy vs permissions policy? · Download PNG · Open card page

More cards: Ravi Sir's Cards.