← Back to the blog

AWS & n8n • 2026-08-06 • 3 min read

Envelope Encryption with AWS KMS, Explained by the Code That Uses It

KMS will not encrypt your 5 MB document, and that limit is a hint rather than an obstacle. Envelope encryption in practice, including the parameter most teams leave empty.

Envelope Encryption with AWS KMS, Explained by the Code That Uses It — AWS & n8n

KMS refuses payloads over 4 KB. People meet that limit, assume KMS is the wrong tool, and reach for a key in an environment variable. The limit is a nudge towards the pattern you wanted anyway.

The pattern

  1. Ask KMS for a data key. It returns the same key twice: in plaintext, and encrypted under your master key
  2. Encrypt your data locally with the plaintext key, using AES-GCM
  3. Store the ciphertext next to the encrypted data key
  4. Zero the plaintext key from memory

To decrypt, send the encrypted data key to KMS, get the plaintext back, decrypt locally, zero it again.

The master key never leaves KMS. Your application never stores a usable key. The number of KMS calls stays proportional to the number of objects, not to their size.

// rust
let dek = kms.generate_data_key()
    .key_id(MASTER_KEY_ARN)
    .key_spec(DataKeySpec::Aes256)
    .encryption_context("tenant", tenant_id)
    .send()
    .await?;

let cipher = Aes256Gcm::new_from_slice(dek.plaintext().unwrap().as_ref())?;
let sealed = cipher.encrypt(&nonce, document.as_ref())?;

store(Record {
    ciphertext: sealed,
    nonce: nonce.to_vec(),
    wrapped_key: dek.ciphertext_blob().unwrap().as_ref().to_vec(),
})?;

Encryption context is not optional

The parameter most examples leave out is the one that does the most work. The encryption context is authenticated additional data: decryption only succeeds if the same context is supplied.

Put the tenant id in it, and a data key wrapped for tenant A cannot be unwrapped while serving tenant B — even by code holding the right IAM permissions, even if the wrapped key was swapped in the database. It turns a logic bug into a failed decryption instead of a cross-tenant data leak.

It also appears in CloudTrail, so your audit log says *which tenant's* data was decrypted, not merely that a decryption occurred.

IAM conditions

Grant kms:Decrypt with a condition on the encryption context, so a service may only decrypt within its own scope:

// json
{
  "Effect": "Allow",
  "Action": "kms:Decrypt",
  "Resource": "arn:aws:kms:eu-central-1:123456789012:key/...",
  "Condition": {
    "StringEquals": { "kms:EncryptionContext:service": "documents" }
  }
}

A role that can decrypt anything is the thing an attacker is looking for once they are inside.

Rotation

Rotating the master key does not re-encrypt your data, and does not need to: old data keys stay unwrappable under the previous key material, which KMS retains. New data keys use the new material.

Re-wrapping is only needed when you are moving to a *different* master key — a region change, a separation of tenants. That is a batch job over the wrapped keys, and it never touches the ciphertext, which is the point of the whole arrangement.

Caching

A KMS call per request is latency and cost. Cache plaintext data keys in memory with a short TTL and a cap on how many operations one key may perform. The AWS Encryption SDK implements this; if you roll your own, be deliberate about the lifetime — a cached key is key material in process memory, which is exactly what you were avoiding.

Read next

COOKIES // MEASUREMENT

We count page views through Google Tag Manager. Say yes and it may store a cookie in your browser; say no and it still counts the visit, without storing anything and without an identifier. Nothing here is used for advertising.

Cookie Policy — you can change this at any time from the footer of any page.