Lesson 8 of 12 · 15m
KMS Is an API, Not a Store
KMS never exports keys and caps Encrypt at about 4KB — the envelope pattern does the real work, and the secret stores are different services entirely.
aws kms encrypt --key-id alias/app --plaintext fileb://secret.txt returns ciphertext — and fails if the input exceeds 4096 bytes. That limit is the clue to what KMS actually is: a cryptographic API backed by HSMs whose keys never leave the hardware, not a database for your passwords. The access control is IAM plus the key policy; every Encrypt/Decrypt call lands in CloudTrail with the principal, IP, and key ARN. You are not storing secrets in KMS. You are paying per call to encrypt small things.
Envelope encryption
The 4KB limit resolves through GenerateDataKey: KMS mints a random 256-bit data key and returns it twice — plaintext and ciphertext wrapped under your KMS key. Your app encrypts the actual payload locally with the plaintext data key (AES-GCM via your crypto library), stores encrypted_data_key + ciphertext together, and forgets the plaintext data key. To read, send the encrypted data key to kms:Decrypt; if IAM allows, you get the plaintext data key back and decrypt locally. The KMS key never moves; the data keys are disposable; the expensive HSM call happens once per object, not once per byte. This is how Secrets Manager encrypts its payloads, how S3 SSE-KMS works, and how sops wraps data keys for kms: recipients — same envelope, everywhere.
The three AWS services people conflate
- KMS: encrypts keys and small blobs. ~$1/key/month plus per-request pricing. No secret storage, no rotation of your data — it rotates the key material under the same ARN if you opt in annually.
- Secrets Manager: stores secret values with versioning (
AWSCURRENT/AWSPENDINGstaging labels) and managed rotation — built-in Lambda-based rotation for RDS, Redshift, and DocumentDB, or custom Lambdas. $0.40/secret/month plus $0.05 per 10k API calls. - SSM Parameter Store: free standard tier up to 4KB values,
SecureStringtype encrypted under KMS. No managed rotation — you write it. Advanced tier lifts the size cap for a per-parameter price.
GCP Secret Manager and Azure Key Vault split the same way: a secret store service backed by a key-management service.
Two access mechanics matter before you ship anything. The key policy lives on the key itself and is the mandatory first gate — IAM grants mean nothing if the key policy doesn't name the account or principal. Grants are the per-caller delegation primitive: EBS, RDS, and your own app code can hold a grant that permits Decrypt on a specific key under constraints (e.g. only via a specific grantee principal, only with encryption context matching). Encryption context — an authenticated key-value map logged in CloudTrail and bound to the ciphertext — is the underused control: kms:EncryptionContext:service=payments in a condition means a valid role decrypting for the wrong service gets denied and logged.
The wrong mental model
"Send the secret to KMS." KMS will encrypt up to 4KB for you, but then you own ciphertext storage, rotation, and access flow yourself — the parts Secrets Manager actually implements. The inverse mistake is just as common: paying $0.40/month per secret for what Parameter Store does free — storing rarely-changing values that don't need managed rotation.
Concrete practice
- Envelope encrypt large data at rest;
Encrypt/Decryptdirectly only for values under the limit. - Secrets Manager when managed DB rotation is worth $0.40/mo; Parameter Store for config-shaped secrets; KMS only as the encryption layer.
- Scope access with the key policy plus IAM — a key policy naming
kms:Decryptto one role is stronger than a broad IAM statement. - Alarm in CloudTrail on
DecryptandGetSecretValueby principals or IPs outside your baseline — a valid role used from an unfamiliar ASN is your early warning.
KMS holds the wrapping keys. Put the secrets where secrets live, and let the audit log watch the unwrap.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
RSA Is Simple. Deploying RSA Correctly Almost Never Is.
The capstone to our RSA series. The math checks out. Shipping it as written gets you breached. This is the nineteen-year-old attack that proves it, and what belongs in production instead.
RSA in JavaScript Part 2: The JWT Is Where You'll Actually Meet It
Most JavaScript developers never call an RSA function directly. They verify an RS256 token, and two classic attacks turn that into an authentication bypass — both demonstrated here, both fixed by one line.
Implementing RSA, AES-GCM, and a TLS 1.3 Handshake from Scratch in Python
What is Encryption? The Math That Keeps Your Data Private
Encryption transforms readable data into unreadable ciphertext that only someone with the right key can reverse. It's what makes HTTPS, messaging apps, banking, and password storage work. Here's how it actually functions.