IAM policy example: use a KMS key only through S3, with a least privilege rewrite
Facts read 16 Nov 2026. Page reviewed 16 Nov 2026.
Allows a role to use one KMS key only when S3 makes the call on the role's behalf, using the kms:ViaService condition key. Direct calls from the CLI or an SDK are not allowed. The original has the condition but allows creating grants for any service.
Open the workbench ยท All examples
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ViaS3",
"Effect": "Allow",
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:GenerateDataKey*",
"kms:CreateGrant"
],
"Resource": "arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
"Condition": {
"StringEquals": {
"kms:ViaService": "s3.us-east-1.amazonaws.com"
}
}
}
]
}
What it allows
- Allows encrypt, decrypt, data key generation and grant creation on one key when the request comes through S3 in us-east-1.
What it allows
Allow 7 actions (kms:Encrypt, kms:Decrypt, kms:GenerateDataKey*, kms:CreateGrant) on arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab when kms:ViaService StringEquals s3.us-east-1.amazonaws.com.
Action or pattern Actions Access levels kms:Encrypt1 Write 1 kms:Decrypt1 Write 1 kms:GenerateDataKey*4 Write 4 kms:CreateGrant1 Permissions management 1
Checks
No flags from the six checks.
CreateGrant lets the role hand key permissions to other principals, with no limit on who receives them.
Least privilege rewrite
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ViaS3",
"Effect": "Allow",
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:GenerateDataKey*"
],
"Resource": "arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
"Condition": {
"StringEquals": {
"kms:ViaService": "s3.us-east-1.amazonaws.com"
}
}
},
{
"Sid": "GrantForAwsResources",
"Effect": "Allow",
"Action": "kms:CreateGrant",
"Resource": "arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
"Condition": {
"Bool": {
"kms:GrantIsForAWSResource": "true"
}
}
}
]
}
The rewrite allows CreateGrant only when the grant is for an AWS resource, using kms:GrantIsForAWSResource. Grants for people or applications are blocked.
What it allows
Allow 6 actions (kms:Encrypt, kms:Decrypt, kms:GenerateDataKey*) on arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab when kms:ViaService StringEquals s3.us-east-1.amazonaws.com.
Action or pattern Actions Access levels kms:Encrypt1 Write 1 kms:Decrypt1 Write 1 kms:GenerateDataKey*4 Write 4 Allow 1 action (kms:CreateGrant) on arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab when kms:GrantIsForAWSResource Bool true.
Action or pattern Actions Access levels kms:CreateGrant1 Permissions management 1
Checks
No flags from the six checks.
Related pages
Sources
- AWS KMS Developer Guide: default key policy, read 16 Nov 2026
- AWS KMS Developer Guide: key policies, read 16 Nov 2026
- AWS Service Authorization Reference, programmatic access (fetched 16 Nov 2026), read 16 Nov 2026
Last reviewed 16 Nov 2026.