Sub topic 4
Referring to keys with IDs, ARNs and aliases
This chapter assumes you can run AWS CLI commands and read short Python. It also assumes you know what a KMS key is, including the difference between customer managed and AWS managed keys, and that you have created at least one symmetric key.
Every KMS call has to say which key to use. KMS gives you four ways to say it: a key ID, a key ARN, an alias name, or an alias ARN. They look similar, but they differ in stability, in where they work, and in what happens when something changes. Picking the wrong one causes cross-account failures, surprising key swaps, and policies that don't match what you intended.
The four identifiers
The following table shows each identifier for a sample key.
| Identifier | Example | Notes |
|---|---|---|
| Key ID | `1234abcd-12ab-34cd-56ef-1234567890ab` | A UUID. Multi-Region keys use `mrk-` followed by 32 hex characters. |
| Key ARN | `arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab` | Includes the Region and the owning account. |
| Alias name | `alias/tutorial-key` | Must start with `alias/`. |
| Alias ARN | `arn:aws:kms:us-east-1:111122223333:alias/tutorial-key` | Includes the Region and the account that owns the alias. |
The key ID never changes for the life of the key. The key ARN is the key ID plus its location, so it never changes either. An alias is a separate resource that points at a key, and you can repoint it.
Aliases are separate resources
An alias is a friendly name that maps to exactly one KMS key. The rules:
- The name must begin with
alias/. After that prefix it can contain letters, digits,/,_and-. - The name is unique within an account and Region. The same alias name can exist in
us-east-1andeu-west-1, pointing at different keys. - The alias and its key must be in the same account and Region.
- One key can have several aliases, but one alias points at only one key.
- The prefix
alias/aws/is reserved for AWS managed keys, such asalias/aws/s3. You can't create or change aliases with that prefix. - Deleting an alias does not affect the key. Deleting a key's aliases never makes its data unreadable.
The Console adds the alias/ prefix for you. The CLI and SDKs don't, so --alias-name tutorial-key fails.
Working with aliases from the CLI
You created an alias in the spine's first example. These commands inspect and manage it.
# Create a key and an alias for it (the alias prefix is required)
KEY_ID=$(aws kms create-key --description "Tutorial key" \
--query KeyMetadata.KeyId --output text)
aws kms create-alias \
--alias-name alias/tutorial-key \
--target-key-id "$KEY_ID"
# Resolve an alias to the key it points at
aws kms describe-key \
--key-id alias/tutorial-key \
--query 'KeyMetadata.[KeyId,Arn]' \
--output text
# List all aliases, with the key each one targets
aws kms list-aliases \
--query 'Aliases[].[AliasName,TargetKeyId]' \
--output text
# List only the aliases of one key
aws kms list-aliases --key-id "$KEY_ID"
# Repoint an alias at a different key
aws kms update-alias \
--alias-name alias/tutorial-key \
--target-key-id <other-key-id>
# Remove the alias (the key is untouched)
aws kms delete-alias --alias-name alias/tutorial-keylist-aliases also returns aliases that AWS created for AWS managed keys, such as alias/aws/s3. These appear after a service first uses its managed key in your account.
The same lookups work from Python. Resolving an alias to its key ARN is useful when you need to pass a stable identifier to a policy or to another account.
import boto3
kms = boto3.client("kms")
def resolve_alias(alias_name: str) -> tuple[str, str]:
"""Return (key_id, key_arn) for an alias such as 'alias/tutorial-key'."""
meta = kms.describe_key(KeyId=alias_name)["KeyMetadata"]
return meta["KeyId"], meta["Arn"]
def aliases_by_key() -> dict[str, list[str]]:
"""Map each key ID to the alias names that point at it."""
result: dict[str, list[str]] = {}
for page in kms.get_paginator("list_aliases").paginate():
for alias in page["Aliases"]:
# Aliases without TargetKeyId are rare but possible, so skip them.
target = alias.get("TargetKeyId")
if target:
result.setdefault(target, []).append(alias["AliasName"])
return result
if __name__ == "__main__":
key_id, key_arn = resolve_alias("alias/tutorial-key")
print(key_id)
print(key_arn)
print(aliases_by_key().get(key_id, []))Where each identifier is accepted
Use the following rules to choose an identifier.
- Cryptographic operations in your own account.
Encrypt,GenerateDataKey,DescribeKeyand similar calls accept any of the four forms. An alias name is the usual choice for application code. - Cryptographic operations on a key in another account. You must use the key ARN or the alias ARN. A bare key ID or alias name is resolved in the caller's own account and Region, so it fails or hits the wrong key.
- Key management operations. Calls such as
EnableKeyRotation,ScheduleKeyDeletionandCreateGranttake a key ID or key ARN, not an alias. This is why the rotation and deletion examples use<key-id>. - Policies. Key policy and IAM policy
Resourceelements that grant use of a key name the key ARN. An alias ARN inResourcedoes not grant use of the key it points at. To write rules by alias, use the condition keys described below. - Decrypt. For a symmetric key,
Decryptdoes not need a key ID, because the ciphertext records which key made it. For asymmetric keys,KeyIdis required. PassingKeyIdanyway is good practice, because KMS then refuses to decrypt if the ciphertext came from a different key.
What happens when you repoint an alias
update-alias changes which key future requests reach. Two limits apply:
- The new key must have the same type as the old one: both symmetric, both asymmetric, or both HMAC. It must also have the same key usage, for example both
ENCRYPT_DECRYPT. - You can't update aliases of AWS managed keys.
Repointing has a consequence people often miss. Ciphertext already produced by key A stays bound to key A. After you move alias/orders-prod to key B:
- New
EncryptandGenerateDataKeycalls through the alias use key B. - Existing ciphertext still needs key A. A
Decryptcall withoutKeyIdworks as long as key A is enabled and the caller may use it. - A
Decryptcall that passesKeyId=alias/orders-prodnow fails for old ciphertext, because the alias resolves to key B, which didn't produce it. - If anything holds a
KeyIdpinned to the alias, you must either keep key A or re-encrypt the data.
This is also why an alias is not a rotation mechanism for data. Rotation of key material, covered in a later chapter, is a separate feature. Swapping aliases is a manual strategy and leaves the old key in service for as long as old data exists.
Aliases and access control
Because an alias can move, you can base policy conditions on it, but you must understand which alias the condition checks. Two condition keys cover this:
kms:RequestAliasmatches the alias name used in the request. It's present only if the caller passed an alias.kms:ResourceAliasesmatches any alias associated with the key, however the caller named it.
The following IAM policy statement lets a role use only keys that carry the alias alias/tutorial-key.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey"],
"Resource": "arn:aws:kms:us-east-1:111122223333:key/*",
"Condition": {
"ForAnyValue:StringEquals": {
"kms:ResourceAliases": "alias/tutorial-key"
}
}
}
]
}The statement never names a key ID. Whoever can run update-alias or create-alias for a key can therefore change what the role is allowed to reach. Treat kms:CreateAlias, kms:UpdateAlias and kms:DeleteAlias as permissions that affect access, and don't hand them out as harmless naming operations. Creating an alias needs permission on both the alias resource and the target key.
Common errors
| Symptom | Likely cause |
|---|---|
| `ValidationException` on `create-alias` | The name lacks the `alias/` prefix or uses a reserved `alias/aws/` prefix. |
| `AlreadyExistsException` | The alias name is already used in this account and Region. |
| `NotFoundException` for an alias that exists elsewhere | The call went to a different Region, or you used an alias name for a key in another account. |
| `AccessDeniedException` in cross-account calls | The caller used a key ID or alias name. Use the key ARN or alias ARN, and make sure the key policy and the caller's IAM policy both allow the call. |
| `Decrypt` fails after `update-alias` | The call passes the alias as `KeyId`, and the alias now points at a key that didn't encrypt the data. |
| `InvalidArnException` or "not an alias" errors on `EnableKeyRotation` | The operation needs a key ID or key ARN. |
Recommended practice
- Put the alias name in application configuration and the key ARN in policies, grants and cross-account settings.
- Use one alias per purpose and environment, for example
alias/orders-prod, so a key swap is a singleupdate-aliascall. - Log and review alias changes. They appear as
CreateAlias,UpdateAliasandDeleteAliasevents in CloudTrail, covered in a later chapter. - Keep an old key enabled until nothing needs its ciphertext.
Exercise
Your application calls kms.decrypt(CiphertextBlob=blob, KeyId="alias/orders-prod"). An operator runs update-alias to point alias/orders-prod at a new key. New data encrypts fine, but old blobs now fail to decrypt. Why, and what are two ways to fix it?
Show answer
The alias now resolves to the new key, and old ciphertext was produced by the old key, so KMS rejects the call. One fix is to omit KeyId from decrypt, which works for symmetric keys because the ciphertext records its key (the old key must still be enabled and usable by the caller). The other is to decrypt old data with the old key's ID or ARN, or to re-encrypt it under the new key. Either way, keep the old key until no ciphertext depends on it.
Exercise
A role in account 444455556666 needs to call Encrypt on alias/shared-data, owned by account 111122223333 in us-east-1. What identifier must the caller pass, and what else must be configured for the call to succeed?
Show answer
The caller must pass the alias ARN arn:aws:kms:us-east-1:111122223333:alias/shared-data or the key ARN, because an alias name alone would resolve in the caller's own account. The key policy in account 111122223333 must allow the role (or its account), and an IAM policy in account 444455556666 must allow kms:Encrypt on the key ARN.