Conditions and policy evaluation
Narrow policies with conditions, cap permissions with boundaries, and work out how AWS decides allow or deny.
What it is
Basic policies say who can do what on which resource. Real designs need more:
- Conditions add when: only from the office IP range, only in two Regions, only with MFA, only on resources with a certain tag.
- Permissions boundaries set the most an identity can ever be given, even by an admin who can write policies.
- Policy evaluation logic is how AWS combines every policy that applies to a request into one answer: allow or deny.
IAM conditions
A Condition block makes a statement apply only when its test is true. It uses an operator (such as StringEquals, IpAddress, Bool) and a condition key (such as aws:SourceIp).
aws:SourceIp: where the call comes from
Restricts the IP address the API call is made from. This policy denies everything unless the call comes from two company IP ranges:
{
"Effect": "Deny",
"Action": "*",
"Resource": "*",
"Condition": {
"NotIpAddress": { "aws:SourceIp": ["192.0.2.0/24", "203.0.113.0/24"] }
}
}aws:RequestedRegion: which Region the call goes to
Restricts the Region the API call is made to. This denies EC2, RDS and DynamoDB actions outside Frankfurt and Ireland:
{
"Effect": "Deny",
"Action": ["ec2:*", "rds:*", "dynamodb:*"],
"Resource": "*",
"Condition": {
"StringNotEquals": { "aws:RequestedRegion": ["eu-central-1", "eu-west-1"] }
}
}ec2:ResourceTag and aws:PrincipalTag: matching tags
Allows starting and stopping only instances tagged Project=DataAnalytics, and only for users tagged Department=Data:
{
"Effect": "Allow",
"Action": ["ec2:StartInstances", "ec2:StopInstances"],
"Resource": "arn:aws:ec2:us-east-1:123456789012:instance/*",
"Condition": {
"StringEquals": {
"ec2:ResourceTag/Project": "DataAnalytics",
"aws:PrincipalTag/Department": "Data"
}
}
}Using tags like this is called attribute-based access control (ABAC): add a tag to a new resource or person and the permissions follow, with no policy change.
aws:MultiFactorAuthPresent: require MFA
Allows all EC2 actions, but denies stopping and terminating instances unless the user signed in with MFA:
{
"Version": "2012-10-17",
"Statement": [
{ "Effect": "Allow", "Action": "ec2:*", "Resource": "*" },
{
"Effect": "Deny",
"Action": ["ec2:StopInstances", "ec2:TerminateInstances"],
"Resource": "*",
"Condition": { "BoolIfExists": { "aws:MultiFactorAuthPresent": false } }
}
]
}The other examples on this page show a single statement; in a real policy each sits inside "Statement": [ … ] like this.
aws:PrincipalOrgID: anyone in my organization
Used in resource-based policies to allow every account in an AWS organization at once, without listing each account ID. This bucket policy lets any member account read and write one folder, and blocks everyone outside the organization:
{
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::2022-financial-data/*",
"Condition": {
"StringEquals": { "aws:PrincipalOrgID": ["o-yyyyyyyyyy"] }
}
}IAM for S3: bucket vs object
S3 permissions apply at two levels, and the Resource ARN must match:
s3:ListBucketis a bucket-level action. Its resource is the bucket:arn:aws:s3:::test.s3:GetObject,s3:PutObject,s3:DeleteObjectare object-level actions. Their resource is the objects:arn:aws:s3:::test/*.
{
"Version": "2012-10-17",
"Statement": [
{ "Effect": "Allow", "Action": "s3:ListBucket", "Resource": "arn:aws:s3:::test" },
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
"Resource": "arn:aws:s3:::test/*"
}
]
}Writing arn:aws:s3:::test for GetObject (or arn:aws:s3:::test/* for ListBucket) silently grants nothing.
Permissions boundaries
A permissions boundary is a managed policy attached to an IAM user or role (not a group) that sets the maximum permissions it can have. It never grants anything by itself.
Three circles overlap: the organization's SCPs and RCPs, the permissions boundary, and the identity-based policy. An action is allowed only in the middle, where all three allow it.
For example: the boundary allows s3:*, cloudwatch:* and ec2:*. The user's policy allows iam:CreateUser. Result: no permissions at all, because nothing is allowed by both.
Use cases:
- Delegate safely. Let a team lead create IAM users for their team, with a boundary so the new users can never get more than the team's limits.
- Stop privilege escalation. Developers can write their own policies but can't make themselves administrators, because the boundary caps them.
- Limit one user or role, where an SCP would restrict a whole account.
Policy evaluation logic
When a request arrives, AWS gathers every policy that applies and decides. Three rules sum it up:
- By default, everything is denied (an implicit deny).
- An explicit
Denyin any policy always wins. - Otherwise, the request needs an
Allowfrom every policy type that applies.
The checks run in this order:
- Explicit deny? If any policy denies the action, the answer is Deny.
- Organization policies: if the account is in an organization, its SCPs and RCPs must allow the action, or it's denied.
- Resource-based policy: if the resource has one (a bucket policy, for example) and it allows the caller, the request is allowed (*).
- Identity-based policy: the user's or role's policies must allow it.
- Permissions boundary: if one is set, it must allow it.
- Session policy: if the caller passed one when assuming a role, it must allow it.
- If everything that applies allows it, the answer is Allow.
(*) Within one account, a resource-based policy that names the caller is enough on its own. Across accounts, both sides must allow: the caller's identity policy in its account, and the resource-based policy in the other account.
Worked example
This policy is attached to a user:
{
"Version": "2012-10-17",
"Statement": [
{ "Action": "sqs:*", "Effect": "Deny", "Resource": "*" },
{ "Action": ["sqs:DeleteQueue"], "Effect": "Allow", "Resource": "*" }
]
}- Can the user call
sqs:CreateQueue? No: the explicit deny onsqs:*matches. - Can the user call
sqs:DeleteQueue? No: the explicit deny still wins over the allow. - Can the user call
ec2:DescribeInstances? No: nothing allows it, so the implicit deny applies.
Exam tips
- "Only allow API calls from the corporate network" → condition
aws:SourceIp. - "Restrict which Regions can be used" →
aws:RequestedRegion(in an IAM policy or, across accounts, an SCP). - "Allow every account in our organization to access the bucket" → bucket policy with
aws:PrincipalOrgID. - "Let developers create roles without being able to escalate privileges" → permissions boundary.
- "Policy allows and another denies" → explicit deny wins.
- "
ListBucketdoesn't work butGetObjectdoes" → wrong resource ARN level (bucket vsbucket/*).
Common traps
- "A permissions boundary grants permissions." It only limits. The identity still needs an allow from its own policy.
- "Permissions boundaries can be set on groups." Only on users and roles.
- "An allow overrides a deny if it's more specific." An explicit deny always wins.
- "Cross-account: a bucket policy allowing the other account is enough." The caller's own identity policy must also allow the action.
Hands-on lab: test policies with the IAM policy simulator
Goal: check the worked example and a Region condition without touching real resources.
Free
The IAM policy simulator only evaluates policies; nothing is created except a test policy, which is free.
Create a test policy
Open IAM → Policies → Create policy → JSON and paste the worked example above. Name it saa-sqs-test and create it.
Open the simulator
Go to policysim.aws.amazon.com. In the left panel choose Policies, then select saa-sqs-test under customer managed policies.
Run the three questions
Choose service Amazon SQS, tick CreateQueue and DeleteQueue, and Run simulation. Both show denied, with explicit deny. Add Amazon EC2 → DescribeInstances: implicitly denied.
Try a condition
Edit the policy to add a statement allowing ec2:DescribeInstances with "Condition": {"StringEquals": {"aws:RequestedRegion": "eu-west-1"}}. In the simulator, set the context key aws:RequestedRegion to eu-west-1 (allowed), then to us-east-1 (denied).
Expected result: the simulator confirms each answer and shows which statement decided it.
Clean up: delete the saa-sqs-test policy.