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

Ravi Sir's Cards › AWS › IAM roles and policies

AWS Cloud

AWS IAM roles and policies: trust, temporary credentials and explicit Deny

This page keeps the related IAM cards on one topic page, so you can revise roles, trust, and policies in one sitting. It has the six cards from “What is an AWS IAM role and when to use it”, the eight cards from “AWS IAM roles: 10 practical examples”, and the six cards from “AWS IAM policies: JSON, managed, inline and conditions”.

Each card still has its image and its original text. Under each group you will also find a short Why, What, How example, and an exam tip, in easy English. The names, account IDs, bucket names and amounts in the stories are made up for learning.

What is an IAM role, and when to use it

Why this matters

A named IAM user with a long-lived access key is like a lifelong office badge. If that key lands in Git, on a laptop, or inside an AMI, anyone who finds it can keep using it until you delete it. Compute in AWS needs a different pattern: a time-bound pass for one job.

What it is

An IAM role is an identity with no long-lived password or access key. A caller assumes the role, and AWS STS returns temporary credentials. Two policies sit on the role and do different jobs. The trust policy (resource-based on the role) lists who may call sts:AssumeRole. The permissions policy lists the Actions and Resources that session may use. Prefer roles for EC2, Lambda, cross-account access, and humans who sign in through IAM Identity Center. Keep IAM users only for rare break-glass or old tools.

How it works — a real example

Suppose we are in a Pune lab and an EC2 instance must read one bucket, lab-bucket. We create a role, set trust Principal to the EC2 service, attach a permissions policy that allows only s3:GetObject on that bucket's objects, and attach the role with an instance profile. On the instance, aws sts get-caller-identity shows an assumed-role session, and the keys come from the metadata service — nothing is stored in ~/.aws/credentials. The names and account details are made up for learning.

Common mistake / exam tip

A perfect S3 permissions policy still fails if the trust Principal does not include the caller. When a lab returns AccessDenied, read the trust policy first, then the permissions policy. Do not mix “who may wear the jacket” with “what the jacket allows”.

What is an IAM role?

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.
Card 1 of 20: What is an IAM role? · Download PNG
Read card text

What this card says

Why do we need it?

At Infosys Pune a visitor gets a time-bound pass for one floor — not a lifelong office badge. Compute needs the same pattern.

What is it?

An IAM role is an identity with no long-lived password or access key. A caller assumes it and STS returns temporary credentials. AssumeRole

How to do it

Create the role, attach a permissions policy, set a trust policy for who may assume, then let EC2/Lambda/CLI assume it.

Example

An EC2 instance profile role lets the instance call S3 with rotating keys from the metadata service — no keys on disk.

Trust vs permissions policy

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.
Card 2 of 20: Trust vs permissions policy · Download PNG
Read card text

What this card says

Why do we need it?

Mixing “who may wear the jacket” with “what the jacket allows” causes AccessDenied you cannot debug.

What is it?

Trust policy (resource-based on the role) lists who may AssumeRole. Permissions policy lists Actions/Resources the session may call. sts:AssumeRole

How to do it

Edit trust Principal (user/role/service ARN or account). Attach customer-managed permissions separately.

Example

Trust Principal = your IAM user; permissions allow s3:GetObject on one lab bucket only.

When to use a role

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.
Card 3 of 20: When to use a role · Download PNG
Read card text

What this card says

Why do we need it?

Long-lived access keys on EC2 or in GitHub are how student accounts get hijacked.

What is it?

Prefer roles for EC2, Lambda, cross-account access, and humans via Identity Center. Use IAM users sparingly for break-glass or old tools.

How to do it

Attach an instance profile / execution role / permission set instead of creating an access key. No long-lived keys

Example

Lambda reads DynamoDB with its execution role; you never paste a secret key into the function code.

Comparison: Identity style

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.
Card 4 of 20: Comparison: Identity style · Download PNG
Read card text

What this card says

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.

Key points

EC2, Lambda, cross-account, Identity Center sessions. Break-glass, legacy tools, rare human cases.

Example

EC2 instance profile calls S3.

Use when

Prefer for compute. Prefer sparingly for humans.

Interview: User vs role — what is the difference?

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.
Card 5 of 20: Interview: User vs role — what is the difference? · Download PNG
Read card text

What this card says

What it is

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.

Why it matters

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. temporary

How / example

EC2 and Lambda use roles (instance profile / execution role). Humans increasingly use Identity Center, which also assumes roles under the hood.

Interview: Trust policy vs permissions policy?

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.
Card 6 of 20: Interview: Trust policy vs permissions policy? · Download PNG
Read card text

What this card says

What it is

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.

Why it matters

A perfect S3 permissions policy still fails if trust Principal does not include the caller. Wrong trust is a top lab AccessDenied cause. Principal

How / example

Example: trust Principal = arn:aws:iam::111:user/ravi; permissions Resource = arn:aws:s3:::lab-bucket/* with s3:GetObject.

Practical role examples: EC2, ECS, GitHub OIDC

Why this matters

The same idea shows up in every service, but the names change, and students paste long-lived keys “just to make it work”. Keys on a server, in GitHub secrets, or in function code are a common way student accounts get hijacked.

What it is

An EC2 instance profile wraps a role; the instance gets rotating keys from the metadata service. ECS needs two roles with trust for ecs-tasks.amazonaws.com: the execution role is for the ECS agent (ECR pull and logs), and the task role is for your container code (DynamoDB, S3). GitHub Actions can assume a role with OIDC (AssumeRoleWithWebIdentity) instead of storing AWS keys in GitHub secrets. A service-linked role (name starts with AWSServiceRoleFor…) is created and owned by an AWS service; you cannot edit its trust or policy. For a vendor in another account, an ExternalId in the trust policy stops the confused deputy problem.

How it works — a real example

Suppose Priya's GitHub repo must upload a build to S3. We add an OIDC provider for token.actions.githubusercontent.com, put a sub condition such as repo:org/repo:ref:refs/heads/main on the role trust, and in the workflow set permissions id-token: write with aws-actions/configure-aws-credentials and role-to-assume. No long-lived AKIA… key is stored in the repo. The repo name is made up for learning.

Common mistake / exam tip

The AWS SDK and CLI search credentials in a fixed order: environment variables, then shared config or profile, then web identity, then container, then EC2 metadata. An old AWS_ACCESS_KEY_ID in the environment silently beats the role, and you may think you are in the wrong account. Unset env keys, then run aws sts get-caller-identity. On ECS, CannotPullContainerError is the execution role; AccessDenied from app code is the task role.

EC2 reads S3 with a role

EC2 reads S3 with a role. Keys in ~/.aws/credentials on a server leak through Git and AMIs. Like a flat key copied by every tenant. An instance profile wraps a role. EC2 gets rotating keys from the metadata service. Create role → trust ec2.amazonaws.com → attach S3 read → Actions → Security → Modify IAM role. On the instance: aws sts get-caller-identity shows assumed-role/…/i-0abc.
Card 7 of 20: EC2 reads S3 with a role · Download PNG
Read card text

What this card says

Why do we need it?

Keys in ~/.aws/credentials on a server leak through Git and AMIs. Like a flat key copied by every tenant.

What is it?

An instance profile wraps a role. EC2 gets rotating keys from the metadata service. instance profile

How to do it

Create role → trust ec2.amazonaws.com → attach S3 read → Actions → Security → Modify IAM role.

Example

On the instance: aws sts get-caller-identity shows assumed-role/…/i-0abc. aws sts get-caller-identity

ECS: two roles

ECS: two roles. ECS needs to pull the image and your app needs DynamoDB. One role for both gives too much power. Execution role = ECS agent (ECR pull, logs). Task role = your code (DynamoDB, S3). Trust ecs-tasks.amazonaws.com in both. Set both ARNs in the task definition. CannotPullContainerError = execution role. AccessDenied from app = task role.
Card 8 of 20: ECS: two roles · Download PNG
Read card text

What this card says

Why do we need it?

ECS needs to pull the image and your app needs DynamoDB. One role for both gives too much power.

What is it?

Execution role = ECS agent (ECR pull, logs). Task role = your code (DynamoDB, S3). executionRoleArn / taskRoleArn

How to do it

Trust ecs-tasks.amazonaws.com in both. Set both ARNs in the task definition.

Example

CannotPullContainerError = execution role. AccessDenied from app = task role.

GitHub Actions with OIDC

GitHub Actions with OIDC. Storing AWS keys in GitHub secrets means long-lived keys that anyone with repo admin can misuse. GitHub issues an OIDC token. AWS trusts it and returns short STS credentials. Add provider token.actions.githubusercontent.com → role with sub condition repo:org/repo:ref:refs/heads/main. Workflow: permissions id-token: write + aws-actions/configure-aws-credentials with role-to-assume.
Card 9 of 20: GitHub Actions with OIDC · Download PNG
Read card text

What this card says

Why do we need it?

Storing AWS keys in GitHub secrets means long-lived keys that anyone with repo admin can misuse.

What is it?

GitHub issues an OIDC token. AWS trusts it and returns short STS credentials. AssumeRoleWithWebIdentity

How to do it

Add provider token.actions.githubusercontent.com → role with sub condition repo:org/repo:ref:refs/heads/main.

Example

Workflow: permissions id-token: write + aws-actions/configure-aws-credentials with role-to-assume. id-token: write

Interview: Why ExternalId in cross-account trust?

Why ExternalId in cross-account trust?. A secret-like value the vendor must send in sts:AssumeRole. Your trust policy checks it. It stops the confused deputy problem: another customer cannot trick the vendor into using your role. Condition StringEquals sts:ExternalId = "pune-acme-7731" in the trust policy.
Card 10 of 20: Interview: Why ExternalId in cross-account trust? · Download PNG
Read card text

What this card says

What it is

A secret-like value the vendor must send in sts:AssumeRole. Your trust policy checks it.

Why it matters

It stops the confused deputy problem: another customer cannot trick the vendor into using your role. sts:ExternalId

How / example

Condition StringEquals sts:ExternalId = "pune-acme-7731" in the trust policy.

Interview: What is a service-linked role?

What is a service-linked role?. A role created and owned by an AWS service, with a fixed trust and policy you cannot edit. The service needs exact rights to work. You cannot accidentally break it. Example: AWSServiceRoleForAutoScaling. Delete only after the service stops using it.
Card 11 of 20: Interview: What is a service-linked role? · Download PNG
Read card text

What this card says

What it is

A role created and owned by an AWS service, with a fixed trust and policy you cannot edit.

Why it matters

The service needs exact rights to work. You cannot accidentally break it. AWSServiceRoleFor…

How / example

Example: AWSServiceRoleForAutoScaling. Delete only after the service stops using it.

Interview: How does the SDK find role credentials?

How does the SDK find role credentials?. A fixed search order: env vars → shared config/profile → web identity → container → EC2 metadata. Old AWS_ACCESS_KEY_ID in env silently beats the role. Many "wrong account" bugs come from this. Unset env keys, then run aws sts get-caller-identity to see who you really are.
Card 12 of 20: Interview: How does the SDK find role credentials? · Download PNG
Read card text

What this card says

What it is

A fixed search order: env vars → shared config/profile → web identity → container → EC2 metadata.

Why it matters

Old AWS_ACCESS_KEY_ID in env silently beats the role. Many "wrong account" bugs come from this. AWS_ACCESS_KEY_ID

How / example

Unset env keys, then run aws sts get-caller-identity to see who you really are. aws sts get-caller-identity

Comparison: Credentials

IAM role vs Access keys. Temporary STS keys that rotate by themselves. / Long-lived keys that stay valid until you delete them.
Card 13 of 20: Comparison: Credentials · Download PNG
Read card text

What this card says

IAM role

Temporary STS keys that rotate by themselves.

Access keys

Long-lived keys that stay valid until you delete them.

Key points

No secret on disk or in Git. Easy to leak in code, AMIs and laptops.

Example

EC2 instance profile reads S3.

Use when

Use for EC2, Lambda, ECS, CI. Only when no role option exists.

Comparison: Who uses it

Task role vs Execution role. Your container code calls AWS (DynamoDB, S3). / The ECS agent pulls the image and writes logs.
Card 14 of 20: Comparison: Who uses it · Download PNG
Read card text

What this card says

Task role

Your container code calls AWS (DynamoDB, S3).

Execution role

The ECS agent pulls the image and writes logs.

Key points

App AccessDenied means fix this. CannotPullContainerError means fix this.

Example

taskRoleArn with DynamoDB PutItem.

Use when

Give only app permissions. Give only ECR and logs.

IAM policy JSON, managed vs inline, and explicit Deny

Why this matters

If every role has its own copy-pasted inline policy, you cannot fix one S3-read rule in one place. Students also think “I attached AdministratorAccess, so a Deny cannot matter” — then an explicit Deny or an SCP still blocks them.

What it is

A policy is JSON with Version, Statement, Effect, Action, Resource, and an optional Condition. Use Version 2012-10-17 for modern conditions. There are three kinds of identity policy you will meet: AWS managed (AWS-maintained, often broad), customer managed (your reusable JSON, attached to many users, roles or groups), and inline (glued to one identity). Evaluation starts from default deny (no Allow). An explicit Deny overrides any Allow, and AWS checks all applicable policies, including SCPs and permissions boundaries.

How it works — a real example

Suppose two lab roles both need to read lab-bucket. We write one customer-managed policy, ReadLabBucket, that allows s3:GetObject on arn:aws:s3:::lab-bucket/*, and attach it to both roles. Edit once, both roles update. If we later must block deletes even if someone attaches a broad Allow, we add an explicit Deny for s3:DeleteObject. Deny wins. The names are made up for learning.

Common mistake / exam tip

Prefer customer-managed policies for labs and apps. Use AWS managed policies carefully, because they are often broader than you need. Use inline policies only for a rare one-off exception. Remember: default deny means “no Allow was found”. Explicit Deny means “this is blocked even if an Allow exists”.

IAM policy JSON fields

Revision card: IAM policy JSON fields. Society notice: clubhouse Allow 6 a.m.–10 p.m. (Condition). IAM policies read the same way. Policy JSON: Version, Statement, Effect, Action, Resource, optional Condition. Use Version 2012-10-17 for modern conditions. Allow only the exact Actions and ARNs you need; add Condition for IP, MFA, time or tags. Allow s3:GetObject on arn:aws:s3:::lab-bucket/* — one bucket’s objects, not s3:*.
Card 15 of 20: IAM policy JSON fields · Download PNG
Read card text

What this card says

Why do we need it?

Society notice: clubhouse Allow 6 a.m.–10 p.m. (Condition). IAM policies read the same way.

What is it?

Policy JSON: Version, Statement, Effect, Action, Resource, optional Condition. Use Version 2012-10-17 for modern conditions. 2012-10-17

How to do it

Allow only the exact Actions and ARNs you need; add Condition for IP, MFA, time or tags.

Example

Allow s3:GetObject on arn:aws:s3:::lab-bucket/* — one bucket’s objects, not s3:*.

Managed vs inline policies

Revision card: Managed vs inline policies. Inline-only sprawl makes the same S3-read rule impossible to update in one place. AWS managed = AWS-maintained broad policies. Customer managed = your reusable JSON. Inline = glued to one user/role/group. Prefer customer-managed policies for labs and apps; use AWS managed carefully (often too broad); use inline sparingly. One customer-managed ReadLabBucket attached to two roles updates both when you edit the policy once.
Card 16 of 20: Managed vs inline policies · Download PNG
Read card text

What this card says

Why do we need it?

Inline-only sprawl makes the same S3-read rule impossible to update in one place.

What is it?

AWS managed = AWS-maintained broad policies. Customer managed = your reusable JSON. Inline = glued to one user/role/group. customer managed

How to do it

Prefer customer-managed policies for labs and apps; use AWS managed carefully (often too broad); use inline sparingly.

Example

One customer-managed ReadLabBucket attached to two roles updates both when you edit the policy once.

Explicit Deny always wins

Revision card: Explicit Deny always wins. Students think “I attached AdministratorAccess so Deny cannot matter” — then SCP or explicit Deny blocks them. Default deny means no Allow. Explicit Deny overrides any Allow. Evaluation checks all applicable policies including SCPs and boundaries. Use explicit Deny to document “never from outside this IP” even if a future broad Allow appears. Allow s3:* plus Deny s3:DeleteObject still blocks deletes — Deny wins.
Card 17 of 20: Explicit Deny always wins · Download PNG
Read card text

What this card says

Why do we need it?

Students think “I attached AdministratorAccess so Deny cannot matter” — then SCP or explicit Deny blocks them.

What is it?

Default deny means no Allow. Explicit Deny overrides any Allow. Evaluation checks all applicable policies including SCPs and boundaries. Explicit Deny

How to do it

Use explicit Deny to document “never from outside this IP” even if a future broad Allow appears.

Example

Allow s3:* plus Deny s3:DeleteObject still blocks deletes — Deny wins.

Comparison: Reuse

Comparison: Customer managed policy vs Inline policy. Customer managed policy: Reusable JSON attached to many users/roles/groups; edit once. Inline policy: Glued to one identity; copy-paste to reuse.
Card 18 of 20: Comparison: Reuse · Download PNG
Read card text

What this card says

Customer managed policy

Reusable JSON attached to many users/roles/groups; edit once.

Inline policy

Glued to one identity; copy-paste to reuse.

Key points

Apps, labs, shared least-privilege rules. One-off exceptions (use sparingly).

Example

ReadLabBucket customer managed on two roles.

Use when

Default for most work. Avoid sprawl.

Interview: Explicit Deny vs default deny?

Interview card: Explicit Deny vs default deny?. Default deny: if nothing Allows the action, it is denied. Explicit Deny: a Deny statement overrides any Allow from other policies. SCPs, permission boundaries, and resource policies participate too — an Allow on the role is not the whole story. Allow s3:* plus Deny s3:DeleteBucket still blocks delete — Deny wins. Missing Allow alone is enough to deny ListBucket.
Card 19 of 20: Interview: Explicit Deny vs default deny? · Download PNG
Read card text

What this card says

What it is

Default deny: if nothing Allows the action, it is denied. Explicit Deny: a Deny statement overrides any Allow from other policies. Explicit Deny

Why it matters

SCPs, permission boundaries, and resource policies participate too — an Allow on the role is not the whole story.

How / example

Allow s3:* plus Deny s3:DeleteBucket still blocks delete — Deny wins. Missing Allow alone is enough to deny ListBucket.

Interview: Managed vs inline — when to use which?

Interview card: Managed vs inline — when to use which?. Customer managed: reusable, editable in one place — default for apps/labs. AWS managed: convenient but often broad. Inline: one identity only — use sparingly. Trust policies on roles are resource-based JSON on the role, separate from identity-based managed/inline permissions. Put ReadLabBucket as customer managed; attach to several roles; avoid copying the same inline JSON five times.
Card 20 of 20: Interview: Managed vs inline — when to use which? · Download PNG
Read card text

What this card says

What it is

Customer managed: reusable, editable in one place — default for apps/labs. AWS managed: convenient but often broad. Inline: one identity only — use sparingly. customer managed

Why it matters

Trust policies on roles are resource-based JSON on the role, separate from identity-based managed/inline permissions.

How / example

Put ReadLabBucket as customer managed; attach to several roles; avoid copying the same inline JSON five times.

FAQ

What is the difference between an IAM user and an IAM role?

An IAM user is a named, long-lived identity with a password and/or access keys until you delete them. An IAM role has no long-lived password or access key. A caller assumes it, and STS returns temporary credentials.

What is the difference between a trust policy and a permissions policy?

The trust policy answers who may call sts:AssumeRole on this role. The permissions policy answers what API actions the resulting session may perform on which resources.

Why should compute use a role instead of access keys?

Role credentials rotate by themselves and need not sit on disk or in Git. Long-lived access keys stay valid until you delete them, and they are easy to leak in code, AMIs and laptops.

Why does ECS need two roles?

The execution role is for the ECS agent (pull the image from ECR, write logs). The task role is for your container code (for example DynamoDB or S3). One role for both gives too much power, and it makes AccessDenied harder to debug.

What is GitHub OIDC used for here?

GitHub issues an OIDC token. AWS trusts that token and returns short STS credentials with AssumeRoleWithWebIdentity, so you do not store long-lived AWS keys in GitHub secrets.

What is an ExternalId?

A secret-like value the vendor must send in sts:AssumeRole. Your trust policy checks sts:ExternalId. This stops the confused deputy problem: another customer cannot trick the vendor into using your role.

Does an explicit Deny beat AdministratorAccess?

Yes. Explicit Deny overrides any Allow. AWS also starts from default deny when no Allow is found. SCPs and permissions boundaries are part of the same evaluation.

When should I use a customer-managed policy instead of an inline policy?

Use a customer-managed policy as the default, so you can attach the same JSON to many identities and edit it once. Use an inline policy only for a rare one-off exception, so you do not get copy-paste sprawl.

These chapter pages are already on the site (checked live, HTTP 200, on 19 Nov 2026):

See all AWS revision cards