Lesson 1 of 8 AWS KMS and AWS CloudHSM

Controlling AWS KMS Keys with Key Policies and Grants

This lesson explains the AWS Key Management Service (AWS KMS) key types and how access to them is controlled, which is essential for the data protection domain, since KMS is at the centre of encryption on AWS.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish AWS owned, AWS managed and customer managed keys.
  • Distinguish symmetric, asymmetric and HMAC key specifications.
  • Explain how key policies, IAM policies and grants control access.
  • Restrict key use with kms:ViaService and encryption context conditions.

Key types by ownership

AWS KMS keys fall into three types according to who owns and controls them. AWS owned keys are used internally by AWS services to protect data in many accounts. You cannot see, manage or audit them, and they are free. AWS managed keys, with aliases such as aws/s3 and aws/ebs, are created in your account when a service first needs one. AWS rotates them every year, and you can see their use in CloudTrail, but you cannot change their key policy, so they cannot be shared with other accounts.

Customer managed keys are created and controlled by you. You write the key policy and grants, choose rotation, schedule deletion and allow cross-account use, for a monthly fee per key. Whenever a scenario requires sharing encrypted data across accounts, controlling who can decrypt, or controlling rotation, the answer is a customer managed key. The following table summarises the types.

TypeWho controls itNotes
AWS owned keysAWS, used internallyInvisible to you; free
AWS managed keys (aws/s3, aws/ebs)AWS, in your accountRotated yearly; key policy cannot be changed; cannot be shared across accounts
Customer managed keysYouKey policy, grants, rotation, deletion, cross-account use; monthly fee per key
Sort into groups

Which kind of KMS key?

Exam skills 5.3.5 · 5.2.1

Decide which KMS key type each statement describes.

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

Key specifications

A key's specification decides what it can do. A symmetric key (AES-256) is the default and is what AWS service integrations use. The same key encrypts and decrypts, and its material never leaves KMS unencrypted, so every use is a KMS API call subject to the key policy and logged in CloudTrail. An asymmetric key pair (RSA, elliptic curve or ML-DSA) has a public key that can be downloaded. Parties outside AWS can then encrypt data, or verify signatures, without calling KMS, while the private key stays in KMS. An HMAC key generates and verifies message authentication codes, which prove a message has not been altered. The following table summarises the specifications.

SpecUse
Symmetric (AES-256)Default; never leaves KMS unencrypted; used by AWS service integrations
Asymmetric (RSA, ECC, ML-DSA)Encryption or signing outside AWS
HMACMessage authentication codes
Match the pairs

Key specifications

Exam skills 5.3.5

Match each KMS key specification with its use.

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

Controlling access

Access to KMS keys is controlled by key policies, IAM policies and grants. The following list describes how these controls are applied.

  • Key policies are the primary control and every key has one. The default policy allows the account root, which is what lets IAM policies grant access; without it, IAM policies have no effect.
  • Separate key administrators (manage) from key users (encrypt and decrypt).
  • Grants give temporary, delegated permissions, often created by services (e.g. EBS so an instance can decrypt a volume).
  • Condition keys: kms:ViaService (only through a given service and Region), kms:CallerAccount, kms:EncryptionContext:*, kms:GrantIsForAWSResource.

The following diagram shows what each principal can do under the key policy of a KMS key that protects payment data.

What each principal can do under the payments key policy. Key administrators manage the key but hold no cryptographic actions; the application role may encrypt and decrypt only when the request comes through DynamoDB (kms:ViaService) for an AWS resource grant, so the same credentials cannot decrypt directly with the KMS API.
The scenario is illustrative.
Match the pairs

KMS condition keys and controls

Exam skills 5.3.5 · 4.2.3

Match each KMS control with what it does.

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

Envelope encryption

Envelope encryption uses a data key to encrypt data locally and a KMS key to protect the data key. The process consists of the following steps.

  1. GenerateDataKey returns a plaintext data key and the same key encrypted under the KMS key.
  2. The plaintext data key encrypts data locally and is discarded.
  3. The encrypted data key is stored with the data; decrypting it later needs KMS.

Every KMS API call is logged in CloudTrail, auditing key use.

Put in order

Envelope encryption

Exam skills 5.2.1

Arrange the steps of envelope encryption with KMS.

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

A key usable only through one service and one context

A payments application stores card tokens in DynamoDB, encrypted with a customer managed key. The security team wants the application role to use the key only through DynamoDB, so the same credentials cannot be used to decrypt data directly with the KMS API, and wants key administrators unable to decrypt anything:

{
  "Version": "2012-10-17",
  "Statement": [
    { "Sid": "RootDelegation", "Effect": "Allow",
      "Principal": { "AWS": "arn:aws:iam::111111111111:root" }, "Action": "kms:*", "Resource": "*" },
    { "Sid": "AdminsManageButCannotUse", "Effect": "Allow",
      "Principal": { "AWS": "arn:aws:iam::111111111111:role/KeyAdmins" },
      "Action": ["kms:Describe*", "kms:List*", "kms:Get*", "kms:Put*", "kms:Update*", "kms:Enable*",
                 "kms:Disable*", "kms:TagResource", "kms:UntagResource",
                 "kms:ScheduleKeyDeletion", "kms:CancelKeyDeletion"],
      "Resource": "*" },
    { "Sid": "AppViaDynamoDBOnly", "Effect": "Allow",
      "Principal": { "AWS": "arn:aws:iam::111111111111:role/PaymentsApp" },
      "Action": ["kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey*", "kms:DescribeKey", "kms:CreateGrant"],
      "Resource": "*",
      "Condition": {
        "StringEquals": { "kms:ViaService": "dynamodb.eu-west-1.amazonaws.com", "kms:CallerAccount": "111111111111" },
        "Bool": { "kms:GrantIsForAWSResource": "true" } } }
  ]
}

The root delegation statement means IAM policies can still grant access to others, so it is common to pair this key policy with an SCP or permissions boundary that prevents broad kms:* grants. CloudTrail records every decryption, including the DynamoDB table in the encryption context, giving a complete audit of access to card tokens.

Check yourself

How can a key be restricted to use through one AWS service?

Show answer

With the kms:ViaService condition in the key policy.

Exam Tips

Remember for the exam

  • To use a key across accounts, use a customer managed key whose key policy names the other account.
  • To restrict a key to use through one service, use the kms:ViaService condition.
  • The policy of aws/s3 cannot be edited or shared because it is AWS managed; use a customer managed key instead.
  • IAM policies work for a key only if the key policy allows the account.
  • To audit every use of a key, rely on CloudTrail's logging of KMS API calls.
  • To encrypt large amounts of data efficiently, use envelope encryption with data keys.