Restrict an IAM user to a folder
Choose a pattern
Pattern
Use when
Pattern A - Hardcoded prefix
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowListProjectA",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-bucket",
"Condition": {
"StringLike": { "s3:prefix": "team-data/projectA/*" }
}
},
{
"Sid": "AllowReadWriteProjectA",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::my-bucket/team-data/projectA/*"
}
]
}Attach the policy via AWS CLI
Test the policy
Pattern B - Per-user folder via ${aws:username}
Test from each user
Combining with an IP restriction
Block a sub-folder with explicit Deny
Gotchas
The trailing slash matters
The user must always send --prefix
Empty prefix is treated as absent
Nested keys are visible
Usernames are emails
Common mistakes
Symptom
Cause
Fix
See also
Last updated
Was this helpful?