Sub topic 10
Encrypting data in AWS services with KMS
This chapter assumes you can already create a customer managed key, read a key policy, and explain envelope encryption. The earlier chapters on those topics cover the background. Here you will see how AWS services use KMS for you, how to turn it on for S3 and EBS, and how to fix the permission errors that come with it.
What the service does for you
Most AWS services that encrypt data at rest do not send your data to KMS. They use envelope encryption, as in the Python example earlier in this tutorial. The service asks KMS for a data key, encrypts your data locally with that key, and stores the encrypted copy of the data key next to the data. To read the data, the service asks KMS to decrypt the data key.
Your application never handles the data key. What you control is which KMS key the service uses, and who is allowed to use that key through the service.
Two ways a service gets permission
A service reaches KMS in one of two ways, and the difference explains most access problems.
- On behalf of the caller. S3 works this way. The service calls KMS with the identity of the user or role that made the request. That identity needs
kms:GenerateDataKeyto upload andkms:Decryptto download, in addition to its S3 permissions. - Through a grant. EBS and RDS work this way. The first time you use a key, the service creates a grant, which is a temporary permission attached to the key. The grant lets the service use the key later without your identity in the request. Creating it requires
kms:CreateGrantfrom you.
Both paths are checked against the key policy, and IAM policies count only if the key policy allows them (the policy chapters cover this).
Which key the service uses
Each service can use any of the three key ownership types.
| Option | Who controls it | Visible in your account | Typical use |
|---|---|---|---|
| AWS owned key | AWS | No | Default for some services, such as DynamoDB tables |
| AWS managed key (`aws/s3`, `aws/ebs`) | AWS creates it, you can only view it | Yes | Encryption with no key management work |
| Customer managed key | You | Yes | Custom policy, cross-account access, audit, rotation and deletion control |
AWS managed keys have a key policy you cannot edit. They also cannot be shared across accounts. If another account must read your encrypted data, use a customer managed key.
Example: S3 default encryption with SSE-KMS
SSE-KMS is the S3 setting for server-side encryption with a KMS key. Set a bucket default so every new object uses your key. This example uses the key ARN, which is the safest way to identify a key to S3.
aws s3api put-bucket-encryption \
--bucket my-tutorial-bucket \
--server-side-encryption-configuration '{
"Rules": [
{
"ApplyServerSideEncryptionByDefault": {
"SSEAlgorithm": "aws:kms",
"KMSMasterKeyID": "<key-arn>"
},
"BucketKeyEnabled": true
}
]
}'Upload a file and check how it was stored:
echo -n "quarterly numbers" > report.txt
aws s3 cp report.txt s3://my-tutorial-bucket/report.txt \
--sse aws:kms \
--sse-kms-key-id <key-arn>
aws s3api head-object \
--bucket my-tutorial-bucket \
--key report.txt \
--query '[ServerSideEncryption, SSEKMSKeyId]'The output shows aws:kms and the ARN of the key. A bucket default applies only to new objects. Existing objects keep their old encryption until you copy them over themselves.
BucketKeyEnabled makes S3 reuse a bucket-level key to cut the number of KMS requests. The chapter on quotas and cost explains it.
S3 supplies its own encryption context. With Bucket Keys enabled it is the bucket ARN. Without them it is the object ARN. You will see it as aws:s3:arn in CloudTrail.
Example: EBS encryption by default
EBS (Elastic Block Store) volumes can be encrypted at creation. Two account settings, both per Region, make this automatic:
aws ec2 enable-ebs-encryption-by-default --region us-east-1
aws ec2 modify-ebs-default-kms-key-id \
--region us-east-1 \
--kms-key-id <key-arn>
aws ec2 create-volume \
--region us-east-1 \
--availability-zone us-east-1a \
--size 8 \
--volume-type gp3The new volume is encrypted with your key even though the command has no --encrypted option. You cannot encrypt an existing volume in place. You snapshot it, copy the snapshot with encryption on, and create a new volume from the copy. RDS databases follow the same rule.
Letting only a service use the key
Sometimes you want a role to use a key only through S3, so the role cannot call kms:Decrypt directly in a script. Use the kms:ViaService condition key in the key policy:
{
"Sid": "AllowAppToUseKeyViaS3Only",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/app-role" },
"Action": ["kms:GenerateDataKey", "kms:Decrypt"],
"Resource": "*",
"Condition": {
"StringEquals": {
"kms:ViaService": "s3.us-east-1.amazonaws.com",
"kms:CallerAccount": "111122223333"
}
}
}kms:ViaService limits the statement to requests that S3 in that Region makes for the role. kms:CallerAccount limits it to your own account. For EBS, the equivalent pattern lets the role create grants only when kms:GrantIsForAWSResource is true.
Common errors
AccessDeniedon upload. The caller lackskms:GenerateDataKeyon the bucket's key. Add it in the key policy or in an IAM policy, provided the key policy delegates to IAM.AccessDeniedon download. The caller lackskms:Decrypt. This is common with cross-account readers, who need permission in both the key policy and their own IAM policy.KMS.DisabledExceptionor a key in the wrong state. The key is disabled or pending deletion. Every object, volume and snapshot encrypted under it becomes unreadable until the key is enabled again.Client.InvalidKMSKey.InvalidStatewhen launching an EC2 instance. An encrypted volume or AMI uses a key that is disabled or pending deletion. Re-enable the key.- Wrong Region. KMS keys exist in one Region. A bucket or volume in
us-west-2cannot use a single-Region key fromus-east-1. - Surprise bills. Each S3 request on an SSE-KMS object can trigger a KMS call. High request volume without Bucket Keys can approach KMS request quotas.
Design choices
- Customer managed key per data class. Separate keys for logs, customer data and backups let you give each its own policy and deletion decision. The cost is more keys to manage.
- AWS managed key. This is the least work, but you cannot share the data across accounts or tighten the policy.
- One key for everything. This is simple, but disabling or mis-editing that key affects every service at once.
Exercise
A role in your account has full s3:* permissions on a bucket whose default encryption uses a customer managed key. It can list objects but gets AccessDenied on GetObject. What is the likely cause, and how would you fix it so the role can read the objects only through S3?
Show answer
The role lacks kms:Decrypt on the bucket's KMS key. S3 calls KMS as the caller, so the role needs that permission as well as s3:GetObject. Add a key policy statement (or an IAM policy, if the key policy delegates to IAM) that allows kms:Decrypt for the role, with the condition kms:ViaService set to s3.<region>.amazonaws.com so the role cannot call KMS directly.