Engineering Notes
Home/DevOps & Cloud/AWS/Secrets Manager, SSM Parameter Store and KMS
DevOps & Cloud · AWS

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.

☁️ DevOps & Cloud● Intermediate⏱ 5 min read▶ 2 interactive🛠️ 2 exercises🎤 5 interview Q&As

🎯 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

1Three Services, Three Jobs

ServiceJobTypical content
AWS KMScreate and control encryption keys; encrypt/decrypt small data; envelope encryption for everything elsekeys for S3, EBS, RDS, Secrets Manager, your own data
AWS Secrets Managerstore secrets with rotation, versioning, cross-region replicationDB credentials, API tokens, OAuth client secrets
SSM Parameter Storestore configuration (and simple secrets as SecureString) in a hierarchyfeature flags, URLs, IDs, small secrets

2KMS: Keys You Never See

bash
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/GenerateDataKey via 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: GenerateDataKey returns 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

bash
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 version AWSPENDING), setSecret (change the password in the database), testSecret, finishSecret (move AWSCURRENT label). Apps always read AWSCURRENT.
  • Managed rotation: RDS manage_master_user_password lets RDS own the secret and rotate it (devops-cloud/terraform/08-…).
  • Native integrations: ECS/EKS (secrets valueFrom / 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

bash
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 (valueFrom is fine — injected at start).
  • Least privilege: each workload can read only its own secrets (secretsmanager:GetSecretValue on specific ARNs, kms:Decrypt on the specific key via kms:ViaService condition).
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.

An application reads a secret from Secrets Manager or a parameter from SSM; the service decrypts it via KMS using envelope encryption, rotation Lambdas update the secret and the database, the app caches values with a TTL and refreshes on auth failure, and CloudTrail logs every access
An application reads a secret from Secrets Manager or a parameter from SSM; the service decrypts it via KMS using envelope encryption, rotation Lambdas update the secret and the database, the app caches values with a TTL and refreshes on auth failure, and CloudTrail logs every access

⚠️ Common Pitfalls

  1. Secrets in code, tfvars, task definition env values or images.
  2. Fetching secrets on every request — cost, latency, throttling.
  3. Caching forever — rotations break the app until restart.
  4. Over-broad permissions (secretsmanager:* on *, kms:Decrypt on *).
  5. Using AWS managed keys when cross-account access is needed (their key policies can't be edited).
  6. Scheduling KMS key deletion without checking what's encrypted with it (data becomes unrecoverable after the waiting period).

📋 Quick Reference

text
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

Questions interviewers ask about this topic. Answer out loud first, then open a card to compare.

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.

Follow-ups interviewers ask
  • 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.

Follow-ups interviewers ask
  • 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.

Follow-ups interviewers ask
  • 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).

Follow-ups interviewers ask
  • 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.

Follow-ups interviewers ask
  • 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.

1. Why not send a 2 GB file to KMS to encrypt?
Encrypt data keys, not data.
2. Where is the CMK's key material?
You only reference it.
3. Which service rotates database passwords automatically?
Rotation via Lambda.
4. What controls who can use a KMS key?
Key policies are the root of trust.