AWS KMS manages the encryption keys used by almost every AWS service. Turning on encryption for a bucket or a database is easy, and many people stop there. The problem is that encryption only stops people who cannot use the key from reading the data. If the key policy is too permissive, any identity with broad access in the account can decrypt the data, and the option you switched on in the console gives nothing more than a false sense of security.
In KMS, access to keys is controlled by the key policy, together with IAM permissions, usage conditions and grants, and that is where most of this guide focuses. Key types, rotation, envelope encryption, deletion and multi-Region keys also come into play, because they affect cost, performance or the key's lifecycle. For auditing, CloudTrail records every use and shows who decrypted what. With these pieces tied together, the key becomes an access boundary you can actually verify.
Key types and the key policy
KMS works with keys managed by AWS and with keys managed by the customer (customer managed keys). AWS managed keys are convenient, but you control neither their policy nor their rotation. For sensitive data, prefer customer managed keys: with them you decide who administers and who uses each key, and you can write specific policies for each context.
The key policy is the primary access control for a key, and IAM permissions add to it. The most common mistake is a key policy that delegates everything to the account's IAM. In this case, any principal with a generic KMS permission (a kms:* inside a broad policy, for example) can use the key. Keep key administrators separate from key users and avoid wildcards in the Principal element.
Key administrators: create, disable, change policyKey users: encrypt, decrypt, generateDataKeyAvoid granting both sets to the same broad principalSymmetric or asymmetric key
In most cases the right choice is a symmetric key, which uses the same material to encrypt and decrypt and never leaves KMS. Asymmetric keys exist for specific situations, such as digital signatures or encryption done by an outside party that only has the public key. Using an asymmetric key when you do not need one has a cost: the design gets more complex, and fewer AWS services can use the key transparently.
Conditions that restrict key usage
Listing principals is only the start. A key policy gets much stronger when it also uses conditions to limit the context in which the key can be used. With conditions you can require the call to come through a specific service, restrict usage to your organization or tie the operation to an encryption context. In practice, even a valid credential can only use the key within the expected context.
"Condition": { "StringEquals": { "kms:ViaService": "s3.sa-east-1.amazonaws.com", "aws:PrincipalOrgID": "o-example123" }}The kms:ViaService condition makes the key accept only calls that arrive through an authorized service, such as S3 or EBS. An identity that tries to use the key directly, outside that service, is turned away. The aws:PrincipalOrgID condition limits usage to principals in your organization: even if the principal is written broadly in the policy, access from external accounts is blocked.
Encryption context
The encryption context is a set of authenticated but non-secret data pairs that you attach to an operation. It does not change the ciphertext, but it has to be presented again, identical, at decryption time, and it is recorded in CloudTrail. That ties key usage to a specific context. When you put the context in a key policy condition, a shared key only decrypts data tagged with the right context, which lowers the risk of one workload reading data that belongs to another.
Rotation, grants and envelope encryption
Turn on automatic rotation for customer managed keys whenever it applies to your case. Rotation replaces the cryptographic material and keeps the same key identifier, without requiring existing data to be re-encrypted. For temporary, specific permissions, use grants instead of letting the key policy keep growing, since a grant has a narrow scope and can be revoked.
Most services use envelope encryption: KMS protects a data key, and that data key encrypts the content. The model is worth understanding because it affects cost and performance. Too many calls to KMS cost money and can run into throttling. Data key caching cuts down on those calls, but use it with judgment, because it trades a little security for efficiency.
Grants instead of a bloated policy
Grants are a good fit when a service or workload needs one-off access to the key, such as a service that creates an encrypted resource on your behalf. Instead of editing the key policy for every request, you issue a grant with a specific permission and a narrow scope, which can be revoked later. The key policy stays stable and easy to read, and day-to-day permissions are recorded and can be undone.
Lifecycle: deletion, aliases and multi-Region keys
Deleting a key cannot be undone: any data protected by it becomes unrecoverable. That is why KMS does not delete the key right away. It schedules the deletion, with a configurable waiting period. Before you even think about deleting, disable the key and watch whether anything breaks. If a good while passes with nothing depending on it, only then consider scheduling the deletion, always with monitoring in place.
# Disable the key and watch for impact before any deletionaws kms disable-key --key-id <key-id># If a deletion was scheduled by mistake, cancel it within the waiting periodaws kms cancel-key-deletion --key-id <key-id>In code and configuration, refer to keys by alias instead of scattering the raw identifier around. An alias is a stable name, and it lets you swap the key behind it without hunting for identifiers across the whole environment. The catch is keeping the alias pointed at the right key, because repointing an alias changes the key used by everything that references it.
Multi-Region keys replicate the key material across Regions under the same identifier, which makes disaster recovery and replication of encrypted data easier. The price is a larger surface, since each replica is one more place where the key can be used. Use multi-Region keys only when you actually replicate across Regions, and keep each replica's key policy as restrictive as the original key's.
Auditing and separation of duties
Every use of a KMS key is recorded in CloudTrail. During an investigation, that record answers who decrypted a given piece of data, when and from where. Also monitor sensitive events, such as attempts to disable a key or to schedule its deletion. These events can come from an operational mistake or from someone acting in bad faith, and in both cases you want to know right away.
- Use customer managed keys for sensitive data
- In the key policy, separate key administrators from key users
- Avoid wildcards in the principal and broad delegation to IAM
- Restrict usage with kms:ViaService, aws:PrincipalOrgID and encryption context
- Turn on automatic rotation where it applies
- Use grants for temporary permissions that can be revoked
- Refer to keys by alias, not by the raw identifier
- Watch CloudTrail for keys being disabled or deleted
Practical checklist
- Adopt customer managed keys for sensitive data
- Write the key policy with administration and usage in separate groups
- Remove wildcards from the principal and do not delegate everything to the account's IAM
- Apply kms:ViaService and aws:PrincipalOrgID where it makes sense
- Bind operations to an encryption context
- Enable automatic rotation on the keys where it applies
- Grant temporary, specific access through grants
- Use aliases in code and configuration
- Disable the key and watch the impact before scheduling deletion
- Track the use and administration of each key in CloudTrail
- Raise an alert when a key is disabled or scheduled for deletion
- Review from time to time who is able to decrypt
- Document what each important key is for
Best practices
- Treat the key as an access boundary: what matters is who can use it
- Separate administration from usage so no one holds both powers
- Use conditions to limit where and in what context the key is used
- Prefer revocable grants over an ever-growing key policy
- Check CloudTrail to find out who decrypted what
- Consider separate keys per context or environment
- Avoid accidental deletion: disable first and use an adequate waiting period
Common mistakes
- Key policy that delegates everything to the account's IAM
- Any principal with a generic KMS permission can use the key and read the data.
- The same principal administers and uses the key
- With both powers in one hand, abuse gets easier and separation of duties gets harder to maintain.
- Only AWS managed keys on sensitive data
- You lose control over policy and rotation exactly where you need it most.
- Nobody monitors key deletion
- If a deletion goes unnoticed, the data protected by the key becomes unrecoverable.
- Wildcards in the key policy principal with no conditions
- Key usage is open far beyond what is needed, even to identities outside the organization.
- Raw key identifiers scattered around instead of aliases
- When the key has to change, someone must hunt for identifiers across all the code and configuration.
Authorship and review
Cesar Gargiulo
CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.
Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.