# 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.

```json
{
  "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.

- Allows 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.

Checks:
- No flags.

CreateGrant lets the role hand key permissions to other principals, with no limit on who receives them.

## Least privilege rewrite

```json
{
  "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.

- Allows 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.
- Allows 1 action (kms:CreateGrant) on arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab when kms:GrantIsForAWSResource Bool true.

Checks:
- No flags.

- [Paste it into the workbench](/)
- [Every KMS action and its access level](/actions/kms/)

## Related pages

- [IAM policy example: administer KMS keys without using them](/examples/kms-key-administrator-without-use/)
- [IAM policy example: read one Secrets Manager secret](/examples/secretsmanager-read-one-secret/)
- [IAM policy example: read Secrets Manager secrets by tag](/examples/secretsmanager-read-secrets-by-tag/)
- [All of them](/examples/)

## Sources

- AWS KMS Developer Guide: default key policy: https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html (read 16 Nov 2026)
- AWS KMS Developer Guide: key policies: https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-overview.html (read 16 Nov 2026)
- AWS Service Authorization Reference, programmatic access (fetched 16 Nov 2026): https://docs.aws.amazon.com/service-authorization/latest/reference/service-reference.html (read 16 Nov 2026)

Last reviewed 16 Nov 2026. Canonical: https://iampolicygenerator.com/examples/kms-use-key-through-s3-only/

This site is not affiliated with Amazon Web Services. AWS service and product names are used only to describe AWS services.
