Sub topic 8
Restricting access with conditions
This chapter assumes you can read an IAM policy document (Version, Statement, Effect, Action, Resource) and know the difference between an Allow and a Deny. A policy without a condition applies every time the action and resource match. A Condition element narrows that. The statement takes effect only when the request context satisfies the condition.
The working model
When a principal sends a request, AWS builds a request context. This is a set of key-value pairs describing the request: who is calling, from which IP address, at what time, whether MFA was used, which tags are attached, and so on. Each key is a condition key. A Condition block compares those keys against values you write in the policy.
A condition has three parts:
- Condition operator, such as
StringEquals,IpAddress, orBool. It defines how to compare. - Condition key, such as
aws:SourceIporaws:MultiFactorAuthPresent. - Condition value, the value or values you compare against.
"Condition": {
"StringEquals": {
"aws:RequestedRegion": ["eu-west-1", "eu-central-1"]
}
}If the condition evaluates to true, the statement applies. If it evaluates to false, AWS treats the statement as if it did not match, and the request falls through to other statements.
How multiple conditions combine
The combination rules cause many mistakes:
- Several values for one key are joined with OR. In the example above, the region may be either one.
- Several keys under one operator are joined with AND.
- Several operators in one
Conditionblock are joined with AND.
"Condition": {
"IpAddress": { "aws:SourceIp": "203.0.113.0/24" },
"Bool": { "aws:SecureTransport": "true" }
}This statement matches only when the request comes from that network range and uses TLS.
Common operators
| Operator | Compares | Example use |
|---|---|---|
| `StringEquals`, `StringLike` | Exact text, or text with `*` and `?` wildcards | Match a tag value or a name prefix |
| `NumericLessThan` and related | Numbers | Limit a value such as `s3:max-keys` |
| `DateGreaterThan`, `DateLessThan` | Dates and times | Grant access only until a given date |
| `Bool` | `true` or `false` | Require MFA or TLS |
| `IpAddress`, `NotIpAddress` | CIDR ranges | Allow calls only from a corporate network |
| `ArnLike`, `ArnEquals` | ARNs | Restrict by the calling resource's ARN |
| `Null` | Whether a key is present | Require that a tag be supplied |
Any operator can take a set operator prefix, ForAllValues: or ForAnyValue:, for keys that hold multiple values, such as a list of tag keys in a request. You can also add the suffix IfExists, which is covered below.
Global and service-specific keys
Keys beginning with aws: are global condition keys. They are available across many services. Common ones include:
aws:SourceIp, the caller's public IP address. It does not work well when the request travels through a VPC endpoint or comes from an AWS service acting for you.aws:RequestedRegion, the Region the call targets.aws:PrincipalTag/key, a tag on the calling user or role.aws:ResourceTag/key, a tag on the resource being accessed.aws:MultiFactorAuthPresent, whether the caller authenticated with MFA.aws:PrincipalOrgID, the AWS Organizations ID of the caller.
Keys beginning with a service prefix, such as s3:prefix or ec2:InstanceType, are service-specific keys. They exist only for that service's actions. Each action supports a particular set of keys, so check the Service Authorization Reference before relying on one. If you use a key that the action never populates, the condition can never match as you intended.
Requiring MFA with a deny
The most common pattern is a Deny with a condition. Recall the policy from the Topic Spine:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyStopWithoutMFA",
"Effect": "Deny",
"Action": "ec2:StopInstances",
"Resource": "*",
"Condition": {
"BoolIfExists": { "aws:MultiFactorAuthPresent": "false" }
}
}
]
}An explicit Deny overrides any Allow, so no other policy can undo this restriction. The condition says "when the MFA key is false".
Why `BoolIfExists` and not `Bool`
aws:MultiFactorAuthPresent is absent from the request context when the caller used long-lived access keys, because no MFA-authenticated session exists. If you write "Bool": {"aws:MultiFactorAuthPresent": "false"}, a request with no such key does not match, the Deny does not apply, and the caller gets through without MFA. BoolIfExists treats a missing key as a match, so the Deny covers both "MFA was false" and "MFA information is missing".
The IfExists suffix means: if the key is in the context, evaluate it normally. If the key is missing, treat the condition as true. This is the opposite of the default, where a missing key makes the condition false.
Trust policy conditions
Conditions are not limited to identity-based policies. A trust policy can use them too. The MFA trust policy from the Topic Spine lets principals from account 111122223333 assume a role only when they signed in with MFA:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "sts:AssumeRole",
"Condition": {
"Bool": { "aws:MultiFactorAuthPresent": "true" }
}
}
]
}Here Bool with true is safe, since a missing key makes the condition false and the Allow does not apply. The rule is the reverse of the Deny example. In an Allow, you want a missing key to fail the match. In a Deny, you want a missing key to match.
Tag-based access control
Tags let one policy cover many resources without listing each ARN. This approach is called attribute-based access control (ABAC). A common pattern compares the principal's tag with the resource's tag:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:StartInstances",
"ec2:StopInstances"
],
"Resource": "arn:aws:ec2:*:*:instance/*",
"Condition": {
"StringEquals": {
"aws:ResourceTag/team": "${aws:PrincipalTag/team}"
}
}
}
]
}${aws:PrincipalTag/team} is a policy variable. AWS replaces it with the caller's team tag when it evaluates the request. A user tagged team=payments can start and stop only instances tagged team=payments. Adding a new team needs no policy change.
Be careful about who can change tags. If a user can edit their own team tag, they can give themselves access. Restrict iam:TagUser, iam:TagRole, and ec2:CreateTags accordingly.
Common errors
- Using
BoolwhereBoolIfExistsis needed in a Deny. The Deny silently skips requests that lack the key. - Expecting AND when you wrote two values. Multiple values for one key mean OR.
- Using a key that the action does not support. The condition never matches, and the statement has no effect.
- Relying on
aws:SourceIpfor service-to-service calls. When an AWS service calls on your behalf, the source IP is the service's address, not yours. - Mistyping key names or values. Condition keys are checked for syntax only. A wrong key name does not produce a save-time error.
- Comparing case-sensitively by accident.
StringEqualsis case-sensitive. UseStringEqualsIgnoreCasewhen case may vary.
Exercise
A Deny statement uses "Bool": {"aws:MultiFactorAuthPresent": "false"} on ec2:StopInstances. A script using long-lived access keys, with no MFA session, still stops instances. Why does it succeed, and what is the fix?
Show answer
Long-lived access keys do not produce an MFA session, so aws:MultiFactorAuthPresent is absent from the request context. With plain Bool, a missing key makes the condition false, so the Deny does not match and the call is allowed. Change the operator to BoolIfExists, which treats a missing key as a match, so the Deny applies to requests with MFA false and requests with no MFA information.
Exercise
A condition block contains "StringEquals": {"aws:RequestedRegion": ["eu-west-1", "eu-central-1"]} and "Bool": {"aws:SecureTransport": "true"}. A request targets eu-central-1 over plain HTTP. Does the statement apply? Explain using the combination rules.
Show answer
No. Values for one key combine with OR, so the Region condition is true for eu-central-1. Separate operators combine with AND, and aws:SecureTransport is false for plain HTTP, so the Bool condition fails. Because one operator is false, the whole Condition block is false and the statement does not apply.