Encryption and Secrets: KMS and Secrets Manager
Data must be protected at rest (on disks and in S3) and in transit (TLS). Application secrets – database passwords, API keys – must never live in code or Git.
AWS Key Management Service (KMS)
| Concept | Meaning |
|---|---|
| KMS key | A logical key whose material never leaves KMS hardware security modules |
| AWS managed key | Created by a service (aws/s3), automatic |
| Customer managed key | You control policy, rotation, deletion – required for fine-grained access and auditing |
| Key policy | Resource policy deciding who can use/administer the key |
| Rotation | Yearly automatic rotation of key material |
| Grants | Temporary, delegated use of a key by a service |
Envelope encryption
sequenceDiagram
participant App
participant KMS
App->>KMS: GenerateDataKey(KeyId)
KMS-->>App: plaintext data key + encrypted data key
App->>App: encrypt data with plaintext key, discard it
App->>App: store ciphertext + encrypted data key
App->>KMS: Decrypt(encrypted data key) when reading
Large data is encrypted locally with a data key; only the small data key is encrypted by KMS. Services such as S3 (SSE-KMS), EBS and RDS do this for you. Every use of a key is recorded in CloudTrail.
AWS Secrets Manager
| Feature | Notes |
|---|---|
| Store secrets | JSON such as {"username":"app","password":"…"}, encrypted with KMS |
| Automatic rotation | Built-in rotation for RDS/Aurora/Redshift/DocumentDB; Lambda rotation for anything else |
| Versioning | AWSCURRENT, AWSPREVIOUS labels for safe rotation |
| Access control | IAM policies + resource policies |
| Integrations | ECS/EKS secrets injection, Lambda extension with caching |
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
const sm = new SecretsManagerClient({});
let cached; // reuse across warm invocations
export const handler = async () => {
cached ??= JSON.parse((await sm.send(new GetSecretValueCommand({ SecretId: 'prod/db' }))).SecretString);
// connect using cached.username / cached.password
};
The function’s role needs secretsmanager:GetSecretValue on that secret (and kms:Decrypt for a customer managed key).
SSM Parameter Store
Hierarchical configuration (/notes/prod/table-name) – String, StringList and SecureString (KMS-encrypted). Free standard tier; no built-in rotation. Use it for configuration, Secrets Manager for credentials that rotate.
Encryption in transit
TLS everywhere: HTTPS endpoints, ACM certificates on ALB/CloudFront/API Gateway, aws:SecureTransport conditions in bucket policies, TLS to RDS.
No secrets in code, environment files or Git. Store them in Secrets Manager (rotating) or Parameter Store (config), read them at runtime with a least-privilege role, and encrypt data with KMS keys you control.
Practise it
Virtual lab · Amazon Web Services · 35 min
Protect secrets with AWS KMS and Secrets Manager
Create a customer managed KMS key with rotation, encrypt and decrypt data from the CLI, store database credentials in Secrets Manager encrypted with your key, give a Lambda function least-privilege access to read them, and rotate the secret.
Quick revision
- KMS keys never leave KMS; key policies control use.
- Envelope encryption: data key encrypts data, KMS encrypts the data key.
- Secrets Manager stores and rotates credentials.
- Parameter Store for configuration and SecureStrings.
- TLS in transit with ACM certificates.
Practise this topic
Hands-on virtual lab
Apply what you just read in a realistic, guided simulation. Free with a verified account.
Create a free account to track your progress and unlock virtual labs.