Choosing and Controlling Access to AWS KMS Keys
This lesson explains how AWS Key Management Service (AWS KMS) creates and controls encryption keys that never leave the service unencrypted. A key policy decides who may use a key, and code refers to it by its ID, ARN or alias.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish AWS owned, AWS managed and customer managed keys.
- Distinguish symmetric and asymmetric keys and their uses.
- Explain how key policies, IAM policies and grants combine to control access to a key.
- Describe how aliases, multi-Region keys and the deletion waiting period work.
Types of KMS key
AWS KMS keys are classified by who creates and manages them, and the type decides how much control and visibility the customer has.
AWS owned keys belong to AWS and are shared across many accounts. A service uses them to encrypt data by default, as DynamoDB does for every table unless another key is chosen. They do not appear in the account, their use is not recorded in CloudTrail, and they cost nothing. AWS managed keys are created in the account by a service the first time it needs one, with aliases such as aws/s3 or aws/lambda. They are visible and their use is logged, but their key policy cannot be changed, so they cannot be shared with other accounts, and AWS rotates them every year.
Customer managed keys are created by the customer, who writes the key policy, decides which principals and accounts may use the key, enables or disables it, chooses the rotation period and can schedule its deletion. They cost a monthly fee per key plus request charges. The following table summarises the three types.
| Key type | Created by | You control the policy? | Rotation | Example |
|---|---|---|---|---|
| AWS owned | AWS, used internally by services across many accounts | No | Managed by AWS | Default DynamoDB encryption |
| AWS managed | AWS, per service in your account | No | Every year, automatically | aws/s3, aws/lambda |
| Customer managed | You | Yes | Optional, 90 to 2,560 days | alias/orders-key |
Customer managed keys cost a monthly fee per key plus requests, and are required when you need to control the key policy, share the key with another account or audit and disable it yourself.
Which type of key?
Symmetric and asymmetric keys
A KMS key is either symmetric or asymmetric, and the choice is made when the key is created.
A symmetric key is a single 256-bit secret that both encrypts and decrypts. It never leaves KMS unencrypted, so every encryption and decryption is a call to KMS made with AWS credentials, and every use is authorised by the key policy and logged in CloudTrail. AWS services that encrypt data on your behalf, such as S3, EBS and DynamoDB, use symmetric keys, and it is the default type.
An asymmetric key is a public and private key pair, based on RSA or elliptic curve cryptography. The private key never leaves KMS, but the public key can be downloaded and used anywhere. This allows two things that a symmetric key does not: parties outside AWS, without AWS credentials, can encrypt data with the public key that only KMS can decrypt, and KMS can sign data, such as a software release, whose signature anyone can verify with the public key. An HMAC key generates and verifies message authentication codes, for example to check that a token has not been altered. The following table summarises the key types.
| Key spec | Used for | Key material |
|---|---|---|
Symmetric (SYMMETRIC_DEFAULT) | Encryption and decryption inside KMS; integrated with AWS services | Never leaves KMS |
| Asymmetric RSA or ECC | Encryption outside AWS, or signing and verification | The public key can be downloaded; the private key never leaves KMS |
| HMAC | Generating and verifying message authentication codes | Never leaves KMS |
AWS services that encrypt data for you use symmetric keys.
Key policies and grants
Every KMS key has exactly one key policy, its primary access control. IAM policies take effect only if the key policy allows the account to delegate access through IAM, which the default key policy does with this statement:
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {"AWS": "arn:aws:iam::111122223333:root"},
"Action": "kms:*",
"Resource": "*"
}The statement above allows IAM policies to grant access to the key. The following list describes further controls over the use of KMS keys.
- If that statement is removed, only principals named in the key policy can use or manage the key.
- Grants give a principal temporary, programmatic use of a key for specific operations; AWS services such as EBS create grants on your behalf.
- Condition keys such as
kms:ViaServicelimit a key to use through a particular service, such as S3.
Aliases and multi-Region keys
KMS provides aliases, multi-Region keys and a waiting period before deletion. The following list describes these features.
- An alias, such as
alias/orders-key, lets code refer to a key by name, so you can point the alias at a new key without changing code. Aliases are Regional. - Multi-Region keys share key ID and key material across Regions, so data encrypted in one Region decrypts in another without a cross-Region call; useful for global tables, cross-Region replication and disaster recovery.
- A key scheduled for deletion waits 7 to 30 days first; disabling a key is the reversible alternative.
aws kms create-key --description "Orders data" --query KeyMetadata.KeyId --output text
aws kms create-alias --alias-name alias/orders-key --target-key-id 1234abcd-12ab-34cd-56ef-1234567890abAliases, Regions and deletion
Choosing a key type
The appropriate type of KMS key depends on the level of control required. The following table identifies the key suited to several requirements.
| Requirement | Key |
|---|---|
| Encrypt an S3 bucket with no key management at all | SSE-S3, or the AWS managed key aws/s3 |
| Audit and restrict who can decrypt payroll files | Customer managed key with a restrictive key policy |
| Share encrypted snapshots with a disaster recovery account | Customer managed key that allows the other account |
| Decrypt replicated data in a second Region without cross-Region calls | Multi-Region customer managed key |
| Sign software releases so customers can verify them | Asymmetric signing key; the public key is distributed |
Applications refer to each key through an alias such as alias/payroll, so a key can be replaced later without code changes.
When must a customer managed key be used instead of an AWS managed key?
Show answer
When you need to control the key policy, share the key with other accounts, disable or delete it yourself, or choose its rotation period. AWS managed keys cannot be shared and their policies cannot be changed.
Exam Tips
Remember for the exam
- Control over the key policy, cross-account sharing or your own audit trail requires a customer managed key.
- IAM policies grant KMS access only when the key policy allows the account to use IAM.
- Refer to keys by alias so a key can be replaced without code changes.
- To decrypt replicated data in another Region, use multi-Region keys.
- Deleting a KMS key makes its data unrecoverable; disable the key if access must stop temporarily.