KMS encryption keys
-
Phase 0: Before you start
-
Phase 1: The foundation
-
Phase 2: Operations and security
-
Phase 3: SQL Server on AWS
-
Phase 4: Troubleshooting step by step
-
Phase 5: Final project and career
Encryption is only as good as your control over the key. AWS KMS keys are where that control sits: who can encrypt, who can decrypt, when the keys rotate, plus a log of every use. S3, EBS, RDS, Secrets Manager and AWS Backup all call KMS in the background.
You’ll create a customer managed key, encrypt and decrypt a secret by hand, watch envelope encryption happen, point an S3 bucket at your key and turn on rotation. After that you’ll disable the key to see what it does to things, and fix the KMS error people hit most, which is S3 access without kms:Decrypt.
| Time | You need | By the end you can |
|---|---|---|
| 45 minutes | Lessons 17 and 25 done (S3 security, saat-trail), your laptop signed in as saat-admin. Cost: a customer managed key costs about USD 1 a month, charged by the hour, plus a few cents of requests; you schedule it for deletion at the end. | Create a customer managed key and give it an alias, read its key policy, encrypt and decrypt from the CLI, use the key for S3 SSE-KMS, turn on rotation, and troubleshoot disabled keys and a missing kms:Decrypt. |
What it is, in one line
AWS KMS keys are encryption keys managed by AWS Key Management Service. They never leave KMS, and every use goes through key policies and IAM and gets logged in CloudTrail.
In Simple Words
The bank locker key held at the counter
Your locker sits in the bank vault, but the key never leaves the bank. When you want something out, you show your ID at the counter, the bank checks your name is on the approved list, opens the locker for you and writes in a book that you came at 3:15 p.m. KMS works like that counter: your data can be anywhere, but the key that opens it stays inside KMS, with the key policy as the approved list and CloudTrail as the book. And if the bank freezes your locker key, nobody gets in, you included, until it’s unfrozen, which is what a disabled key is.
Engineer's View
How KMS really works
A KMS key (what used to be called a CMK) is a logical key backed by hardware security modules, and its key material never leaves KMS unencrypted. A direct Encrypt call only handles up to 4 KB, so services use envelope encryption: GenerateDataKey hands back a plaintext data key, which is used in memory and thrown away, along with the same key encrypted under your KMS key, which gets stored with the data. To read the data later, you ask KMS to decrypt that small encrypted key.
The key policy is the first thing that controls access. The default policy gives the account root principal full access, which is what lets IAM policies grant use of the key, and if that statement isn’t there, IAM policies on their own won’t work.
Grants hand temporary, narrow permissions to services. Anyone calling needs kms:Decrypt (and often kms:GenerateDataKey) on top of their S3, EBS or RDS permissions.
Automatic rotation creates new key material on a schedule (anywhere from 90 to 2,560 days, with 365 as the default), and the old material stays around to decrypt old data. Disabling a key stops all use straight away. Deletion is always scheduled with a 7 to 30 day waiting period, because once a key is deleted the data it protected can’t be recovered.
How it works, step by step
This is what happens when S3 stores an object using your key.
1S3 asks KMS for a data key
When you upload, S3 calls GenerateDataKey with alias/saat-data, and KMS checks the key policy and your IAM permissions before handing back two copies of a new data key.
2The data is encrypted locally
S3 encrypts the object with the plaintext data key, throws that key away and keeps the encrypted data key next to the object.
3Reading needs KMS again
When you download, S3 sends the encrypted data key to KMS to be decrypted. If the caller has no kms:Decrypt, or the key is disabled, the download fails even though the S3 permissions are fine.
4Pick the right kind of key
The kinds of key don’t all give you the same control:
Go for customer managed keys when you need to control who can decrypt (payroll, customer data, backups), keep each team’s access separate, or show auditors how the key is used. For low-risk data, AWS managed keys are fine.
✅ Checkpoint: You can explain envelope encryption, what the root statement in the key policy does, and why S3 access on its own won’t let you read SSE-KMS objects.
Hands-on lab: create and control AWS KMS keys
Before you run any command, double-check it
Read every command twice before you press Enter. Check the target: the system or account, the IDs, the names and the sizes. These commands are for learning and were tested in our lab, not on your system. At work, run the read-only list commands first, follow your change process, make sure a backup exists, and ask a second person to check anything that changes or deletes data. You run them at your own risk.
Sign in and set up the names:
cd C:\aws-lab
aws login --profile saat-admin
$env:AWS_PROFILE = 'saat-admin'
$acct = aws sts get-caller-identity --query Account --output text
$myArn = aws sts get-caller-identity --query Arn --output text
$bucket = "saat-kms-$acct-ap-south-1-an"1Create a key and read its policy
Paste this whole block:
$key = aws kms create-key --description "saat lab data key" --tags TagKey=course,TagValue=saatvedha-aws --query KeyMetadata.KeyId --output text
$keyArn = aws kms describe-key --key-id $key --query KeyMetadata.Arn --output text
aws kms create-alias --alias-name alias/saat-data --target-key-id $key
(aws kms get-key-policy --key-id $key --policy-name default --query Policy --output text | ConvertFrom-Json).Statement | Select-Object Sid, Effect, @{n='Principal';e={$_.Principal.AWS}}, ActionYou should see something like this:
Sid Effect Principal Action
--- ------ --------- ------
Enable IAM User Permissions Allow arn:aws:iam::111122223333:root kms:*That one statement means “this account’s IAM policies decide who can use the key”. Take it out, and only principals named in the key policy can use the key, no matter what IAM says.
2Encrypt and decrypt a secret
Paste this whole block:
Set-Content -NoNewline secret.txt "db_password=Saat-Lab-Not-Real-123"
$ct = aws kms encrypt --key-id alias/saat-data --plaintext fileb://secret.txt --query CiphertextBlob --output text
[IO.File]::WriteAllBytes("$PWD\secret.enc", [Convert]::FromBase64String($ct))
Remove-Item secret.txt
$pt = aws kms decrypt --ciphertext-blob fileb://secret.enc --query Plaintext --output text
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($pt))
$dk = aws kms generate-data-key --key-id alias/saat-data --key-spec AES_256 --output json | ConvertFrom-Json
"plaintext data key: $([Convert]::FromBase64String($dk.Plaintext).Length) bytes, encrypted copy: $([Convert]::FromBase64String($dk.CiphertextBlob).Length) bytes"
Remove-Variable dkYou should see something like this:
db_password=Saat-Lab-Not-Real-123
plaintext data key: 32 bytes, encrypted copy: 184 bytesDecrypt didn’t need the key ID, because the ciphertext records which key made it. The data key is the thing S3 and EBS work with: you’d encrypt a file with the 32-byte key, keep the 184-byte encrypted copy next to it, and throw the plaintext away.
3Use the key for an S3 bucket
Paste this whole block:
aws s3api create-bucket --bucket $bucket --bucket-namespace account-regional --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1 | Out-Null
'{ "Rules": [ { "ApplyServerSideEncryptionByDefault": { "SSEAlgorithm": "aws:kms", "KMSMasterKeyID": "KEY_ARN" }, "BucketKeyEnabled": true } ] }' -replace 'KEY_ARN', $keyArn | Set-Content kms-enc.json
aws s3api put-bucket-encryption --bucket $bucket --server-side-encryption-configuration file://kms-enc.json
Set-Content oct.csv "employee,net_pay"
aws s3 cp oct.csv "s3://$bucket/payroll/oct.csv" | Out-Null
aws s3api head-object --bucket $bucket --key payroll/oct.csv --query "[ServerSideEncryption,SSEKMSKeyId]" --output text
aws kms enable-key-rotation --key-id $key --rotation-period-in-days 365
aws kms get-key-rotation-status --key-id $key --query "[KeyRotationEnabled,RotationPeriodInDays]" --output textYou should see something like this:
aws:kms arn:aws:kms:ap-south-1:111122223333:key/1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d
True 365The payroll object is now protected by your own key and not by the AWS managed aws/s3 key from Lesson 17. Rotation will create new key material every year, and neither the bucket nor the apps will notice.

4See every use in CloudTrail
Paste the whole block. KMS events normally show up within five minutes:
do { Start-Sleep 30; $e = aws cloudtrail lookup-events --lookup-attributes "AttributeKey=ResourceName,AttributeValue=$keyArn" --max-results 20 --query "Events[].EventName" --output text } until ($e -match 'Decrypt')
$eYou should see something like this:
GenerateDataKey Decrypt GenerateDataKey Encrypt EnableKeyRotation CreateAlias CreateKeyEvery encrypt, decrypt and data key request becomes a CloudTrail event that names the caller, and auditors like customer managed keys for exactly that reason, since you can prove who read the payroll data.
✅ Checkpoint: alias/saat-data exists with the root statement in its policy, you decrypted the secret, payroll/oct.csv is using your key, rotation is True at 365 days, and CloudTrail shows the KMS calls. Keep it all for the break lab.
Break It, Then Fix It
Here are two KMS problems, where one breaks everything at once and the other quietly breaks a single user.
1Break it: disable the key
Disable the key and then try to decrypt:
aws kms disable-key --key-id $key
aws kms decrypt --ciphertext-blob fileb://secret.enc --query Plaintext --output textYou’ll get this error back:
An error occurred (DisabledException) when calling the Decrypt operation: arn:aws:kms:ap-south-1:111122223333:key/1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d is disabled.At work, this same key might be protecting an RDS database, some EBS volumes and a backup vault, and disabling it takes all of them down instantly: instances won’t start, databases can’t read their storage and restores fail. Teams sometimes disable “unused” keys during a cleanup without checking CloudTrail first.
Fix: enable it again, and before you disable or delete any key, check how it’s been used recently:
aws kms enable-key --key-id $key
$pt = aws kms decrypt --ciphertext-blob fileb://secret.enc --query Plaintext --output text
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($pt))You should see this:
db_password=Saat-Lab-Not-Real-1232Break it: S3 access without kms:Decrypt
Create a reader role that can read the bucket but has no KMS permission, and download as that role:
'{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "MY_ARN" }, "Action": "sts:AssumeRole" } ] }' -replace 'MY_ARN', $myArn | Set-Content reader-trust.json
$roleArn = aws iam create-role --role-name saat-kms-reader --assume-role-policy-document file://reader-trust.json --query Role.Arn --output text
'{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::BUCKET", "arn:aws:s3:::BUCKET/*" ] } ] }' -replace 'BUCKET', $bucket | Set-Content reader-s3.json
aws iam put-role-policy --role-name saat-kms-reader --policy-name read-payroll --policy-document file://reader-s3.json
aws configure set role_arn $roleArn --profile saat-kms-reader
aws configure set source_profile saat-admin --profile saat-kms-reader
aws configure set region ap-south-1 --profile saat-kms-reader
Start-Sleep 15
aws s3 cp "s3://$bucket/payroll/oct.csv" - --profile saat-kms-readerYou’ll get an error like this:
download failed: s3://saat-kms-111122223333-ap-south-1-an/payroll/oct.csv to - An error occurred (AccessDenied) when calling the GetObject operation: User: arn:aws:sts::111122223333:assumed-role/saat-kms-reader/botocore-session-1760971234 is not authorized to perform: kms:Decrypt on resource: arn:aws:kms:ap-south-1:111122223333:key/1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d because no identity-based policy allows the kms:Decrypt action
S3 said yes and KMS said no. In Lesson 17 the reader didn’t need this, because the AWS managed key lets anyone in the account use it through S3, but your own key follows your own rules.
Fix: give the reader kms:Decrypt on this key:
'{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::BUCKET", "arn:aws:s3:::BUCKET/*" ] }, { "Effect": "Allow", "Action": "kms:Decrypt", "Resource": "KEY_ARN" } ] }' -replace 'BUCKET', $bucket -replace 'KEY_ARN', $keyArn | Set-Content reader-s3.json
aws iam put-role-policy --role-name saat-kms-reader --policy-name read-payroll --policy-document file://reader-s3.json
Start-Sleep 10
aws s3 cp "s3://$bucket/payroll/oct.csv" - --profile saat-kms-readerYou should see this:
employee,net_payNow clean up. You can’t delete a key on the spot; you schedule the deletion with a waiting period, and you can cancel it during that time:
aws s3 rm "s3://$bucket" --recursive | Out-Null
aws s3 rb "s3://$bucket"
aws iam delete-role-policy --role-name saat-kms-reader --policy-name read-payroll
aws iam delete-role --role-name saat-kms-reader
aws kms delete-alias --alias-name alias/saat-data
aws kms schedule-key-deletion --key-id $key --pending-window-in-days 7 --query "[KeyState,DeletionDate]" --output text
Remove-Item secret.enc, oct.csv, kms-enc.json, reader-trust.json, reader-s3.json
notepad $HOME\.aws\configThe schedule command shows something like this:
PendingDeletion 2026-10-27T20:15:00+05:30Open Notepad, delete the lines under [profile saat-kms-reader] and save the file.
✅ Checkpoint: You watched DisabledException stop every use of the key, and fixed a read that S3 allowed and KMS refused. The bucket, role and alias are gone, and the key is waiting to be deleted.
This happened at work
The cleanup that took down the database
It was a quarterly KMS key review at a fintech company, and the security team came across a key with a vague name, “temp-migration-key”, created two years before. Nobody remembered what it was for, so on a Thursday evening they disabled it as the first step towards deleting it. Within minutes the reporting database went into an inaccessible state, because it had been restored from a migration snapshot two years earlier and was still encrypted with that key, and three batch servers wouldn’t start because their EBS volumes used it as well. The reports for regulators went out late the next morning. They re-enabled the key and things came back. Now, before anyone disables a key, they have to check CloudTrail for its use in the last 90 days and list the resources that reference it; every key needs owner and purpose tags, and deletion always gets the full 30-day waiting period with an alarm on any use of a key that’s pending deletion.
Common mistakes and troubleshooting
| Mistake | What happens | What to do |
|---|---|---|
| Granting S3 access but not kms:Decrypt | AccessDenied on GetObject for SSE-KMS objects | Grant kms:Decrypt on the key to readers |
| Disabling or deleting a key without checking use | Databases, volumes and restores fail | Check CloudTrail and resource references first |
| Removing the root statement from the key policy | IAM policies stop working; possible lockout | Keep it unless you deliberately manage access in the key policy |
| One key for everything | No separation of who can decrypt what | Keys per data class or team |
| Short deletion window on important keys | Data lost forever after 7 days | Use 30 days and alarm on use of pending keys |
| Forgetting cross-account KMS access | Shared snapshots or backups can’t be used | Allow the other account in the key policy and its IAM |
Interview questions
Click a question to open it. Each one is phrased the way an interviewer would ask it, and under it you’ll find why they ask it and one way you could answer in your own words.
Fresher level
1. Can you explain in simple words what AWS KMS is?
Why they ask this: Nearly every service you touch uses KMS somewhere, so they want to know you understand how encryption keys are managed in AWS.
How you might answer: KMS is a managed service where you create and control encryption keys, and the keys never leave KMS unencrypted, so nobody can copy them out. Every time a key is used, KMS checks the key policy and IAM and logs the call in CloudTrail, so I can always see who used which key.
2. What’s the difference between an AWS managed key and a customer managed key, and when would you pick each?
Why they ask this: They’re checking you know how much control each key type gives you, because that choice affects sharing, auditing and cost.
How you might answer: An AWS managed key like aws/s3 is created and controlled by AWS with a fixed policy. A customer managed key is mine, so I write the key policy, control rotation and lifecycle, and pay a monthly fee for it. For simple cases the AWS managed key is fine, but if I need to share data with another account or decide exactly who can decrypt, I’d go with a customer managed key.
3. How would you explain envelope encryption to a new team member?
Why they ask this: They want to see you understand how KMS works underneath, which is what explains how it can encrypt large data quickly.
How you might answer: I’d say the data gets encrypted with a data key, and then that data key gets encrypted with the KMS key, so only the small encrypted data key goes back to KMS when you need to decrypt. It’s a bit like locking your valuables in a box and then locking the box key in a safe that only KMS can open.
2–5 years experience
4. A user tells you they can list a bucket but get AccessDenied when they download an object. What’s likely going on?
Why they ask this: They want to see you look beyond S3 permissions, since missing KMS access is one of the most common causes of this confusing error.
How you might answer: I’d check how the objects are encrypted. If they’re SSE-KMS with a customer managed key, the user needs kms:Decrypt on that key as well as S3 read permission, and that has to come from the key policy, or from IAM if the key policy delegates to IAM.
5. Your security team asks you to turn on key rotation. Will that break any applications?
Why they ask this: People often worry about rotation without knowing how it works, so they’re checking you understand what it changes.
How you might answer: It shouldn’t. When a KMS key rotates, KMS creates new key material and uses it for new encryption, but it keeps the old material to decrypt older data, and the key ID and ARN stay the same, so the applications don’t need any changes.
6. Another team needs a copy of an encrypted EBS snapshot from your account. How would you share it with them?
Why they ask this: They want to see you know cross-account sharing needs both the snapshot and the key, and that you understand where AWS managed keys fall short.
How you might answer: I’d share the snapshot with their account and update the customer managed key’s policy so their account can use it, with Decrypt, CreateGrant and DescribeKey. If it was encrypted with aws/ebs it can’t be shared at all, so I’d copy it first and re-encrypt the copy with a customer managed key.
Senior level
7. You join a regulated company with no clear plan for encryption keys. How would you design a KMS key strategy?
Why they ask this: They’re looking for governance thinking (separation of duties, protection against mistakes, auditing) and whether you can design for regulators and not only for engineers.
How you might answer: I’d create keys for each data classification and environment, with key policies that keep key administrators separate from key users, and rotation switched on. Keys for shared data would sit in a security account, and every key would carry owner and purpose tags. I’d also add SCPs so only a break-glass role can delete keys, plus CloudTrail alerts on DisableKey and ScheduleKeyDeletion.
8. You find out someone has scheduled a production KMS key for deletion. What do you do?
Why they ask this: They want a calm, quick incident response, where you stop the damage first and then make sure it can’t happen again.
How you might answer: I’d cancel the deletion straight away with cancel-key-deletion and re-enable the key so everything using it works again. Then I’d find every resource using the key through CloudTrail and resource configurations, work out who did it and why, and add guardrails like SCPs and alarms so it can’t happen quietly again.
Quick quiz
Q1. Why did the reader fail to download the payroll file?
- No S3 permission
- No kms:Decrypt on the key
- The bucket was public
- The key was rotated
Show answer
B — SSE-KMS objects need both S3 and KMS permissions.
Q2. What does disabling a KMS key do?
- Nothing until deletion
- Immediately blocks all use of the key
- Deletes the data
- Rotates the key
Show answer
B — It is an instant outage for anything using the key.
Q3. What does the default key policy’s root statement do?
- Gives root user only
- Lets IAM policies in the account grant access
- Makes the key public
- Disables rotation
Show answer
B — It delegates access control to IAM.
Q4. What is the minimum waiting period for key deletion?
- None
- 1 day
- 7 days
- 90 days
Show answer
C — 7 to 30 days, so mistakes can be cancelled.
Q5. In envelope encryption, what is stored next to the data?
- The plaintext data key
- The encrypted data key
- The KMS key
- Nothing
Show answer
B — The encrypted data key is decrypted by KMS when needed.
Quick recap
- KMS keys never leave KMS, and services use envelope encryption with data keys.
- The root statement in the key policy is what lets IAM grant use of the key.
- To read SSE-KMS data you need kms:Decrypt as well as S3 permissions.
- Disabling a key is an instant outage, so check how it’s used before you do it.
- Rotation keeps the same key ID, and deletion waits 7 to 30 days.
Next lesson
Lesson 31: Secrets Manager and Parameter Store → Next you’ll store database passwords and settings the right way, read them from a server, and rotate a secret with no downtime.
Further reading: AWS KMS keys
Want to go deeper into AWS KMS keys? Read AWS Key Management Service and Key policies in AWS KMS. You can also see the full plan for this course on the AWS Cloud Administration course page.
