KMS via SDK Best Practices
A working checklist of KMS habits that keep encryption secure, cheap, and easy to operate at scale.
Busca en todas las páginas de la documentación
A working checklist of KMS habits that keep encryption secure, cheap, and easy to operate at scale.
Each rule is stated positively, with a one-line reason and how to enforce it.
KeyUsage and KeySpec are immutable - a wrong choice means creating a new key and migrating.MultiRegion cannot be added after creation.# --- Python (boto3) ---
import boto3
kms = boto3.client("kms")
key = kms.create_key(KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT")
kms.create_alias(AliasName="alias/app-secrets-key", TargetKeyId=key["KeyMetadata"]["KeyId"])// --- TypeScript (AWS SDK v3) ---
import { KMSClient, CreateKeyCommand, CreateAliasCommand } from "@aws-sdk/client-kms";
const kms = new KMSClient({});
const key = await kms.send(new CreateKeyCommand({ KeySpec: "SYMMETRIC_DEFAULT", KeyUsage: "ENCRYPT_DECRYPT" }));
await kms.send(new CreateAliasCommand({ AliasName: "alias/app-secrets-key", TargetKeyId: key.KeyMetadata!.KeyId! }));{
"note": "Storage record shape for envelope-encrypted data",
"ciphertextBlob": "<base64 blob from GenerateDataKey>",
"nonce": "<base64 12-byte AES-GCM nonce>",
"authTag": "<base64 AES-GCM auth tag>",
"data": "<base64 encrypted payload>"
}["Decrypt"], not a broader set, per grantee.# --- Python (boto3) ---
grant = kms.create_grant(
KeyId="alias/app-secrets-key",
GranteePrincipal="arn:aws:iam::111122223333:role/worker-role",
Operations=["Decrypt"],
)// --- TypeScript (AWS SDK v3) ---
const grant = await kms.send(new CreateGrantCommand({
KeyId: "alias/app-secrets-key",
GranteePrincipal: "arn:aws:iam::111122223333:role/worker-role",
Operations: ["Decrypt"],
}));GetKeyRotationStatus confirms it actually took effect.ListGrants and revoke anything no longer tied to an active workload.Resource or Principal, note why for reviewers.Symmetric operations are faster and cover nearly every encryption use case within AWS. Asymmetric keys add value only when an outside party must encrypt or verify without AWS credentials.
Direct Encrypt caps near 4KB for symmetric keys. Envelope encryption calls KMS once per object (for the data key) regardless of payload size, then does unlimited-size AES-GCM work locally.
Persisting it anywhere - logs, disk, a cache - defeats the purpose of envelope encryption. Scope it to the smallest code block and discard it right after use.
Whenever access is temporary, per-workload, or needs independent revocation. Grants avoid coordinating edits to a document other teams or automations may also depend on.
You risk permanently locking every principal, including admins, out of the key - there is no external recovery path for a CMK's own policy. Always keep a root-account statement unless you have a tested plan.
No. Rotation is transparent - the key ID, ARN, and alias stay the same, and old ciphertext continues to decrypt because KMS retains prior key material internally.
Because it cannot be changed after the fact. If cross-region decrypt for DR or a global application is even plausible, provision the key as multi-region from the start.
No - treat it as metadata only. It is authenticated (must match on decrypt) but not encrypted, and it appears in plaintext in CloudTrail logs.
On a recurring cadence, not once. Review CloudTrail, run ListGrants to prune stale delegations, and confirm rotation and multi-region settings still match your DR plan.
Stack versions: This page was written for boto3 1.43.x (Python 3.10+) and the AWS SDK for JavaScript v3 (Node.js 18+).
Revisado por Chris St. John·Última actualización: 25 jul 2026