Lesson 1 of 3 Data Protection and Encryption

Managing Encryption Keys with AWS KMS

AWS Key Management Service (AWS KMS) creates and controls the keys that most AWS services use to encrypt data. Its access controls decide who can use data, independently of who can read the storage. This lesson explains key types, envelope encryption, key policies and grants, encryption context, rotation, multi-Region keys, imported material and custom key stores, and key deletion.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish between AWS owned, AWS managed and customer managed keys and between symmetric and asymmetric keys.
  • Explain envelope encryption and how AWS services use KMS data keys.
  • Explain key policies, grants, the kms:ViaService condition and encryption context, including cross-account use.
  • Describe key rotation, multi-Region keys, imported key material, custom key stores and key deletion.

Key Types

KMS keys come in three ownership models. AWS owned keys are used by some services by default and are invisible to the customer. AWS managed keys, with aliases such as aws/s3 and aws/ebs, are created in the account for a service, are used only by that service, rotate yearly automatically and cannot have their policies changed or be used across accounts. Customer managed keys are created and controlled by the customer: their key policies, grants, rotation and deletion are configurable, they can be shared with other accounts, and they are required whenever control over access or cross-account use is needed.

Keys can be symmetric (AES-256, the default, used by AWS services; the key material never leaves KMS unencrypted), asymmetric (RSA, elliptic curve and post-quantum ML-DSA for signing, or RSA for encryption, where the public key can be downloaded) or HMAC keys for generating and verifying message authentication codes. Keys are Regional unless they are multi-Region keys.

Envelope Encryption

KMS encrypts at most 4 KB of data directly. For larger data, services use envelope encryption: they call GenerateDataKey, which returns a data key in plaintext and encrypted under the KMS key; they encrypt the data locally with the plaintext data key, discard the plaintext key, and store the encrypted data key with the data. To decrypt, they send the encrypted data key to KMS with Decrypt, which checks permissions and returns the plaintext data key. The KMS key never leaves KMS, data never travels to KMS, and access to the data depends on permission to use the KMS key. S3 Bucket Keys reduce KMS requests by using a bucket-level data key for many objects.

The following diagram shows the encrypt and decrypt paths.

Envelope encryption with AWS KMS. To encrypt, a service calls GenerateDataKey and receives a data key in plaintext and encrypted under the KMS key. It encrypts the data locally with the plaintext key, discards that key and stores the encrypted data key with the encrypted data. To decrypt, it sends the encrypted data key to KMS Decrypt, which checks permissions and returns the plaintext data key. The KMS key never leaves KMS, and the data never goes to KMS.
Only the small data key travels to and from KMS.

Key Policies, Grants and Conditions

Every KMS key has a key policy, a resource-based policy that is the primary access control. Unlike most resources, a KMS key can be used only if its key policy allows it, directly or by delegating to IAM: the default key policy includes a statement giving the account root principal full access, which enables IAM policies in the account to grant permissions on the key. Without that statement, IAM policies have no effect, and a key whose policy names no usable principal becomes unmanageable. Key policies typically separate key administrators, who can manage but not use the key, from key users, who can encrypt and decrypt.

Grants give temporary, delegated permissions on a key to a principal, often created by AWS services on the customer's behalf, for example EBS creating a grant so that an instance can use an encrypted volume. The condition key kms:ViaService restricts a key to use through a specific service, such as allowing decryption only when the request comes through S3 in a given Region. Encryption context is non-secret key-value data bound to a ciphertext; decryption requires the same context, it appears in CloudTrail logs for auditing, and policies can require it, as shown for CloudWatch Logs in the logging module.

Cross-account use requires both sides: the key policy in the key's account must allow the external account or role, and the external principal's IAM policy must allow the KMS actions on the key ARN. AWS managed keys cannot be used across accounts, which is the root cause of many failures in cross-account pipelines, snapshot sharing and replication.

{
  "Sid": "AllowUseThroughS3FromProdAccount",
  "Effect": "Allow",
  "Principal": {"AWS": "arn:aws:iam::333333333333:role/ReportsReader"},
  "Action": ["kms:Decrypt", "kms:GenerateDataKey"],
  "Resource": "*",
  "Condition": {"StringEquals": {"kms:ViaService": "s3.eu-west-1.amazonaws.com"}}
}
Scenario questions

Control key use

Exam skills 6.2.4 · 6.1.1

Practise KMS key policies, grants and conditions.

Log in to try this activity Interactive activities are free with an account.

Rotation, Multi-Region Keys and Key Material

Automatic rotation of customer managed symmetric keys creates new key material on a schedule, configurable from 90 to 2,560 days (365 by default). The key ID, ARN and policy stay the same; KMS keeps old material to decrypt existing data and uses the new material for new encryption, so no re-encryption or application change is needed. On-demand rotation rotates immediately, and is also available for keys with imported key material. Asymmetric and HMAC keys do not support automatic rotation.

Multi-Region keys are sets of related keys in different Regions with the same key ID and key material, so data encrypted in one Region can be decrypted in another without re-encryption. They suit disaster recovery, global tables and client-side encryption of data that moves between Regions. Each replica has its own key policy.

With imported key material (bring your own key), the customer generates key material and imports it, retaining a copy and the ability to delete it from KMS immediately or set an expiry. Custom key stores keep key material outside standard KMS: an AWS CloudHSM key store in the customer's CloudHSM cluster, or an external key store in an HSM outside AWS, for requirements that the key material must reside in hardware the customer controls.

Disabling and Deleting Keys

Deleting a key makes all data encrypted under it permanently unrecoverable, so deletion requires a waiting period of 7 to 30 days (default 30), during which the key is unusable and deletion can be cancelled. A CloudWatch alarm on attempts to use a key pending deletion, found through CloudTrail, reveals whether something still depends on it. Disabling a key is reversible and is the safer first step. SCPs commonly deny kms:ScheduleKeyDeletion except for a security role.

FeatureBehaviour
Automatic rotationNew material every 90 to 2,560 days; same key ID; old data still decryptable
Multi-Region keysSame key material and ID across Regions
Imported materialCustomer-generated; can expire or be deleted immediately
Custom key storeKey material in CloudHSM or an external HSM
DeletionWaiting period of 7 to 30 days; irreversible afterwards
Fill in the value

Key lifecycle numbers

Exam skills 6.2.4

Recall the waiting periods and rotation range for KMS keys.

Log in to try this activity Interactive activities are free with an account.
Worked example

Sharing Encrypted Snapshots Across Accounts

A backup process must copy encrypted RDS snapshots from a production account to a recovery account. Sharing fails because the snapshots are encrypted with the AWS managed key aws/rds.

  1. Re-encrypt

    Copy the snapshot in the production account, re-encrypting it with a customer managed key, because AWS managed keys cannot be shared.

  2. Key policy

    Allow the recovery account to use the customer managed key (Decrypt, DescribeKey, CreateGrant).

  3. Share and copy

    Share the snapshot with the recovery account, which copies it and re-encrypts it with its own key, removing the dependency on the production key.

  4. Automate

    Use AWS Backup cross-account copy with customer managed keys for future copies.

Check yourself

An IAM policy grants a role kms:Decrypt on a customer managed key, but decryption fails with access denied. The key policy was written from scratch and lists only the key administrators. Why?

Show answer

The key policy does not allow the role and does not delegate to IAM through the account root principal statement, so IAM policies have no effect on that key. Add the role to the key policy or add the root delegation statement.

Exam Tips

Remember for the exam

  • AWS managed keys cannot be used across accounts or have their policies changed; use customer managed keys for control and sharing.
  • Key policies are the primary control; IAM policies work only if the key policy delegates to the account.
  • Envelope encryption uses data keys from GenerateDataKey; kms:ViaService restricts use to a service; encryption context must match for decryption.
  • Automatic rotation keeps the key ID (period 90 to 2,560 days); multi-Region keys share material across Regions.
  • Key deletion has a 7 to 30 day waiting period; disable first and alarm on use during the waiting period.