Sub topic 14
Request quotas, cost and S3 Bucket Keys
This chapter assumes you have read the chapters on envelope encryption and on using KMS with AWS services. You should know that each S3 object encrypted with SSE-KMS gets its own data key, and that every data key request is a KMS API call.
Why KMS calls add up
KMS charges for keys and for requests, and it limits how many requests you can make per second. Both limits matter most when many small objects or many callers hit the same key.
- Key cost: each customer managed key has a monthly charge. AWS managed keys have no monthly charge.
- Request cost: you pay per 10,000 API requests, after a small monthly free tier. Encrypt, Decrypt and GenerateDataKey all count.
- Request quota: cryptographic operations share a per-second quota for each account and Region. The exact figure depends on the Region and key type, so check the Service Quotas console.
Check the current KMS pricing page for exact prices, because they change.
Quota errors
When you exceed the quota, KMS returns a ThrottlingException. The AWS SDKs retry throttled calls with exponential backoff, so a short burst usually succeeds, only slower. A sustained overload causes failed requests and added latency.
A common cause is a design that calls KMS for every small item. A busy S3 bucket with millions of objects is a typical example.
You can reduce KMS calls in three ways:
- Use envelope encryption, and reuse one data key for many local operations instead of calling KMS each time.
- Cache data keys for a short time, for example with the AWS Encryption SDK's caching.
- Enable S3 Bucket Keys when you use SSE-KMS.
Caching trades safety for speed. A cached key stays in memory longer and encrypts more data, so limit its age and the number of messages it protects.
How S3 Bucket Keys work
Without a Bucket Key, S3 asks KMS for a new data key for every object you upload. It also calls KMS when you read an object. With a Bucket Key, S3 asks KMS once for a short-lived bucket-level key. S3 then derives the per-object data keys itself, without calling KMS again.
AWS states that Bucket Keys can cut KMS request costs by up to 99 percent. The saving is largest for buckets with many objects that share one KMS key.
Enabling a Bucket Key
Set BucketKeyEnabled in the bucket's default encryption configuration:
aws s3api put-bucket-encryption \
--bucket my-example-bucket \
--server-side-encryption-configuration '{
"Rules": [{
"ApplyServerSideEncryptionByDefault": {
"SSEAlgorithm": "aws:kms",
"KMSMasterKeyID": "alias/tutorial-key"
},
"BucketKeyEnabled": true
}]
}'This setting applies to new objects. Existing objects keep their current encryption until you copy them over themselves.
Side effects to know about
- Encryption context changes. With a Bucket Key, the context is the bucket ARN, not the object ARN. A key policy condition on the object ARN will stop matching.
- Fewer CloudTrail events. There are fewer KMS calls, so there are fewer log entries per object. You can no longer trace each object read to its own KMS event.
- Same key policy needed. The caller still needs permission to use the KMS key.
Exercise
A bucket uses SSE-KMS and receives millions of small uploads. Uploads sometimes fail with ThrottlingException. What is the simplest change that reduces KMS calls, and which policy condition might break afterward?
Show answer
Enable S3 Bucket Keys on the bucket. S3 then makes one KMS call to create a bucket-level key and derives object keys itself. Any policy condition on the encryption context that uses the object ARN might break, because the context becomes the bucket ARN.