Sub topic 11
Rotating keys
This chapter assumes you can create a KMS key and alias, call Encrypt, Decrypt and GenerateDataKey, and read a key policy. It covers what rotation changes, what it leaves alone, and how to rotate when automatic rotation is not available.
What rotation changes
A KMS key is a logical object with an ID, an ARN, a policy and an alias. The cryptographic key material that does the encrypting sits behind it. Rotation replaces the material and keeps everything else.
After a rotation:
- The key ID, ARN, aliases, key policy, grants and tags stay the same.
- New
EncryptandGenerateDataKeycalls use the newest material. Decryptuses the material that produced the ciphertext. The ciphertext records which version that was.- KMS keeps every older version of the material until the key is deleted.
Applications therefore need no change, and nothing already encrypted becomes unreadable.
Which keys rotate automatically
Support depends on who owns the key and how its material was made.
| Key | Rotation |
|---|---|
| Customer managed, symmetric, KMS-generated material | Optional automatic rotation, plus on-demand rotation |
| AWS managed key | Rotated by AWS about once a year. You cannot change this. |
| AWS owned key | Managed entirely by the service. You see nothing. |
| Asymmetric or HMAC key | No automatic rotation. Rotate manually. |
| Key with imported material or in a custom key store | No automatic rotation. Rotate manually. |
Imported key material is an area where KMS capabilities have been changing. Check the current documentation before you rely on this row.
Multi-Region keys have a shared-material rule: you rotate the primary key, and KMS carries the new material to its replicas. The Multi-Region chapter covers this.
Turning on automatic rotation
Automatic rotation is off by default for customer managed keys. The default period is 365 days. You can choose a period from 90 to 2560 days when you enable it.
aws kms enable-key-rotation \
--key-id alias/tutorial-key \
--rotation-period-in-days 180
aws kms get-key-rotation-status --key-id alias/tutorial-keyThe status output includes KeyRotationEnabled, RotationPeriodInDays and NextRotationDate. If a rotation has been done on demand, it also includes OnDemandRotationStartDate.
Two behaviors are easy to miss:
- KMS does not rotate a key while it is disabled or pending deletion. Rotation resumes if you re-enable the key.
- Rotation can add a monthly charge for the key. Check the KMS pricing page for the current figures.
Rotating on demand
Sometimes you cannot wait for the schedule. You may suspect exposure of the material, or you may be testing your process. Rotate immediately like this:
aws kms rotate-key-on-demand --key-id alias/tutorial-key
aws kms list-key-rotations --key-id alias/tutorial-keylist-key-rotations returns one entry per rotation, with the date and the type (AUTOMATIC or ON_DEMAND). Each key allows a limited number of on-demand rotations, currently 10. An on-demand rotation does not reset the automatic schedule. The key also has to be enabled and must not be pending deletion.
On-demand rotation requires the kms:RotateKeyOnDemand permission. Automatic rotation needs kms:EnableKeyRotation. Neither is covered by the Encrypt, Decrypt and GenerateDataKey permissions in the earlier example policy.
What rotation does not do
Rotation does not re-encrypt your data. This is the most common misunderstanding, and it has three consequences.
- Existing ciphertext stays under old material. An S3 object encrypted last year is still protected by version 1. If version 1 were compromised, that object would be exposed whether or not you rotated since.
- Envelope-encrypted data keys stay wrapped under old material. The
encrypted_keyyou stored next to your ciphertext still decrypts through the old version. - A leaked plaintext data key is not fixed. Anyone who copied a data key can decrypt the data it protects, regardless of what KMS does afterward.
Rotation limits how much new data sits under any one version of the material. It also meets compliance requirements that say keys must change periodically. It is not an incident response tool for data already written.
Moving existing data keys to new material
For envelope encryption, you do not have to touch the bulk data. Re-wrap each stored data key with ReEncrypt. KMS decrypts the data key with the old material and encrypts it again with the current material, inside the service. The plaintext key never reaches your code.
import boto3
kms = boto3.client("kms")
CONTEXT = {"purpose": "demo"}
def rewrap(blob: dict) -> dict:
resp = kms.re_encrypt(
CiphertextBlob=blob["encrypted_key"],
SourceEncryptionContext=CONTEXT,
DestinationKeyId="alias/tutorial-key",
DestinationEncryptionContext=CONTEXT,
)
return {**blob, "encrypted_key": resp["CiphertextBlob"]}The caller needs kms:ReEncryptFrom on the source key and kms:ReEncryptTo on the destination key. Here both are the same key, so one policy statement can grant both. The encryption context must match on the source side, or the call fails. After re-wrapping, the data key itself is unchanged, so the bulk ciphertext needs no rewrite.
To move bulk data itself, you must decrypt it and encrypt it again with a new data key. That is practical for small stores and expensive for large ones, so decide whether you need it.
Rotating manually
Manual rotation means creating a new KMS key and moving your workloads to it. You need it for asymmetric, HMAC, imported and custom key store keys. You also need it if you want a new key ID for a policy or ownership reason.
The sequence is:
- Create the new key with the same type, policy and tags.
- Recreate any grants and IAM permissions that reference the old key ARN.
- Repoint the alias to the new key.
- Re-encrypt data that must move.
- Keep the old key enabled until nothing needs it, then disable it and schedule deletion.
NEW_KEY_ID=$(aws kms create-key \
--description "Tutorial key, generation 2" \
--query KeyMetadata.KeyId \
--output text)
aws kms update-alias \
--alias-name alias/tutorial-key \
--target-key-id "$NEW_KEY_ID"update-alias changes what the name resolves to. It does nothing to ciphertext. New encryptions through the alias use the new key. Old ciphertext still needs the old key. For symmetric keys, Decrypt finds the old key from the ciphertext, so the old key must stay enabled and your callers must still be allowed to use it. This is why manual rotation breaks more often than automatic rotation.
Compare the two approaches:
| Property | Automatic or on-demand | Manual |
|---|---|---|
| Key ID and ARN | Unchanged | New |
| Policies and grants | Unchanged | Must be recreated |
| Alias change | Not needed | Needed |
| Old ciphertext | Still decrypts | Needs the old key retained |
| Works for asymmetric and HMAC | No | Yes |
Checking that rotation happened
Rotation is recorded in CloudTrail. KMS writes a service event named KeyRotation when it rotates the material, for both automatic and on-demand rotation. You can also check the list-key-rotations output on a schedule. A simple operational check is to alert on any customer managed key whose get-key-rotation-status shows KeyRotationEnabled as false, unless it is in the manual-rotation table above.
Exercise
A team enabled automatic rotation on a symmetric key a year ago. They now find that an S3 object written before the rotation still decrypts fine, and they ask whether it is now protected by the new key material. What do you tell them, and what would you do if they want it to be?
Show answer
The object still decrypts because KMS keeps old material and picks the right version from the ciphertext, but the object remains protected by the old material. Rotation does not rewrite existing data. To move it to current material, the object must be re-encrypted, for example by copying it over itself so S3 encrypts it again with the key. For envelope-encrypted data you own, call ReEncrypt on the stored data keys. This needs kms:ReEncryptFrom and kms:ReEncryptTo.
Exercise
You use an asymmetric signing key. Can you enable automatic rotation? What do you do instead to move to a new key without breaking callers that use the alias?
Show answer
No, asymmetric keys do not support automatic rotation. Create a new key of the same type, replicate the policy and permissions, and repoint the alias with update-alias. Keep the old key enabled long enough to verify signatures it already made, because the old public key is still needed for that.