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.
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?
A database password and an encryption key serve different roles. Put the password in Secrets Manager so an approved app can fetch the current secret value. A KMS key can protect the data key used to encrypt that value at rest. The app calls GetSecretValue and Secrets Manager performs the service’s cryptographic work. If a customer managed key protects the secret, check the secret-reading permission and the key permission separately. A permission to use the database is also separate from permission to obtain its password.
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.
| Service | Use for |
|---|---|
| KMS | Create/control CMKs, encrypt data keys |
| Secrets Manager | DB creds, API tokens, rotation |
| SSM Parameter Store | Config and simpler secrets |
3. How to wire it
Rotation means replacing the old credential with a new one and coordinating the change with the service that uses it. For a practice example, imagine the secret changes from one dummy value to another. A program with the old cached value can fail after the database expects the new one. Test how the lab app notices a refresh by recording only whether access works. Keep the returned secret out of logs. If the request is denied, read the policy evidence and key condition rather than printing the secret to diagnose access.
- Create a CMK (or use aws/secretsmanager carefully). Prefer customer CMK for audit.
- Create a secret; choose RDS rotation if supported.
- Grant the app role
secretsmanager:GetSecretValueon the secret ARN. If the secret uses a customer managed KMS key, also allowkms:Decryptfor that key. Check that its key policy allows the access either directly or through IAM. - Never log the secret string.
- Alert on unusual GetSecretValue spikes.
- 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
Use a dummy value unrelated to real logins. Verify the authorized lab identity can retrieve it without displaying or recording the value. Compare with a test identity without access, which should receive a denial. If you test a customer managed key, document which permission controls decryption. Afterward schedule deletion according to the lab’s cleanup steps and check the secret’s pending state.
Do this in ap-south-1 (Mumbai) unless your org forbids it. Set a budget alert first.
- KMS → create symmetric CMK alias alias/lab-secrets.
- Secrets Manager → store new secret lab/demo string not-a-real-password.
- Encrypt with your CMK.
- From CloudShell get-secret-value in ap-south-1 with a lab role.
- Note CloudTrail shows the API call.
Cleanup (stop billing)
- Delete the secret schedule and schedule CMK deletion (7–30 days).
- 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 / symptom | Fix |
|---|---|
| Secret in Lambda env plaintext | Move to Secrets Manager; inject at runtime. |
| Resource star on GetSecretValue | Pin secret ARN. |
| Disabled CMK broke prod | Monitor CMK state; staged disable. |
| Rotation Lambda fails silently | Alarm on rotation errors. |
| Cached secret forever in memory | Short TTL cache; refresh on auth errors. |
| Shared secret across 40 apps | Split secrets; least blast radius. |
| Put secret in AMI | Bake no secrets; fetch at boot. |
| Forgot replica for multi-region | Replicate secret + key strategy. |
| Developers all Admin | Separate read roles; break-glass only. |
| Printed secret in CI logs | Mask; use OIDC roles. |
7. Troubleshooting checklist
- AccessDeniedException → IAM + key policy both.
- Secret pending deletion — restore if needed.
- Rotation: check Lambda VPC/SG to DB.
- Decrypt failures after key policy edit — rollback policy.
- Wrong region secret id.
- 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?
In the same account, a KMS key policy can allow a principal directly or let an IAM policy grant access. Check for applicable explicit denies.
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
- Scan one personal project for hardcoded passwords.
- Write IAM policy for one secret ARN only.
- Explain envelope encryption to a friend in 60 seconds.
In short
- Secrets Manager stores rotating secrets.
- KMS holds and controls encryption keys.
- IAM + key policy together gate decrypt.
- 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.











More cards: Ravi Sir's Cards.
Practise this chapter
Interview questions and answers
An app is allowed to call GetSecretValue but gets a denial for a customer-managed-encrypted secret. What is your next check?
Confirm the ARN it asks for and inspect the KMS key policy and permission to decrypt with the relevant customer managed key. Secret access and cryptographic key access are evaluated separately. Reproduce with a dummy secret and log the success or denial while keeping the value private.
Check your understanding
Choose an answer, then compare your reasoning with the explanation.
Practice Tasks
Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.
About this scenarioAmravati 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
Inventory secrets.
List secrets.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy Describe secret
Metadata without value.
Describe secret amravatiagro/db.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy List keys
See CMKs.
List KMS keys.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy List aliases
Human names for keys.
List aliases.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy Min recovery window days
Delete schedule.
Minimum recovery window days often used in labs (enter 7)?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Easy Plaintext env bad — enter 0 for allowed?
Never plaintext.
Is plaintext DB password in Lambda env OK? Enter 0 for no.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Medium Get secret value
Runtime fetch.
Get secret value amravatiagro/db.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Describe key
Check state/policy.
Describe key alias/lab-secrets.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Decrypt on one key
Apps need decrypt.
Allow kms:Decrypt on arn:aws:kms:ap-south-1:120348739971:key/abcd-efgh.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.
Medium Secret string JSON
Structured secrets.
JSON username app password changeme.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Fill in each property with the value from the task.
- Click Check answer. Key order does not matter.
Medium Apply the lesson: scenario 11
A role in account A needs to use a KMS key in account B. Account A already grants IAM permission. Which policy on the key must also permit the cross-account use?
- Read the situation and identify what you need to decide.
- Work through the relevant concept or calculation.
- Enter only the requested term or number.
- Check your answer, then read the explanation.
Medium Short TTL minutes classroom
Do not cache forever.
A short classroom secret cache TTL in minutes (enter 5)?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Hard Rotate secret
Trigger rotation.
Rotate secret amravatiagro/db.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Delete secret schedule
Cleanup labs safely.
Delete secret amravatiagro/lab with recovery window 7.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard GetSecretValue pinned
No star resources.
Allow secretsmanager:GetSecretValue on arn:aws:secretsmanager:ap-south-1:120348739971:secret:amravatiagro/db-*.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.
Hard Rotation days JSON
Schedule rotation.
JSON RotationDays 30.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Fill in each property with the value from the task.
- Click Check answer. Key order does not matter.