Secrets Manager, SSM Parameter Store and KMS
KMS manages encryption keys you never see; Secrets Manager stores and rotates secrets; Parameter Store holds configuration. Walk through envelope encryption step by step, and see how ECS injects a secret into a container.
🎯 What you'll learn
- Three services, three jobs
- KMS keys, key policies, grants and envelope encryption
- Secrets Manager rotation vs SSM Parameter Store
- Using secrets in applications and what ECS does under the hood
📚 Before you start
1Three Services, Three Jobs
| Service | Job | Typical content |
|---|---|---|
| AWS KMS | create and control encryption keys; encrypt/decrypt small data; envelope encryption for everything else | keys for S3, EBS, RDS, Secrets Manager, your own data |
| AWS Secrets Manager | store secrets with rotation, versioning, cross-region replication | DB credentials, API tokens, OAuth client secrets |
| SSM Parameter Store | store configuration (and simple secrets as SecureString) in a hierarchy | feature flags, URLs, IDs, small secrets |
2KMS: Keys You Never See
aws kms create-key --description "petpal app data" --key-spec SYMMETRIC_DEFAULT
aws kms create-alias --alias-name alias/petpal-app --target-key-id <key-id>
aws kms encrypt --key-id alias/petpal-app --plaintext fileb://card-token.txt # ≤ 4 KB- Keys never leave KMS's HSMs in plaintext; you call
Encrypt/Decrypt/GenerateDataKeyvia API, every call is logged in CloudTrail. - AWS managed keys (
aws/s3,aws/rds…) vs customer managed keys (your key policy, rotation, cross-account grants, disable/schedule deletion) — use CMKs when you need control over who can decrypt. - Key policies are the primary access control (IAM policies only work if the key policy allows the account); grants give temporary, scoped permissions (used by services like EBS).
- Automatic rotation (yearly or custom period): new key material, old material kept to decrypt old data — transparent to callers.
- Envelope encryption:
GenerateDataKeyreturns a plaintext data key (encrypt locally with AES-GCM, then discard) and the same key encrypted under the KMS key (store it next to the ciphertext). Decrypt =Decrypt(encryptedDataKey)→ plaintext data key → decrypt data locally. That's how S3/EBS/RDS encryption work, and how the AWS Encryption SDK works (security/security/07-hashing_vs_encryption).
3Secrets Manager
aws secretsmanager create-secret --name prod/petpal/stripe --secret-string '{"apiKey":"sk_live_..."}' \
--kms-key-id alias/petpal-secrets
aws secretsmanager get-secret-value --secret-id prod/petpal/stripe- Rotation: a Lambda (AWS-provided templates for RDS/Aurora/Redshift/DocumentDB, or your own) runs on a schedule through four steps —
createSecret(new versionAWSPENDING),setSecret(change the password in the database),testSecret,finishSecret(moveAWSCURRENTlabel). Apps always readAWSCURRENT. - Managed rotation: RDS
manage_master_user_passwordlets RDS own the secret and rotate it (devops-cloud/terraform/08-…). - Native integrations: ECS/EKS (
secretsvalueFrom / external-secrets), Lambda (Parameters and Secrets extension), CloudFormation dynamic references. - Resource policies for cross-account access; replication to other regions for DR.
- Price: ~$0.40 per secret per month + API calls — cache reads.
4SSM Parameter Store
aws ssm put-parameter --name /prod/petpal/api/base_url --type String --value https://api.petpal.io
aws ssm put-parameter --name /prod/petpal/api/legacy_token --type SecureString --value "..." --key-id alias/petpal-app
aws ssm get-parameters-by-path --path /prod/petpal/api/ --recursive --with-decryption- Hierarchical names → IAM policies by path (
arn:aws:ssm:…:parameter/prod/petpal/*). - Standard tier free (4 KB values, 10,000 parameters), Advanced for 8 KB values and parameter policies (expiration).
SecureString= KMS-encrypted; no built-in rotation.- Great for config shared between IaC units (
devops-cloud/terraform/03-…Part 2) and apps.
Choose: Secrets Manager for credentials that should rotate or be replicated; Parameter Store for configuration and secrets that don't need rotation (cost-sensitive).
5Using Secrets in Applications
- Fetch at startup and cache with a TTL (e.g. 5 minutes) so rotations propagate without restarts, and so you don't call the API per request (cost, throttling).
- On authentication failure, refresh the secret once and retry (the secret may have just rotated).
- Never log secrets; never put them in environment variables baked into images or task definitions as plain values (
valueFromis fine — injected at start). - Least privilege: each workload can read only its own secrets (
secretsmanager:GetSecretValueon specific ARNs,kms:Decrypton the specific key viakms:ViaServicecondition).
Under the HoodWhat Happens When ECS Injects a Secret▾
When a Fargate task starts, the agent — using the task execution role — calls secretsmanager:GetSecretValue (or ssm:GetParameters) for each secrets entry. Secrets Manager stores the secret encrypted with a data key that's itself encrypted under the secret's KMS key; to return the value, Secrets Manager calls kms:Decrypt on behalf of the caller (so the execution role also needs decrypt permission on a customer-managed key, typically constrained with kms:ViaService = secretsmanager.<region>.amazonaws.com). The plaintext is passed into the container as an environment variable at process start. Every step — the GetSecretValue call and the KMS decrypt — appears in CloudTrail with the role and task identity, giving an audit trail of who read which secret when. Note the consequence: a rotated secret reaches a running task only when the task restarts, unless the application reads the secret itself with a cache TTL.
⚠️ Common Pitfalls
- Secrets in code, tfvars, task definition env values or images.
- Fetching secrets on every request — cost, latency, throttling.
- Caching forever — rotations break the app until restart.
- Over-broad permissions (
secretsmanager:*on*,kms:Decrypton*). - Using AWS managed keys when cross-account access is needed (their key policies can't be edited).
- Scheduling KMS key deletion without checking what's encrypted with it (data becomes unrecoverable after the waiting period).
📋 Quick Reference
KMS: CMKs + key policies + rotation · Encrypt ≤ 4 KB · GenerateDataKey for envelope encryption · CloudTrail on every use
Secrets Manager: rotation Lambda (4 steps, AWSCURRENT/AWSPENDING) · managed RDS passwords · replication · ~$0.40/secret
Parameter Store: hierarchical config · SecureString via KMS · free standard tier · IAM by path
apps: cache with TTL, refresh on auth failure, least privilege (ARN-scoped + kms:ViaService)🧠 Key takeaways
- KMS holds keys in HSMs; access is controlled by key policies + IAM + grants, and every use is logged.
- Envelope encryption: KMS encrypts small data keys; data is encrypted locally (KMS calls are limited to 4 KB).
- Secrets Manager for credentials with rotation; Parameter Store for config and SecureStrings (cheaper, no built-in rotation).
- Fetch secrets at runtime via roles (ECS/Lambda integrations), cache with TTL, never bake into images or env files in Git.
🎤 Interview Q&A
Q1Secrets Manager vs SSM Parameter Store?core▾
Secrets Manager: built for secrets, automatic rotation (RDS etc.), cross-account sharing, higher cost per secret. Parameter Store: config and secrets (SecureString), hierarchical, free standard tier, no built-in rotation.
- Common split? Parameter Store for config; Secrets Manager for credentials needing rotation.
Q2What does KMS do?core▾
Manages encryption keys: creates and stores keys in HSMs, controls access through key policies and IAM, encrypts data keys (envelope encryption), logs usage in CloudTrail, and supports rotation.
- Customer managed vs AWS managed keys? Customer managed keys give control over policy, rotation and cross-account access.
Q3How do applications retrieve secrets safely?senior▾
Using their IAM role to fetch at startup or via SDK with caching, never baking secrets into images or code; ECS/EKS can inject secrets into containers; rotate and reload without redeploys where possible.
- Why cache? To reduce latency and API costs, with a TTL for rotation.
Q4How does secret rotation work?core▾
Secrets Manager invokes a rotation Lambda that creates new credentials, updates the target (e.g. database), tests them and marks them current, keeping the previous version during transition (staging labels AWSCURRENT/AWSPREVIOUS).
- App consideration? Retry with a fresh secret on authentication failure.
Q5What's envelope encryption in KMS?core▾
KMS generates a data key; you encrypt data locally with the plaintext data key and store the encrypted data key alongside. Only KMS (with permission) can decrypt the data key, so large data never goes to KMS.
- Why not encrypt directly with KMS? 4 KB limit and API latency/cost.
🛠️ Practice exercises
Each exercise is a file in this topic folder. Code exercises are self-checking: run them and make the checks pass.
✅ Check yourself
Answer the questions — feedback is instant.