Skip to content
Tutorials Center
My learning

Topics

Lesson 49 of 70 ยท Security

Encryption and KMS

Learn envelope encryption, KMS keys, key policies, grants, ACM certificates and encryption at rest and in transit, with a runnable envelope demo.

Encryption turns readable data into scrambled bytes that only someone holding the right key can read again. On AWS it is almost free to turn on, so the real questions are not "should I encrypt?" but "who controls the keys?" and "who is allowed to use them?". Those questions are what AWS Key Management Service (KMS) answers.

This lesson explains envelope encryption (the idea behind nearly every encrypted AWS service), the kinds of KMS keys, how key policies and grants decide who may use a key, and how certificates from AWS Certificate Manager (ACM) protect data in transit. You met encryption options for buckets in S3 Security and for volumes in EBS Volumes and Snapshots; here you see what happens underneath.

KMS and ACM cannot run on this site: all AWS commands and policies below are read-only. The runnable examples use Python's standard library to build a toy envelope-encryption scheme. The toy cipher is for learning only and is NOT secure; never use it for real data. The real KMS keeps key material inside hardware security modules and never shows it to you.

Two places to encrypt: at rest and in transit

  • Encryption at rest protects stored data (disks, snapshots, buckets, databases, backups). If someone gets the raw storage, they only see ciphertext.
  • Encryption in transit protects data moving over a network, using TLS (the "S" in HTTPS). Someone watching the wire sees only ciphertext.

You usually want both. Encryption at rest does nothing for a request an attacker can read on the network, and TLS does nothing for a stolen disk. Also remember what encryption does not do: if your application is allowed to read the data, so is anyone who steals the application's permissions. Encryption moves the question from "who can reach the bytes?" to "who can use the key?", which is easier to control and audit.

Envelope encryption

KMS can encrypt at most 4 KB of data directly. A video, a database or a disk is much bigger, and sending gigabytes to a remote key service would also be slow. So AWS uses envelope encryption:

  1. Ask KMS for a fresh data key (a random key just for this object).
  2. KMS returns two copies: the plaintext data key and the same key encrypted under your KMS key.
  3. Your service encrypts the data locally with the plaintext data key, then throws the plaintext key away.
  4. It stores the encrypted data key next to the ciphertext.
  5. To decrypt, it sends the encrypted data key to KMS. KMS checks permissions, decrypts the key and returns it. The service decrypts the data locally.
sequenceDiagram
    participant App as Your service
    participant KMS as KMS
    App->>KMS: GenerateDataKey (key id)
    KMS-->>App: plaintext data key + encrypted data key
    App->>App: encrypt data with plaintext key, discard it
    App->>App: store ciphertext + encrypted data key
    Note over App,KMS: Later, to read the data
    App->>KMS: Decrypt (encrypted data key)
    KMS-->>App: plaintext data key (if allowed)
    App->>App: decrypt data locally

Only a small key travels to KMS. The bulk data never leaves your service.

The design has a nice consequence: the KMS key is the control point. If you disable the KMS key or deny a role kms:Decrypt, every encrypted data key becomes useless and the data becomes unreadable, even though the ciphertext still sits in S3 or EBS.

This is a toy version you can run. It builds a fake KMS with one master key, wraps data keys with it, and encrypts the data with a keystream made from HMAC-SHA256:

Python Toy envelope encryption with a fake KMS

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Run it and notice the last lines: the ciphertext is unchanged, yet disabling the master key made it unreadable. Real KMS behaves the same way, and every call is logged to CloudTrail (see CloudTrail, Config and X-Ray).

KMS key types

A KMS key (older name: customer master key) is a logical container for key material. Pick by who manages it:

Type Who creates and manages it Notes
AWS owned key AWS, shared across accounts Invisible to you, free, no audit trail of your own. Default for some services.
AWS managed key AWS, in your account, e.g. aws/s3 Visible in KMS, rotated by AWS about yearly, policy cannot be edited.
Customer managed key You You write the key policy, can disable, schedule deletion and enable rotation. Small monthly fee plus per-request fee.

Most keys are symmetric (one key encrypts and decrypts, AES-256-GCM) and that is what AWS services use. Asymmetric KMS keys hold a public/private pair (RSA or elliptic curve) for signing or for encryption by parties outside AWS; the private half never leaves KMS. A key is tied to a Region. Multi-Region keys are linked copies with the same key id so data encrypted in one Region can be decrypted in another.

Create a customer managed key and give it a friendly alias (read-only):

Terminal
aws kms create-key --description "orders-db encryption key"
aws kms create-alias --alias-name alias/orders-db --target-key-id 1234abcd-12ab-34cd-56ef-1234567890ab
aws kms enable-key-rotation --key-id alias/orders-db

With rotation on, KMS creates new key material automatically each year. Old material is kept so existing ciphertext still decrypts, and your applications change nothing. Deleting a key is deliberately slow: you schedule deletion with a waiting period of 7 to 30 days, because deleting it destroys every piece of data wrapped by it.

Who may use a key: key policies, IAM and grants

Every KMS key has a key policy, a resource-based policy. Unlike most AWS resources, a KMS key does not trust IAM automatically. Access works only if the key policy allows it, either directly or by delegating to IAM. The default policy created in the console contains a statement like this:

JSON
{
  "Sid": "EnableIAMPolicies",
  "Effect": "Allow",
  "Principal": { "AWS": "arn:aws:iam::111122223333:root" },
  "Action": "kms:*",
  "Resource": "*"
}

That statement means "this account's IAM policies may grant access to this key". Without it, only principals named in the key policy can use the key, and you can even lock yourself out. A better pattern for sensitive keys separates duties:

JSON
{
  "Sid": "AllowAppToUseKey",
  "Effect": "Allow",
  "Principal": { "AWS": "arn:aws:iam::111122223333:role/orders-app" },
  "Action": ["kms:Decrypt", "kms:GenerateDataKey"],
  "Resource": "*",
  "Condition": {
    "StringEquals": { "kms:ViaService": "s3.eu-west-1.amazonaws.com" }
  }
}

The kms:ViaService condition means the role may use the key only when S3 calls KMS on its behalf, not when someone calls KMS directly. Key administrators (who manage the key) and key users (who use it) should be different roles. The evaluation rules are the same ones from How Policy Evaluation Works; note that cross-account use needs an allow in both the key policy and the caller's IAM policy.

A grant is a temporary, narrow permission attached to a key. AWS services use grants behind the scenes (for example EBS grants EC2 the right to decrypt a volume key when an instance starts) and you can create one with aws kms create-grant. Grants can be retired or revoked without editing the key policy.

Encryption in AWS services

Turning encryption on is usually one setting:

  • S3: SSE-S3 (default, S3-managed keys) or SSE-KMS with your key. A bucket key setting reduces the number of KMS requests, and so the bill.
  • EBS: encrypt a volume and all of its snapshots with a KMS key. You can enable "encrypt new volumes by default" per Region.
  • RDS: choose a KMS key at creation. You cannot encrypt an existing unencrypted database in place; you restore from an encrypted snapshot copy instead.
  • DynamoDB, SQS, SNS, Lambda environment variables: all support KMS keys.

Each KMS request is a billable API call and is subject to a quota per second, so very chatty workloads (millions of tiny objects) should use bucket keys or data key caching.

Encryption in transit and ACM

TLS needs a certificate that proves the server's identity. AWS Certificate Manager (ACM) issues and renews public certificates for free, for use with load balancers, CloudFront and API Gateway:

Terminal
aws acm request-certificate \
  --domain-name www.example.com \
  --subject-alternative-names example.com \
  --validation-method DNS

ACM asks you to prove you own the domain by creating a CNAME record (one click if the domain is in Route 53); after that certificates renew automatically. Two limits to remember: you cannot export the private key of a public ACM certificate, so it only works with integrated AWS services, and CloudFront needs its certificate in the us-east-1 Region.

To enforce TLS, attach a policy that denies plain HTTP, for example an S3 bucket policy with the condition "aws:SecureTransport": "false" and an effect of Deny. Load balancers can redirect port 80 to 443 (Elastic Load Balancing).

One more check on the model: the following stand-in answers "can this caller use this key?" the way a simplified key policy plus IAM check does.

Python A tiny key-policy check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the separation: the application can decrypt but cannot manage or delete the key, and the administrator can manage the key but, with this policy, cannot decrypt data. That split is the point of key policies.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In envelope encryption, what does KMS actually encrypt and decrypt?

    KMS wraps and unwraps small data keys. The service encrypts the bulk data itself with the plaintext data key and stores only the encrypted data key next to it.
  2. 2. You disable a customer managed KMS key. What happens to data encrypted under it?

    The encrypted data keys can no longer be unwrapped, so the data is unreadable until the key is enabled again. Nothing is deleted or re-encrypted.
  3. 3. Why does AWS make deleting a KMS key a scheduled action with a waiting period?

    The waiting period is 7 to 30 days. During it the key is unusable but recoverable, which protects against destroying data by accident.
  4. 4. Which statement about ACM public certificates is correct?

    ACM public certificates are free and renew automatically, but the private key cannot be exported, so they only work with integrated AWS services. They protect data in transit, not at rest.

Summary

  • Encrypt both at rest and in transit; encryption moves control from "who can reach the bytes" to "who can use the key".
  • Envelope encryption: KMS creates and unwraps small data keys, your service encrypts the bulk data locally.
  • Key types: AWS owned, AWS managed and customer managed (you control policy, rotation and deletion); keys are Regional unless multi-Region.
  • Key policies are the first gate for a key; IAM only works if the key policy delegates to it. Separate key administrators from key users.
  • Grants give narrow temporary access, often used by AWS services on your behalf.
  • ACM issues free, auto-renewing public certificates for integrated services; the private key cannot be exported.
  • Bucket keys and data key caching reduce KMS request costs.

Next: Secrets, WAF, Shield and GuardDuty