Sub topic 12
Disabling and deleting keys safely
This chapter assumes you can create a customer managed KMS key, call Encrypt and Decrypt, and read a key policy. It covers what happens to your data when a key stops being usable, and how to retire a key without losing anything by accident.
Why deletion needs care
Deleting a KMS key is permanent. Every ciphertext produced under that key, and every data key it encrypted, becomes unreadable. AWS cannot recover the key material once it is gone. For this reason KMS never deletes a key immediately. You first disable it, then schedule deletion with a waiting period.
Key states
A key is always in one state. The state decides which operations succeed.
| State | Can encrypt or decrypt | How you get there |
|---|---|---|
| Enabled | Yes | Default after creation |
| Disabled | No | `disable-key` |
| Pending deletion | No | `schedule-key-deletion` |
Disabling is reversible with enable-key. A key that is pending deletion can be restored with cancel-key-deletion. Note that cancelling returns the key in the Disabled state, so you must enable it again.
Disable first, then schedule deletion
The safest retirement sequence has three steps.
- Disable the key. Anything still depending on it starts failing with a
DisabledException. - Watch for failures. Check application errors and CloudTrail for denied or failed calls against the key.
- If nothing breaks for a period you trust, schedule deletion.
aws kms disable-key --key-id <key-id>
# Roll back if something breaks
aws kms enable-key --key-id <key-id>
# Delete only after you are sure; the window is 7 to 30 days
aws kms schedule-key-deletion --key-id <key-id> --pending-window-in-days 30
# Change your mind during the window
aws kms cancel-key-deletion --key-id <key-id>
aws kms enable-key --key-id <key-id>The default waiting period is 30 days. The minimum is 7. Use the longest window you can tolerate, because the window is your only chance to find a forgotten dependency. A monthly job or a rarely restored backup may not use the key until after a short window has passed.
What can go wrong
- Hidden dependencies. S3 objects, EBS volumes, RDS snapshots, and Secrets Manager secrets can all be encrypted under the key. Data that is rarely read will not fail until someone reads it.
- Aliases hide the target. Deleting a key does not delete the alias pointing at it. The alias remains, but calls through it fail.
- Envelope-encrypted data. Stored data keys cannot be decrypted without the parent key, so the local ciphertext is lost too.
- Cached data keys. An application that cached a plaintext data key may keep working for a while, which can mask the problem.
Guarding against accidental deletion
You can limit who can retire keys with the key policy. The statement below denies ScheduleKeyDeletion and DisableKey to everyone except one administrator role.
{
"Sid": "RestrictRetirement",
"Effect": "Deny",
"Principal": { "AWS": "*" },
"Action": ["kms:ScheduleKeyDeletion", "kms:DisableKey"],
"Resource": "*",
"Condition": {
"StringNotEquals": {
"aws:PrincipalArn": "arn:aws:iam::111122223333:role/key-admin"
}
}
}Test any deny statement before relying on it, so you do not lock out your own administrators. You can also create a CloudWatch alarm on CloudTrail events for DisableKey and ScheduleKeyDeletion, so that a retirement attempt is noticed quickly.
Exercise
You disabled a key on Monday and nothing failed all week, so you scheduled deletion with a 7-day window. Why is this still risky, and what should you change?
Show answer
A quarterly or yearly process, such as a backup restore, may not touch the key within a week, so the failure would only appear after the key is gone. Use a longer window (up to 30 days), check CloudTrail for any use of the key, and cancel the deletion if a dependency appears.
Exercise
After running cancel-key-deletion, Encrypt calls still fail. What is the cause and the fix?
Show answer
Cancelling deletion returns the key to the Disabled state, not Enabled. Run aws kms enable-key --key-id <key-id>.