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

AWS Cloud

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.

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

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.

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

Start by writing the request you want to evaluate in plain words. A role attempts s3:GetObject on a particular report object. Read statements whose action and resource match that request. A condition narrows the circumstances where the statement applies. A statement with a nonmatching action or a condition that does not apply gives no access by itself.

Try three outcomes on paper before the lab. With no applicable Allow, the request is denied by default. With an applicable Allow and no other control preventing access, it can succeed. If an explicit Deny also applies to the request, it takes priority. If you change only the path, IP or principal, work through the same steps again. This makes it easier to see why a valid policy document may give a result different from the one you intended.

  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.

Common errors: cause and safe next check

An edited managed policy still behaves like the old one

A saved managed-policy version is not necessarily the operative default version. Check the default version on the exact policy ARN and the identities using it. Do not keep adding broad statements to compensate for testing an older active version.

A condition matches at home but fails on another network path

The request context depends on how the request reaches AWS. For example, aws:SourceIp is not present for a request through a VPC endpoint. Inspect the supported key and its availability for that path; do not remove all conditions just to silence a denial.

Use a two-row test for missing context. An Allow with IpAddress and aws:SourceIp can match a direct request from the named range. The same condition does not match a VPC endpoint request where that key is absent. Adding IfExists makes that condition pass when the key is missing, so the same Allow can become broader. Decide the intended result for each path before changing the operator.

Two similar policy names lead to different results

A name seen in a screenshot is not proof of the attachment being tested. Compare the full intended policy ARN, attached identity and default version. Review the actual document and the real action/resource rather than guessing from the friendly label.

Practical questions and full answers

Is the JSON Version date the same as managed-policy version v2?

No. The JSON Version field selects the policy-language version. A managed-policy version such as v1 or v2 is a stored revision of one policy. Changing the language date is not how you activate a saved revision.

Can I edit an AWS managed policy directly?

No. AWS owns that policy. A customer managed policy is the option when you need your own reviewed permissions. Check every identity sharing it before approving a change, because its default version applies wherever it is attached.

Does the editor accepting JSON prove that the live request will be allowed?

No. Syntax validation checks the document, while authorization also depends on the request’s action, resource, conditions and other applicable controls. Treat policy checks as useful review evidence, not a replacement for understanding the effective request context.

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.

Separate resource ARN meaning from condition behavior

List one bucket using the bucket ARN and read one object using the object ARN. Test both a matching and nonmatching request. For MFA conditions check when the context key may be absent, especially with long-term credentials.

Interview check Does Bool false match a missing aws:MultiFactorAuthPresent key?

No. For an explicit Deny intended to include absent or false, review BoolIfExists and validate the effect on intended identities before applying it.

Reference documentation

Practise this chapter

Interview questions and answers

How do you review a policy without being distracted by the JSON syntax first?

I would write out the intended principal, action, resource and relevant context in plain language. Then I would mark which statements match that request, whether an applicable Allow exists and whether any explicit Deny or other control blocks it. Finally I would test both intended outcomes.

Check your understanding

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

0 of 3 answered correctly
1. An S3 read matches no applicable Allow. What is the usual result?

Answer and explanation

A. Denied by default

AWS access starts denied. An applicable Allow is necessary, subject to other controls.

2. What does an IAM Condition control?

Answer and explanation

C. The circumstances in which a statement matches

A condition compares request context with policy settings. When it does not apply, that statement may not match.

3. Removing an unrelated Allow fixes an applicable explicit Deny. Is that correct?

Answer and explanation

A. No, evaluate the statement whose Deny actually matches

The matching explicit Deny takes precedence regardless of unrelated Allow statements.

Evaluate a policy statement against three request records

Before you start

  • Use the classroom policy examples and write down actions, resources and condition values. No policy attachment required.
  1. Take a policy that allows s3:GetObject on arn:aws:s3:::training-reports/team/* when the request matches the approved source context. Write one read of team/day1.csv from that context.

    Expected result: The service action, prefix and condition all match the allow statement in the exercise.

  2. Create a second request that reads personal/day1.csv under the same context and a third that uses the correct key from an unapproved path.

    Expected result: The second request misses the resource scope. The third misses the condition. Without another Allow, both fail.

  3. Add an applicable Deny for team/day1.csv and evaluate the first request again. Write the reason in one sentence.

    Expected result: The specific Deny takes priority over the broad Allow for that request.

If the result differs

  • If your results seem inconsistent, compare the full object ARN and whether the context key is present, then reread the condition operator.

Finish and clean up

  • No policy changes were made. Keep only the test notes.

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

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

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

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

  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

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

Allow dynamodb:GetItem, dynamodb:PutItem, dynamodb:Query on arn:aws:dynamodb:ap-south-1:125324833677:table/blrhealthtech-orders 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 Start and stop servers only

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

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

  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

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

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

  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

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

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

  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

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

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

  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

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

Create a policy named blrhealthtech-s3-read from the document in policy.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 Deny access from outside the office

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

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

  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

Tag conditions let one team control only its own servers.

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

  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

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

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

  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

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

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

  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)

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

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

  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.