Sub topic 8
Restricting access with conditions
This chapter assumes you can read an IAM policy's Effect, Action, and Resource elements and know the difference between an Allow and a Deny statement. Conditions add a further test: a statement applies only when the request also matches the conditions you set.
The Condition element
A Condition block sits inside a statement. It holds one or more condition operators. Each operator holds one or more condition keys and the values to compare against.
"Condition": {
"Bool": { "aws:MultiFactorAuthPresent": "true" }
}Here Bool is the operator, aws:MultiFactorAuthPresent is the condition key, and "true" is the value. AWS fills in the key's value from the request context when it evaluates the request. If the test passes, the statement applies. If it fails, AWS treats the statement as if it were not there.
Condition keys
Condition keys come in two groups:
- Global keys start with
aws:. They work across services. Examples areaws:MultiFactorAuthPresent,aws:SourceIp,aws:RequestedRegion, andaws:PrincipalTag/key. - Service-specific keys start with the service prefix, such as
s3:prefixorec2:InstanceType. Each service documents which keys it supports for each action.
How conditions combine
The combining rules cause many of the surprises in practice:
- Several values under one key are joined with OR. The key matches if any value matches.
- Several keys under one operator, and several operators in one block, are joined with AND. All of them must match.
The explicit Deny and BoolIfExists example
This policy blocks stopping instances unless the caller signed in with MFA.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyStopWithoutMFA",
"Effect": "Deny",
"Action": "ec2:StopInstances",
"Resource": "*",
"Condition": {
"BoolIfExists": { "aws:MultiFactorAuthPresent": "false" }
}
}
]
}Long-term access keys do not carry MFA information, so aws:MultiFactorAuthPresent is missing from those requests. A plain Bool test against a missing key does not match, so the Deny would be skipped and those callers would get through. BoolIfExists treats a missing key as a match, so the Deny applies to them as well. An explicit Deny overrides any Allow.
Common operators
| Operator | Tests | Example key |
|---|---|---|
| `StringEquals` | Exact, case-sensitive string match | `aws:RequestedRegion` |
| `StringLike` | String match with `*` and `?` wildcards | `s3:prefix` |
| `IpAddress` | Request IP falls inside a CIDR range | `aws:SourceIp` |
| `Bool` | Value is true or false | `aws:SecureTransport` |
| `DateGreaterThan` | Request time is after a date | `aws:CurrentTime` |
Adding ForAnyValue: or ForAllValues: in front of an operator lets you test keys that hold several values, such as tag lists.
Common errors
- Using
aws:SourceIpfor requests that come through another AWS service. The service's IP address is what AWS sees, not the user's. - Using a key that the action does not support. The condition then never matches, and an Allow with such a condition grants nothing.
- Expecting
StringEqualsto ignore case. It does not. UseStringEqualsIgnoreCase.
Exercise
A Deny statement uses "Bool": { "aws:MultiFactorAuthPresent": "false" }. A script that uses long-term access keys runs the denied action, and the Deny does not block it. Why, and what is the fix?
Show answer
Requests signed with long-term access keys carry no MFA information, so the key aws:MultiFactorAuthPresent is missing. A plain Bool test on a missing key does not match, so the Deny is skipped. Change the operator to BoolIfExists, which also matches when the key is missing.
Exercise
A condition block has "StringEquals": { "aws:RequestedRegion": ["us-east-1", "eu-west-1"] } and "Bool": { "aws:SecureTransport": "true" }. Which requests match?
Show answer
Requests sent to us-east-1 or eu-west-1 (the two values are OR) that also use HTTPS (the two operators are AND).