TExcelTAD
Home › AWS Cloud › IAM Policies

IAM Policies

An IAM policy is a JSON document with one or more statements. Each statement says: this Effect (Allow or Deny) applies to these Actions on these Resources, optionally only under some Conditions.

json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ReadReports",
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:ListBucket"],
      "Resource": ["arn:aws:s3:::tad-reports", "arn:aws:s3:::tad-reports/*"]
    },
    {
      "Sid": "NeverDeleteReports",
      "Effect": "Deny",
      "Action": "s3:DeleteObject",
      "Resource": "arn:aws:s3:::tad-reports/*"
    }
  ]
}
Element Meaning
Version Policy language version, always "2012-10-17"
Sid Optional statement name
Effect Allow or Deny
Action API actions like s3:GetObject; wildcards allowed (s3:Get*, *)
Resource ARNs the statement applies to; wildcards allowed
Condition Extra tests (source IP, MFA present, tags, time ...)
Principal Only in resource-based policies: who the policy applies to

The evaluation logic

When a request arrives, IAM collects every statement that applies and decides:

  1. Explicit deny wins. If any matching statement says Deny, the request is denied.
  2. Otherwise, if any matching statement says Allow, it is allowed.
  3. Otherwise it is denied by default (implicit deny).

So everything starts denied, Allow opens doors, and Deny closes them for good.

Model: a policy evaluator

This evaluator implements the three rules and IAM-style wildcards (* matches any characters, ? matches one). Action names are case-insensitive in IAM, resource ARNs are case-sensitive.

python · try it yourself
from fnmatch import fnmatchcase

def matches(patterns, value, ignore_case=False):
    if isinstance(patterns, str):
        patterns = [patterns]
    if ignore_case:
        value = value.lower()
        patterns = [p.lower() for p in patterns]
    return any(fnmatchcase(value, p) for p in patterns)

def evaluate(policies, action, resource):
    allowed = False
    for policy in policies:
        for st in policy["Statement"]:
            if matches(st["Action"], action, ignore_case=True) and matches(st["Resource"], resource):
                if st["Effect"] == "Deny":
                    return f"DENY (explicit, {st.get('Sid', 'no sid')})"
                allowed = True
    return "ALLOW" if allowed else "DENY (implicit)"

policy = {"Statement": [
    {"Sid": "ReadReports", "Effect": "Allow", "Action": ["s3:Get*", "s3:ListBucket"],
     "Resource": ["arn:aws:s3:::tad-reports", "arn:aws:s3:::tad-reports/*"]},
    {"Sid": "NeverDelete", "Effect": "Deny", "Action": "s3:DeleteObject",
     "Resource": "arn:aws:s3:::tad-reports/*"},
    {"Sid": "AllS3InSandbox", "Effect": "Allow", "Action": "s3:*",
     "Resource": "arn:aws:s3:::tad-sandbox*"},
]}

requests = [
    ("s3:GetObject", "arn:aws:s3:::tad-reports/2026/q1.csv"),
    ("s3:getobject", "arn:aws:s3:::tad-reports/2026/q1.csv"),
    ("s3:PutObject", "arn:aws:s3:::tad-reports/2026/q1.csv"),
    ("s3:DeleteObject", "arn:aws:s3:::tad-reports/old.csv"),
    ("s3:DeleteObject", "arn:aws:s3:::tad-sandbox-ada/tmp.txt"),
    ("ec2:StartInstances", "arn:aws:ec2:eu-west-1:123456789012:instance/i-0abc"),
]
for action, resource in requests:
    print(f"{evaluate([policy], action, resource):<28} {action:<20} {resource}")
ALLOW                        s3:GetObject         arn:aws:s3:::tad-reports/2026/q1.csv
ALLOW                        s3:getobject         arn:aws:s3:::tad-reports/2026/q1.csv
DENY (implicit)              s3:PutObject         arn:aws:s3:::tad-reports/2026/q1.csv
DENY (explicit, NeverDelete) s3:DeleteObject      arn:aws:s3:::tad-reports/old.csv
ALLOW                        s3:DeleteObject      arn:aws:s3:::tad-sandbox-ada/tmp.txt
DENY (implicit)              ec2:StartInstances   arn:aws:ec2:eu-west-1:123456789012:instance/i-0abc

Look at the deletes: the same action is denied on tad-reports (explicit deny) but allowed on the sandbox bucket, where no deny applies. And ec2:StartInstances is never mentioned, so it is implicitly denied.

Conditions

A Condition block narrows a statement. This one only allows terminating instances when the caller signed in with MFA:

json
{
  "Effect": "Allow",
  "Action": "ec2:TerminateInstances",
  "Resource": "*",
  "Condition": { "Bool": { "aws:MultiFactorAuthPresent": "true" } }
}

Let's add condition support for two common operators to the model:

python · try it yourself
import ipaddress
from fnmatch import fnmatchcase

def condition_ok(cond, ctx):
    for op, tests in cond.items():
        for key, expected in tests.items():
            actual = ctx.get(key)
            if op == "Bool" and str(actual).lower() != expected:
                return False
            if op == "IpAddress" and (actual is None or
                    ipaddress.ip_address(actual) not in ipaddress.ip_network(expected)):
                return False
    return True

def evaluate(statements, action, resource, ctx):
    allowed = False
    for st in statements:
        if fnmatchcase(action.lower(), st["Action"].lower()) and fnmatchcase(resource, st["Resource"]) \
                and condition_ok(st.get("Condition", {}), ctx):
            if st["Effect"] == "Deny":
                return "DENY (explicit)"
            allowed = True
    return "ALLOW" if allowed else "DENY (implicit)"

statements = [
    {"Effect": "Allow", "Action": "ec2:TerminateInstances", "Resource": "*",
     "Condition": {"Bool": {"aws:MultiFactorAuthPresent": "true"}}},
    {"Effect": "Deny", "Action": "*", "Resource": "*",
     "Condition": {"IpAddress": {"aws:SourceIp": "198.51.100.0/24"}}},
]

contexts = [
    {"aws:MultiFactorAuthPresent": True,  "aws:SourceIp": "203.0.113.10"},
    {"aws:MultiFactorAuthPresent": False, "aws:SourceIp": "203.0.113.10"},
    {"aws:MultiFactorAuthPresent": True,  "aws:SourceIp": "198.51.100.7"},
]
for ctx in contexts:
    print(evaluate(statements, "ec2:TerminateInstances", "arn:aws:ec2:eu-west-1:123456789012:instance/i-1", ctx), ctx)
ALLOW {'aws:MultiFactorAuthPresent': True, 'aws:SourceIp': '203.0.113.10'}
DENY (implicit) {'aws:MultiFactorAuthPresent': False, 'aws:SourceIp': '203.0.113.10'}
DENY (explicit) {'aws:MultiFactorAuthPresent': True, 'aws:SourceIp': '198.51.100.7'}

Kinds of policies

Type Attached to Example
AWS managed Users/groups/roles; written by AWS ReadOnlyAccess, AmazonS3FullAccess
Customer managed Users/groups/roles; written by you, reusable tad-reports-reader
Inline Embedded in exactly one identity rarely recommended
Resource-based A resource (S3 bucket policy, SQS queue policy) has a Principal element
Permissions boundary Caps what an identity's policies can grant delegated admin setups
Service control policy (SCP) An AWS Organizations account or OU "no one may leave eu-west-1"

A request must be allowed by every layer that applies: an SCP or permissions boundary can only take away, never add.

Tip: Test policies before deploying with the IAM Policy Simulator or aws iam simulate-principal-policy.

Go further: Writing an AWS-Style Policy Evaluator implements the allow/deny rules in Python, and Permission Boundaries and SCPs adds the full evaluation chain.

Test yourself

A user has one policy allowing s3:* and another denying s3:DeleteObject. What happens on s3:DeleteObject?

  1. Allowed, the allow is broader
  2. Denied, an explicit deny always wins
  3. It depends on the order of the policies
  4. Allowed only in us-east-1

What happens to a request that no policy mentions?

  1. It is allowed
  2. It is denied (implicit deny)
  3. It is logged and allowed
  4. AWS asks the root user

Which element appears in resource-based policies but not in identity-based policies?

  1. Effect
  2. Action
  3. Principal
  4. Version