Sub topic 7
Narrowing statements with conditions
This chapter assumes you can read a policy document and know the roles of Effect, Action, and Resource. It covers the Condition element: how it is built, how AWS evaluates it, and the mistakes that most often cause surprising results.
What a condition does
A statement without a condition applies to every matching request. A Condition element adds a test on the request context. The statement takes effect only when the test is true. If the test is false, AWS treats the statement as if it did not match, and evaluation moves on.
This works the same way for both effects:
- On an
Allow, a false condition means the statement grants nothing. The request falls back to the implicit deny unless another policy allows it. - On a
Deny, a false condition means the statement does not apply. The deny is not triggered.
The request context is the set of key-value pairs AWS builds for each request. It includes the caller's identity, the time, the source IP address, whether TLS was used, tags, and keys supplied by the service being called.
Anatomy of a condition block
A condition has three parts: an operator, a condition key, and one or more values.
"Condition": {
"StringEquals": {
"aws:PrincipalTag/team": "reports"
}
}StringEqualsis the condition operator. It decides how the key is compared with the value.aws:PrincipalTag/teamis the condition key. AWS reads its value from the request context."reports"is the value you compare against.
Each of these parts is a place where mistakes happen, so the next sections take them in turn.
Condition keys
Keys fall into two groups.
| Group | Examples | Where the value comes from |
|---|---|---|
| Global keys, prefixed `aws:` | `aws:SecureTransport`, `aws:SourceIp`, `aws:PrincipalOrgID`, `aws:CurrentTime`, `aws:PrincipalTag/key`, `aws:RequestTag/key` | AWS itself. Many services support them. |
| Service keys, prefixed by the service | `s3:prefix`, `s3:x-amz-server-side-encryption`, `ec2:ResourceTag/key`, `dynamodb:LeadingKeys` | The individual service. Only meaningful for that service's actions. |
Two details are worth knowing now.
First, not every key is present in every request. aws:SourceIp is absent when a request comes through a VPC endpoint, because the request carries aws:VpcSourceIp instead. aws:MultiFactorAuthAge exists only when the caller signed in with MFA. A key that is missing from the context cannot match a positive test.
Second, key names are not case-sensitive, but the values you compare usually are. StringEquals treats Reports and reports as different.
Operators
Operators are grouped by the type of data they compare.
| Type | Operators | Typical use |
|---|---|---|
| String | `StringEquals`, `StringNotEquals`, `StringLike`, `StringNotLike` | Tags, prefixes, organization IDs |
| Numeric | `NumericEquals`, `NumericLessThan`, `NumericGreaterThanEquals` | Limits such as `s3:max-keys` |
| Date | `DateGreaterThan`, `DateLessThan` | Time-limited access |
| Boolean | `Bool` | `aws:SecureTransport`, `aws:MultiFactorAuthPresent` |
| IP address | `IpAddress`, `NotIpAddress` | Restricting to CIDR ranges |
| ARN | `ArnEquals`, `ArnLike` | Source ARNs, role ARNs |
| Existence | `Null` | Testing whether a key is present |
StringLike and ArnLike allow the wildcards * and ?. StringEquals does not, so "s3:prefix": "reports/*" under StringEquals matches only a literal asterisk.
Set operators and the IfExists suffix
Two modifiers change how an operator behaves.
ForAllValues:andForAnyValue:apply to keys that hold several values, such as a list of tag keys in a request.ForAnyValueis true if at least one request value matches.ForAllValuesis true if every request value matches.IfExistsappended to an operator, as inStringEqualsIfExists, makes the test pass when the key is absent. If the key is present, the normal comparison runs.
ForAllValues has a well-known trap. It is also true when the request has no values for the key at all, because there is nothing that fails to match. To block that case, pair it with a Null test:
"Condition": {
"ForAllValues:StringEquals": {
"aws:TagKeys": ["team", "env"]
},
"Null": {
"aws:TagKeys": "false"
}
}"Null": {"aws:TagKeys": "false"} means the key must exist. Together the two tests require that the request carries tags and that every tag key is team or env.
How multiple conditions combine
The combining rules are the part of this chapter most worth memorizing.
- Several values for one key are combined with OR. The test passes if any value matches.
- Several keys under one operator, and several operators in one block, are combined with AND. Every test must pass.
"Condition": {
"StringEquals": {
"aws:PrincipalTag/team": ["reports", "finance"],
"aws:PrincipalTag/env": "prod"
},
"Bool": { "aws:SecureTransport": "true" }
}This is true when the caller's team tag is reports or finance, and the env tag is prod, and the request used TLS.
You cannot repeat the same operator name twice in one block, since JSON objects with duplicate keys are ambiguous. If you need two tests with the same operator, you can use different key names where possible, or split the logic into separate statements. Separate statements are combined with OR in the sense that any one matching Allow can grant access.
Negated operators and missing keys
Negated operators such as StringNotEquals are a frequent source of errors. They behave as expected when the key is present. When the key is absent, a negated operator evaluates to true, since the missing value is "not equal" to anything.
This matters most inside a Deny. Consider a statement meant to deny access to everyone outside an organization:
{
"Sid": "DenyOutsideOrg",
"Effect": "Deny",
"Action": "s3:*",
"Resource": "arn:aws:s3:::example-reports-bucket/*",
"Condition": {
"StringNotEquals": { "aws:PrincipalOrgID": "o-exampleorgid" }
}
}If a caller has no aws:PrincipalOrgID in its context, the condition is true and the deny applies. That is usually the safe result here. The reverse mistake is also common: writing a Deny with StringNotEquals on a key that is sometimes absent for legitimate callers, and locking out service principals or other valid traffic. Always ask what the statement does when the key is missing.
The TLS deny, step by step
The Topic Spine's example denies any request to the bucket that does not use TLS.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyInsecureTransport",
"Effect": "Deny",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::example-reports-bucket",
"arn:aws:s3:::example-reports-bucket/*"
],
"Condition": { "Bool": { "aws:SecureTransport": "false" } }
}
]
}Walk through two requests:
- A request over HTTPS has
aws:SecureTransportset totrue. TheBooltest for"false"is false. The statement does not match, so it neither denies nor allows. Other policies decide the outcome. - A request over plain HTTP has
aws:SecureTransportset tofalse. The test is true, the explicit deny applies, and noAllowanywhere can override it.
The resource list names both the bucket ARN and the object ARN pattern. Bucket-level actions such as s3:ListBucket use the bucket ARN, and object-level actions such as s3:GetObject use the /* form. Listing only one would leave the other unprotected.
Conditions in different policy types
Conditions are available in identity-based policies, resource-based policies, permission boundaries, session policies, and SCPs. The syntax is the same in each. What changes is the role they play.
- In a resource-based policy, conditions often restrict who may use a grant, for example
aws:PrincipalOrgIDto limit a public-looking principal to your organization. - In an identity policy, conditions often tie access to tags, using
aws:PrincipalTagagainstaws:ResourceTag, so one policy serves many teams. - In an SCP or a boundary, conditions are usually guardrails, such as requiring a region or MFA.
Common errors
- Using
StringEqualswith a wildcard and expecting a pattern match. UseStringLike. - Writing
"aws:SecureTransport": falsewith an unquoted boolean in some tools. Quoted strings are the documented form. - Using a service key with an action that does not support it. The key is then absent and the test behaves as described for missing keys.
- Assuming
aws:SourceIpworks for requests from inside a VPC endpoint or from an AWS service acting on your behalf. - Putting a condition on an
Allowwhen you meant to put it on aDeny, or the reverse, and getting the opposite default behavior. - Forgetting
NullalongsideForAllValues.
To check how a condition behaves, run the policy simulator with context keys. The CLI call below supplies a value for the TLS key:
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::111122223333:role/ReportWriter \
--action-names s3:GetObject \
--resource-arns arn:aws:s3:::example-reports-bucket/2026/summary.csv \
--context-entries ContextKeyName=aws:SecureTransport,ContextKeyValues=false,ContextKeyType=booleanThe simulator evaluates policies attached to the principal. It does not read a bucket policy unless you pass it with --resource-policy, so a deny that lives only in a bucket policy may not show up in the output.
Exercise
A statement has "Effect": "Allow" and the condition {"StringEquals": {"aws:PrincipalTag/team": ["reports", "finance"]}, "Bool": {"aws:SecureTransport": "true"}}. A caller tagged team=finance sends a plain HTTP request. Does the statement grant access? What if the statement were a Deny with the same condition?
Show answer
For the Allow, no. The two operators are combined with AND, and the tag test passes (values for one key are ORed) but aws:SecureTransport is false, so the condition fails and the statement grants nothing. The request falls to the implicit deny unless another policy allows it. For the Deny, the condition is also false, so the Deny does not apply. That is the opposite of what you would want for a TLS guardrail. A TLS deny should test "Bool": {"aws:SecureTransport": "false"} so it triggers on insecure requests.
Exercise
A policy uses "ForAllValues:StringEquals": {"aws:TagKeys": ["team"]} to require that requests only use the tag key team. A request carries no tags. Does the condition pass, and how do you fix the policy if you need at least one tag?
Show answer
It passes, because ForAllValues is true when the key has no values. Add "Null": {"aws:TagKeys": "false"} to the same Condition block so the request must contain the key, which means at least one tag.