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

AWS IAM Policies: JSON, Managed, Inline and Conditions Explained

An IAM policy is a JSON rule sheet: Allow or Deny which actions on which resources, sometimes only when a Condition is true. Managed policies are reusable; inline policies stick to one identity.

What you'll learn in this chapter

  • Policy JSON skeleton
  • Effect, Action, Resource
  • Condition keys idea
  • AWS managed vs customer vs inline
  • Explicit Deny wins

1. Everyday example first

Why do we need this?

A housing society notice: “Residents may use the clubhouse (Allow) from 6 a.m. to 10 p.m. (Condition). Guests are Deny after 10.” IAM policies read the same way.

What is it?

IAM identity-based policies attach to users, groups or roles. Resource-based policies (bucket policy, role trust) sit on the resource. Both use similar JSON.

{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "ReadOneBucket",
    "Effect": "Allow",
    "Action": ["s3:GetObject", "s3:ListBucket"],
    "Resource": [
      "arn:aws:s3:::lab-bucket",
      "arn:aws:s3:::lab-bucket/*"
    ],
    "Condition": {
      "IpAddress": { "aws:SourceIp": "203.0.113.10/32" }
    }
  }]
}

2. Each field

FieldMeaning
VersionUse "2012-10-17"
StatementArray of rules
SidOptional human label
EffectAllow or Deny
ActionAPI names: s3:GetObject, ec2:StartInstances, *
ResourceARNs the action applies to
ConditionOptional extra checks (IP, MFA, tags, time)

3. Managed vs inline

  • AWS managed — AWS writes/updates (e.g. ReadOnlyAccess). Fast, often broad.
  • Customer managed — you write once, attach to many roles. Best for least privilege.
  • Inline — embedded in one user/role. Hard to reuse; use sparingly.

4. Evaluation mental model

  1. Start from deny by default (no Allow → no access).
  2. Any matching Allow opens the door.
  3. Any matching explicit Deny closes it again — Deny wins.
  4. SCPs, boundaries, session policies can also restrict.

Ravindra Bagale's Tip

Action on the wrong Resource ARN is a classic bug: ListBucket needs the bucket ARN; GetObject needs object ARNs (…/*).

5. Useful Condition keys

  • aws:SourceIp — only from office/VPN CIDR.
  • aws:MultiFactorAuthPresent — sensitive actions need MFA.
  • aws:RequestedRegion — lock to ap-south-1 for labs.
  • aws:ResourceTag/… / aws:PrincipalTag/… — attribute-based access.

6. Interview angles

Q. Explicit Deny vs default deny?

Default = no Allow; Explicit Deny overrides Allows.

Q. Why Version 2012-10-17?

Enables modern policy features/conditions.

Q. Managed vs inline?

Managed reusable; inline one identity.

Q. Trust policy is which type?

Resource-based policy on the role.

Q. Condition optional?

Yes — but powerful for least privilege.

Try at home

  1. Copy the sample policy and change the IP to yours; read it aloud as Allow/Action/Resource/Condition.
  2. Next: write and test a real Condition policy in the console.

In short

  1. Policy JSON: Effect, Action, Resource, optional Condition.
  2. Customer managed beats broad AWS managed for labs.
  3. Explicit Deny always wins.
  4. Right ARN shape matters.

Practice Tasks

About this scenario

Bengaluru HealthTech is a health technology business in Bengaluru. Arjun Rao 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

Easy Read-only access to one bucket

Why: Least privilege: give only the actions and the bucket the job needs.

What to do: Write an identity policy that lets the user read objects (s3:GetObject) only in bucket blrhealthtech-data.

How:
  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 Lambda access to one table

Why: The function only needs to read and write items in its own table.

What to do: Allow dynamodb:GetItem, dynamodb:PutItem, dynamodb:Query on arn:aws:dynamodb:ap-south-1:125324833677:table/blrhealthtech-orders only.

How:
  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 Start and stop servers only

Why: The night operator may start and stop servers but not terminate them.

What to do: Allow ec2:StartInstances and ec2:StopInstances on all instances in account 125324833677, Region ap-south-1. Also allow ec2:DescribeInstances on *.

How:
  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 List and read one bucket

Why: ListBucket works on the bucket ARN; GetObject works on object ARNs.

What to do: Allow s3:ListBucket on bucket blrhealthtech-data and s3:GetObject on all its objects. Nothing else.

How:
  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 Upload-only to one folder

Why: Field staff can drop files but cannot read or delete anything.

What to do: Allow s3:PutObject only under the prefix uploads/ in bucket blrhealthtech-data.

How:
  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 managed policy to a group

Why: AWS managed policies are ready-made and kept up to date by AWS.

What to do: Attach the AWS managed policy arn:aws:iam::aws:policy/ReadOnlyAccess to the group Auditors.

How:
  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 Create a customer managed policy

Why: Your own managed policy can be reused on many users, groups and roles.

What to do: Create a policy named blrhealthtech-s3-read from the document in policy.json.

How:
  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 Deny access from outside the office

Why: An explicit Deny with aws:SourceIp blocks keys used from anywhere else.

What to do: Write one Deny statement: deny all actions (*) on all resources (*) when the source IP is NOT in 203.0.113.0/24.

How:
  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 only on tagged servers

Why: Tag conditions let one team control only its own servers.

What to do: Allow ec2:StopInstances on any instance (*) only when the instance tag Team equals blrhealthtech.

How:
  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 Test a policy before using it

Why: The policy simulator tells you if an action would be allowed, without doing it.

What to do: Check whether user arn:aws:iam::125324833677:user/arjun may call s3:GetObject.

How:
  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 Require MFA to delete

Why: Risky actions should need MFA, so a stolen password is not enough.

What to do: Deny s3:DeleteObject on arn:aws:s3:::blrhealthtech-data/* when MFA is not present (aws:MultiFactorAuthPresent is false).

How:
  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 Block other Regions (SCP style)

Why: Data rules (like keeping data in one country) are enforced with aws:RequestedRegion.

What to do: Deny ec2:* on * when aws:RequestedRegion is not ap-south-1.

How:
  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.

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: 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:*.
Revision card: IAM policy JSON fields · Download PNG · Open card page
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.
Revision card: Managed vs inline policies · Download PNG · Open card page
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.
Revision card: Explicit Deny always wins · Download PNG · Open card page
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.
Comparison: Customer managed policy vs Inline policy · Download PNG · Open card page
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.
Interview card: Explicit Deny vs default deny? · Download PNG · Open card page
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.
Interview card: Managed vs inline — when to use which? · Download PNG · Open card page

More cards: Ravi Sir's Cards.