Sub topic 1
What KMS is and what it protects
AWS Key Management Service (KMS) creates, stores, and uses cryptographic keys for you. Your code asks KMS to perform an encryption operation, and KMS returns the result without revealing the key. This chapter sets out what KMS does, what it leaves to you, and the vocabulary the rest of the tutorial relies on.
Prerequisites
This chapter assumes you already know:
- Basic AWS account and IAM concepts: users, roles, and policies.
- How to run the AWS CLI with working credentials and a default Region. Python with
boto3helps for one example. - What symmetric encryption does, and the difference between plaintext (readable data) and ciphertext (encrypted data).
You do not need to have created a KMS key yet. The next chapters cover that.
The problem KMS solves
Encrypting data is easy. Managing the key is hard. If you encrypt a database backup with a key stored next to it, anyone who can read the backup can read the key. If you keep keys in application config, every developer and every log line becomes a possible leak.
A key manager separates two things:
- The data, which lives in your application, S3, a database, or a queue.
- The key, which lives in a hardened service. Access to it is controlled and recorded separately.
KMS is that service. Its main properties are:
- Key material stays inside KMS. For a symmetric KMS key, the plaintext key material is never exported. It is generated in, and used only inside, hardware security modules (HSMs) that AWS operates. These are validated under the FIPS 140 standard.
- Every use is an API call. Each use of a key is an authenticated, authorized request. That gives you one place to allow, deny, and audit key use.
- Keys are regional. A KMS key exists in one Region. A key in
eu-west-1cannot be used from a request tous-east-1. Multi-Region keys are a separate feature, covered later. - KMS does not store your data. It stores keys and key metadata. You or another AWS service stores the ciphertext.
KMS key versus data key
Two kinds of keys appear throughout this tutorial, and mixing them up is the most common early mistake.
| Row | KMS key | Data key |
|---|---|---|
| What it is | A logical key resource in KMS, identified by key ID or ARN | A key that encrypts your actual data |
| Where the material lives | Inside KMS HSMs, never exported in plaintext | In your application's memory while in use |
| Who stores it | KMS | You store only its encrypted copy, next to the data |
| Typical size of data it handles | Up to 4,096 bytes per direct call | Any size, using a local cipher such as AES-GCM |
| Created by | `CreateKey` | `GenerateDataKey` |
A KMS key (older documents call it a customer master key, or CMK) is the top-level key. Its key material is held by KMS. The same key can be backed by newer material after rotation, but its ARN stays the same.
A data key is a normal symmetric key that KMS generates on request. You use it locally to encrypt a large payload, then discard the plaintext copy and keep only the version encrypted under a KMS key. This pattern is called envelope encryption, and a later chapter covers it in detail. For now, remember that KMS usually protects the keys that protect your data, not the data itself.
What a request looks like
When an application encrypts a small value directly, the flow is as follows.
Two details matter here:
- The authorization check happens on every call. If the caller lacks permission on that specific key, the call fails with
AccessDeniedException, even if the caller is an administrator elsewhere in the account. - The ciphertext KMS returns records which key produced it. Decrypting a symmetric ciphertext therefore does not require you to name the key, although the caller still needs permission to use it.
What KMS protects, and what it does not
KMS is often credited with more than it does. Be precise about the boundary.
KMS protects:
- Key secrecy. Plaintext KMS key material cannot be read by you, by other tenants, or through the API.
- Data at rest, when combined with a service or your code. S3, EBS, RDS, DynamoDB, Secrets Manager, and many other services can encrypt your data under a KMS key. Your own code can do it through the API.
- Control over who may decrypt. Removing a principal's permission, or disabling the key, stops decryption even though the ciphertext still exists.
- An audit trail. KMS calls are logged to AWS CloudTrail. A later chapter covers this.
KMS does not protect:
- Plaintext in your application. After a successful decrypt, the plaintext is in your process memory, logs, and anywhere you write it. A caller who is allowed to decrypt gets the plaintext.
- Data from a caller with both access to the ciphertext and permission to decrypt. Encryption does not replace access control. It adds a second lock.
- Integrity of your whole system. If an attacker can change your IAM policies or your key policy, they can grant themselves use of the key.
- Data you never encrypted. KMS does nothing unless something calls it.
> KMS answers "who may use this key, and when?" It does not answer "is my application secure?"
Note that the > above is not used. Instead, here is the point as a callout:
Looking at KMS in your own account
Before creating anything, list what already exists. Many accounts already contain AWS managed keys that services created on your behalf.
# Aliases are friendly names; AWS managed keys use the alias/aws/ prefix
aws kms list-aliases --query 'Aliases[].AliasName' --output text
# Inspect one key by the ID or ARN returned from list-keys
aws kms describe-key \
--key-id <key-id> \
--query 'KeyMetadata.{Arn:Arn,Manager:KeyManager,State:KeyState,Usage:KeyUsage,Spec:KeySpec}' \
--output tableIn the describe-key output, KeyManager is AWS for AWS managed keys and CUSTOMER for keys you created. KeyState should be Enabled for a key in normal use. KeyUsage is ENCRYPT_DECRYPT for the symmetric keys used in this tutorial. The next chapters explain each of these values.
The same check in Python:
import boto3
kms = boto3.client("kms")
for page in kms.get_paginator("list_keys").paginate():
for entry in page["Keys"]:
meta = kms.describe_key(KeyId=entry["KeyId"])["KeyMetadata"]
print(meta["KeyId"], meta["KeyManager"], meta["KeyState"], meta["KeySpec"])If you are using a new account and see no keys, that is normal. AWS managed keys appear only after a service first needs one.
Common errors at this stage
- Wrong Region.
NotFoundExceptionon a key you know exists usually means your CLI or SDK is pointed at a different Region. Checkaws configure get regionor pass--region. - Assuming IAM admin rights are enough. A key policy can restrict a key so that an account administrator cannot use it. The key policy chapter explains why.
- Encrypting large files directly. The direct
EncryptAPI accepts at most 4,096 bytes of plaintext. Larger data needs envelope encryption. - Confusing a key ID with a data key. A key ID such as
1234abcd-12ab-34cd-56ef-1234567890abnames a resource in KMS. It is not secret and it is not key material.
Where this fits in the tutorial
The chapters that follow build on this base in order:
- Who owns a key, and which kinds of key exist.
- How to name keys with IDs, ARNs, and aliases.
- Direct encryption, encryption context, and envelope encryption.
- Access control through key policies, IAM, and grants.
- Operating keys: service integration, rotation, deletion, auditing, and cost.
Exercise
An application decrypts a customer record with KMS and then writes the plaintext to its debug log. Has KMS failed to protect the data? Name what KMS did protect and what it did not.
Show answer
KMS did not fail. It protected the key material, enforced that only an authorized caller could decrypt, and recorded the call in CloudTrail. It does not control what the application does with plaintext after decryption. The log entry is an application problem, and the fix is to stop logging plaintext.
Exercise
A teammate wants to use aws kms encrypt to encrypt a 2 MB file in one call. Why will this fail, and which kind of key should do the bulk encryption instead?
Show answer
The direct Encrypt API accepts at most 4,096 bytes of plaintext, so a 2 MB file is rejected. The file should be encrypted locally with a data key from GenerateDataKey (envelope encryption), and KMS should protect only that data key.