Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Secrets Manager and KMS: Protect Keys and Secrets Explained

Juniors still put database passwords in Lambda environment variables in plaintext. That is a gift to anyone who can read the config. AWS Secrets Manager stores the secret encrypted; AWS KMS holds the keys that encrypt. Together they are the lock and the key factory.

What you'll learn in this chapter

  • Why plaintext secrets fail audits
  • Secrets Manager vs Parameter Store
  • KMS CMK concepts: key policy, grants
  • Rotation and RDS integration basics
  • Lab, errors, FAQ, interview

1. Everyday example first

A hospital app connects to RDS. Password rotates every 30 days. Secrets Manager can rotate with a Lambda; apps call GetSecretValue at runtime (or cache briefly). KMS CMK encrypts the secret string. If a laptop is stolen, the thief still needs IAM + KMS permission.

2. Why both services?

Why do we need this?

KMS is about keys and cryptographic operations. Secrets Manager is about storing rotating application secrets. Parameter Store (SSM) can store SecureStrings with KMS too — pick Secrets Manager when you need managed rotation and fine secret lifecycle.

ServiceUse for
KMSCreate/control CMKs, encrypt data keys
Secrets ManagerDB creds, API tokens, rotation
SSM Parameter StoreConfig and simpler secrets
App → GetSecretValue → KMS decrypt → ConnectNeed DB password safely
IAM + KMS gated fetch

3. How to wire it

How to do it (step by step)

  1. Create a CMK (or use aws/secretsmanager carefully). Prefer customer CMK for audit.
  2. Create a secret; choose RDS rotation if supported.
  3. Grant the app role secretsmanager:GetSecretValue on that ARN and kms:Decrypt.
  4. Never log the secret string.
  5. Alert on unusual GetSecretValue spikes.
  6. Document break-glass procedure.
{
  "Effect": "Allow",
  "Action": ["secretsmanager:GetSecretValue"],
  "Resource": "arn:aws:secretsmanager:ap-south-1:111122223333:secret:prod/db-*"
}

4. Industries

  • BFSI: mandatory rotation + CMK where required
  • SaaS multi-tenant: per-tenant API tokens
  • Healthcare: audit who decrypted
  • Startups: stop Slack-shared passwords

5. Lab: create a dummy secret and read it

Do this in ap-south-1 (Mumbai) unless your org forbids it. Set a budget alert first.

How to do it (step by step)

  1. KMS → create symmetric CMK alias alias/lab-secrets.
  2. Secrets Manager → store new secret lab/demo string not-a-real-password.
  3. Encrypt with your CMK.
  4. From CloudShell get-secret-value in ap-south-1 with a lab role.
  5. Note CloudTrail shows the API call.

Cleanup (stop billing)

  1. Delete the secret schedule and schedule CMK deletion (7–30 days).
  2. Remove lab IAM policies.

6. Common errors and fixes

Each row is a real mistake students and juniors hit. Read the fix before you panic.

Error / symptomFix
Secret in Lambda env plaintextMove to Secrets Manager; inject at runtime.
Resource star on GetSecretValuePin secret ARN.
Disabled CMK broke prodMonitor CMK state; staged disable.
Rotation Lambda fails silentlyAlarm on rotation errors.
Cached secret forever in memoryShort TTL cache; refresh on auth errors.
Shared secret across 40 appsSplit secrets; least blast radius.
Put secret in AMIBake no secrets; fetch at boot.
Forgot replica for multi-regionReplicate secret + key strategy.
Developers all AdminSeparate read roles; break-glass only.
Printed secret in CI logsMask; use OIDC roles.

7. Troubleshooting checklist

  1. AccessDeniedException → IAM + key policy both.
  2. Secret pending deletion — restore if needed.
  3. Rotation: check Lambda VPC/SG to DB.
  4. Decrypt failures after key policy edit — rollback policy.
  5. Wrong region secret id.
  6. CloudTrail not showing? Include management events.

8. FAQ

Q. Secrets Manager vs SSM SecureString?

Secrets Manager shines at rotation and secret lifecycle.

Q. Who can decrypt?

Principals allowed by key policy AND IAM.

Q. Envelope encryption?

KMS encrypts data keys; data keys encrypt bulk data.

Q. Cost worry?

Secrets and API calls cost; delete unused.

Q. Can I use CMK with S3/EBS too?

Yes — same CMK discipline.

9. Interview Q&A

Q. Why not env vars for DB passwords?

Visible in console/config; hard to rotate; often logged.

Q. What does KMS do?

Manages keys and encrypt/decrypt/generate data keys.

Q. CMK vs AWS managed key?

CMK: you control policy and rotation options.

Q. How rotation works?

Lambda updates DB and secret versions in stages.

Q. Least privilege for secrets?

GetSecretValue on exact ARN + kms:Decrypt on that CMK.

Q. Detect misuse?

CloudTrail + metric alarms on GetSecretValue.

10. Tips in English, Marathi and Hindi

Ravindra Bagale's Tip (EN)

Treat every secret like a house key: know who has a copy and change the locks on a schedule.

Ravindra Bagale's Tip (MR)

Secret = घरची चावी: copy कोणाकडे, schedule वर locks बदला.

Tip (HI)

हर secret घर की चाबी है — किसके पास copy है जानो और समय पर ताले बदलो।

Try at home

  1. Scan one personal project for hardcoded passwords.
  2. Write IAM policy for one secret ARN only.
  3. Explain envelope encryption to a friend in 60 seconds.

In short

  1. Secrets Manager stores rotating secrets.
  2. KMS holds and controls encryption keys.
  3. IAM + key policy together gate decrypt.
  4. No plaintext secrets in env, AMI, or Git.

Revision, interview and comparison cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: Secrets Manager + KMS. Passwords in git or Lambda env plaintext leak and rarely rotate. Secrets Manager stores secrets; KMS encrypts them. Apps retrieve at runtime with IAM. Create secret → encrypt with CMK → grant app role secretsmanager:GetSecretValue + kms:Decrypt → rotate. RDS password lives in Secrets Manager; Lambda reads it each cold start or caches briefly.
Revision card: Secrets Manager + KMS · Download PNG · Open card page
Revision card: Automatic rotation. Static DB passwords for years are a breach waiting to happen. Rotation uses a Lambda that creates a new password, updates DB and secret versions (AWSPENDING → AWSCURRENT). Enable rotation schedule; test in staging; monitor rotation failures in CloudWatch. Every 30 days the clinic DB password changes without redeploying the app.
Revision card: Automatic rotation · Download PNG · Open card page
Revision card: CMK vs AWS managed. Compliance may require key policies, rotation evidence, and cross-account control. AWS managed keys are easy; customer managed keys (CMK) give key policies, grants, and clearer ownership. Use CMK for production secrets; separate keys per env; least-privilege key policy. Finance account decrypts only with finance CMK; marketing role is denied by key policy.
Revision card: CMK vs AWS managed · Download PNG · Open card page
Revision card: Least privilege for secrets. Resource * on GetSecretValue lets one breach read every secret. Grant GetSecretValue on specific secret ARNs; separate read from put/rotate roles. App role: get one secret. Admin role: create/rotate. Deny decrypt of other envs’ CMKs. Staging role cannot GetSecretValue on prod/db-password ARN.
Revision card: Least privilege for secrets · Download PNG · Open card page
Revision card: Secrets Manager vs SSM. Teams mix Parameter Store and Secrets Manager without a rule. SSM Parameter Store (SecureString) is fine for config; Secrets Manager adds rotation and richer secret lifecycle. Use Secrets Manager for DB/API credentials that must rotate; SSM for non-rotating app config. Stripe API key with rotation → Secrets Manager; FEATURE_X=true → SSM String.
Revision card: Secrets Manager vs SSM · Download PNG · Open card page
Comparison: Secrets Manager vs SSM Parameter Store. Secrets Manager: Secret lifecycle + managed rotation. SSM Parameter Store: Hierarchical config; SecureString optional.
Comparison: Secrets Manager vs SSM Parameter Store · Download PNG · Open card page
Comparison: Customer managed key (CMK) vs AWS managed key. Customer managed key (CMK): You set key policy, rotation, grants, aliases. AWS managed key: AWS manages the key; simpler, less control.
Comparison: Customer managed key (CMK) vs AWS managed key · Download PNG · Open card page
Interview card: Why Secrets Manager + KMS?. Central secret storage with encryption, IAM retrieval, and optional rotation — not git or plaintext env. KMS CMKs add key-policy control for compliance. Lambda gets DB password at runtime from Secrets Manager.
Interview card: Why Secrets Manager + KMS? · Download PNG · Open card page
Interview card: How does rotation work?. Rotation Lambda sets AWSPENDING password, updates DB, then AWSCURRENT. Monitor failures; test in staging first. 30-day schedule for clinic RDS secret.
Interview card: How does rotation work? · Download PNG · Open card page
Interview card: Secrets Manager vs SSM?. Secrets Manager for rotating credentials; SSM Parameter Store for general config (SecureString OK). Do not invent two homes for the same DB password. Stripe key→Secrets; FEATURE flag→SSM.
Interview card: Secrets Manager vs SSM? · Download PNG · Open card page
Interview card: Cross-account secret access?. Resource policy on the secret plus KMS key policy grants to the consumer account/role. Prefer pulling into the app role over copying plaintext across accounts. Analytics role in account B GetSecretValue on account A reporting-api secret.
Interview card: Cross-account secret access? · Download PNG · Open card page

More cards: Ravi Sir's Cards.

Practice Tasks

About this scenario

Amravati Agro Market is a agriculture business in Amravati. Anjali Thakre looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 16

Easy List secrets

Why: Inventory secrets.

What to do: List secrets.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy Describe secret

Why: Metadata without value.

What to do: Describe secret amravatiagro/db.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy List keys

Why: See CMKs.

What to do: List KMS keys.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy List aliases

Why: Human names for keys.

What to do: List aliases.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy Min recovery window days

Why: Delete schedule.

What to do: Minimum recovery window days often used in labs (enter 7)?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Plaintext env bad — enter 0 for allowed?

Why: Never plaintext.

What to do: Is plaintext DB password in Lambda env OK? Enter 0 for no.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Get secret value

Why: Runtime fetch.

What to do: Get secret value amravatiagro/db.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Describe key

Why: Check state/policy.

What to do: Describe key alias/lab-secrets.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Decrypt on one key

Why: Apps need decrypt.

What to do: Allow kms:Decrypt on arn:aws:kms:ap-south-1:120348739971:key/abcd-efgh.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Secret string JSON

Why: Structured secrets.

What to do: JSON username app password changeme.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Medium IAM and key policy both needed — 1

Why: Two gates.

What to do: Enter 1 if decrypt needs IAM and key policy.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Short TTL minutes classroom

Why: Do not cache forever.

What to do: A short classroom secret cache TTL in minutes (enter 5)?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Rotate secret

Why: Trigger rotation.

What to do: Rotate secret amravatiagro/db.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Delete secret schedule

Why: Cleanup labs safely.

What to do: Delete secret amravatiagro/lab with recovery window 7.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard GetSecretValue pinned

Why: No star resources.

What to do: Allow secretsmanager:GetSecretValue on arn:aws:secretsmanager:ap-south-1:120348739971:secret:amravatiagro/db-*.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Rotation days JSON

Why: Schedule rotation.

What to do: JSON RotationDays 30.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.