Sub topic 10
Encrypting data in AWS services with KMS
This chapter assumes you know how to create a customer managed key, read a key policy, and use IAM roles. It also assumes you understand envelope encryption from the previous chapter.
How services use your key
Most AWS services never ask you to call Encrypt or Decrypt. You give the service a KMS key, and the service runs envelope encryption for you. It calls GenerateDataKey to get a data key, encrypts your data with it, and stores the encrypted data key next to the data. When it reads the data back, it calls Decrypt on the stored data key.
The service makes these calls on behalf of a principal, which is a user or role. That principal needs permission on the KMS key as well as on the service. Missing key permissions are the most common cause of failures with encrypted resources.
Example: default encryption on an S3 bucket
The command below sets SSE-KMS (server-side encryption with KMS keys) as the bucket default. Replace the bucket name and key alias with your own.
aws s3api put-bucket-encryption \
--bucket my-tutorial-bucket \
--server-side-encryption-configuration '{
"Rules": [{
"ApplyServerSideEncryptionByDefault": {
"SSEAlgorithm": "aws:kms",
"KMSMasterKeyID": "alias/tutorial-key"
},
"BucketKeyEnabled": true
}]
}'New objects are now encrypted with alias/tutorial-key. Objects uploaded earlier keep their old encryption until you copy them over themselves.
Permissions a caller needs
To work with an encrypted resource, the caller needs two things:
- Permission on the service, such as
s3:GetObject. - Permission on the key, through the key policy, an IAM policy, or a grant.
For S3 with SSE-KMS, uploads need kms:GenerateDataKey and downloads need kms:Decrypt. The key policy from the earlier chapter, which enables IAM policies, lets you grant these in an IAM policy attached to the role.
What differs between services
Services expose the key choice in different places. The details below are typical, but check each service's documentation.
| Service | Where you choose the key | Notes |
|---|---|---|
| Amazon S3 | Bucket default encryption or per-object header | Bucket Keys reduce KMS calls |
| Amazon EBS | Volume or account-level default | The key is fixed when the volume is created |
| Amazon RDS | At database creation | You cannot turn on encryption for an existing unencrypted instance directly. Restore from an encrypted snapshot copy instead. |
| AWS Secrets Manager | Per secret | Defaults to an AWS managed key |
Some services use grants. For example, EBS creates a grant so that EC2 can attach an encrypted volume to an instance without the instance role needing direct key permissions.
Choosing a key for a service
- Use the AWS managed key (such as
aws/s3) for convenience. You cannot edit its policy or share it with other accounts. - Use a customer managed key when you need cross-account access, custom policies, rotation control, or a CloudTrail trail you can attribute to your own key.
Cross-account use requires the key policy to allow the other account and an IAM policy in that account to allow the caller.
Common errors
AccessDeniedExceptiononGenerateDataKey: the caller lacks key permission.KMSInvalidStateException: the key is disabled or pending deletion.- Reads failing after a key change: the stored data key still refers to the original key, so that key must stay enabled.
Exercise
A role can call s3:PutObject on a bucket whose default encryption uses a customer managed key, but uploads fail with AccessDenied. Which KMS action is most likely missing, and why?
Show answer
kms:GenerateDataKey. S3 calls it on behalf of the uploading role to get a data key for the object, so the role needs it in the key policy or an IAM policy.