AWS Builder Center
Envelope Encryption with AWS KMS: Rotate Keys Without Rewriting Every Object

Envelope Encryption with AWS KMS: Rotate Keys Without Rewriting Every Object

Separate data encryption, KMS key rotation, and data-key rewrapping. Build an authenticated envelope and verify the recovery path before retiring a key.

You need to change the key protecting a large collection of encrypted objects. Does that mean reading, decrypting, and rewriting every byte? With an envelope design, often the part that needs to move is much smaller: the encrypted data key beside each object.
Picture a document locked in a box, with the box key stored inside a second locked container. We can change the protection around the small key without opening the large box. The analogy stops at authorization: whoever can unwrap that key and read the ciphertext may still decrypt the document. The extra layer is a useful boundary, not automatic tenant isolation.
I'd make the stored envelope and its recovery procedure explicit before optimizing KMS call counts. Losing the wrapped key is a data-loss event, even when the ciphertext is perfectly intact.

Build the envelope around authenticated encryption

A data encryption key (DEK) encrypts the object locally. A KMS key protects that DEK.  GenerateDataKey  returns both plaintext key bytes and a KMS-encrypted copy. Store the encrypted copy with the encrypted object; keep plaintext key exposure as short as practical.
The following Python example is for small in-memory objects. It uses a fresh data key and AES-GCM nonce for every encryption. Production file formats should use a maintained encryption library, such as the AWS Encryption SDK, rather than extend this small format into a home-grown streaming protocol.
import json
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

def context_bytes(context):
return json.dumps(context, sort_keys=True, separators=(',', ':')).encode()

def encrypt_object(kms, key_arn, plaintext, context):
response = kms.generate_data_key(
KeyId=key_arn, KeySpec='AES_256', EncryptionContext=context
)
dek = response['Plaintext']
nonce = os.urandom(12)
ciphertext = AESGCM(dek).encrypt(nonce, plaintext, context_bytes(context))
del dek
del response['Plaintext']
return {
'version': 1,
'nonce': nonce,
'ciphertext': ciphertext,
'wrapped_key': response['CiphertextBlob'],
}

def decrypt_object(kms, key_arn, envelope, expected_context):
if envelope['version'] != 1:
raise ValueError('unsupported envelope version')
response = kms.decrypt(
KeyId=key_arn,
CiphertextBlob=envelope['wrapped_key'],
EncryptionContext=expected_context,
)
dek = response['Plaintext']
try:
return AESGCM(dek).decrypt(
envelope['nonce'], envelope['ciphertext'],
context_bytes(expected_context),
)
finally:
del dek
del response['Plaintext']
Pass the key ARN from trusted configuration and reconstruct expected context from the authorized tenant and object identity. For example, use non-secret opaque identifiers under  tenant_id  and  object_id . Do not trust an envelope's own claim that it belongs to the requesting tenant. Define binary serialization and size limits at the storage boundary.
 KMS encryption context  is authenticated, case-sensitive context, and appears in logs. It must not contain secrets. The local AEAD associated data binds the same expected context to the object ciphertext.  AES-GCM  authenticates ciphertext and associated data; a nonce must not repeat under the same key.
The  del  statements release references. They don't promise that an immutable Python byte string, allocator copy, or library buffer has been securely zeroed. Process isolation and avoiding logs or unnecessary copies still matter. I'd use an implementation with appropriate key-memory controls when the threat model requires stronger guarantees.

Separate three different changes

KMS key-material rotation changes the cryptographic material behind the same logical KMS key. AWS retains the material needed to decrypt older ciphertexts.  Automatic or on-demand rotation  does not rewrite stored application ciphertext or every wrapped data key.
Rewrapping under a different KMS key changes which logical KMS key protects a DEK. For a blob produced by KMS, use  ReEncrypt  with the correct source and destination context. The plaintext DEK remains inside KMS during that operation. This doesn't work by sending an entire AWS Encryption SDK message to  ReEncrypt ; its container format is different.
Changing the DEK or data cipher requires decrypting and reencrypting the object itself. Rewrapping an exposed DEK doesn't make that DEK secret again. If an attacker already copied the plaintext DEK, changing its outer protection cannot undo that disclosure.

Migrate the small object without losing the large one

For the custom envelope above, I'd read the envelope and its storage version, rewrap  wrapped_key , and write the new envelope using a conditional version check. Preserve the object ciphertext and nonce. If another writer changed the object meanwhile, discard the stale migration result and reread; do not combine an old wrapped DEK with new ciphertext.
Keep the source key usable until representative restores and the migration inventory demonstrate that no required envelope or backup depends on it. Changing an alias only affects how future calls resolve that alias. It doesn't migrate old encrypted data keys. Cryptographic deletion also needs a complete inventory of key copies, backups, cached plaintext, and alternate wrapped copies; it isn't an automatic compliance guarantee.
The local example was exercised with cryptography 50.0.2 and a simulated KMS adapter: successful round-trip, modified ciphertext, and mismatched context. That validates the local envelope behavior only. Real KMS permissions, CloudTrail records, rewrapping, storage compare-and-swap, and restore behavior still require account-level integration checks.

Conclusion

I'd design key changes as migrations with observable states and a tested restore path. Envelope encryption makes some migrations smaller because the key wrapper can move independently of the data. The decision to retire an old key should follow successful recovery evidence, not the completion of a rotation checkbox.
Adapted and substantially updated from  my original article . Technical references reviewed November 8, 2026. Prepared with AI assistance; examples and validation limits are identified in the text.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article