Sub topic 5
Encrypting values with KMS keys
This chapter assumes you can create parameters and secrets (the previous chapters), read IAM policy JSON, and run the AWS CLI. It covers which KMS key protects each value, how to choose that key, and what breaks when key access is wrong.
What KMS protects in each service
AWS Key Management Service (KMS) holds the encryption keys. Neither Parameter Store nor Secrets Manager stores your key material. Both call KMS on your behalf.
- Parameter Store:
StringandStringListparameters are stored unencrypted. OnlySecureStringparameters use KMS. The default key is the AWS managed keyalias/aws/ssm. - Secrets Manager: Every secret is encrypted. The default key is the AWS managed key
aws/secretsmanager.
Secrets Manager uses envelope encryption. It asks KMS for a data key, encrypts the secret with that data key, and stores the encrypted data key beside the secret. To read a secret, it asks KMS to decrypt the data key and then decrypts the value. Your application never calls KMS directly. The caller's identity is still checked by KMS, as the next section shows.
AWS managed key or customer managed key
You can accept the default key or supply your own customer managed key.
| Property | AWS managed key | Customer managed key |
|---|---|---|
| Examples | `alias/aws/ssm`, `aws/secretsmanager` | `alias/myapp-prod` |
| Cost | No monthly fee | Monthly fee per key plus request charges |
| Key policy | Fixed by AWS, cannot be edited | You write it |
| Cross-account use | Not possible | Possible by editing the key policy |
| Disable or schedule deletion | Not possible | Possible |
| Audit and separation of duties | Limited | Per-key policies and CloudTrail records |
The AWS managed key suits a single account where every authorized principal in the account may decrypt. Choose a customer managed key in these cases:
- You need to restrict decryption to specific roles, even among principals that have Secrets Manager or Systems Manager permissions.
- You need to share a secret with another account. A secret encrypted with
aws/secretsmanagercannot be read from another account. - Your compliance rules require you to control the key lifecycle.
Creating and using a customer managed key
Create a key and give it an alias. Aliases let you refer to the key by a stable name.
KEY_ID=$(aws kms create-key \
--description "myapp prod configuration and secrets" \
--query KeyMetadata.KeyId --output text)
aws kms create-alias \
--alias-name alias/myapp-prod \
--target-key-id "$KEY_ID"Pass the key when you create a SecureString parameter:
aws ssm put-parameter \
--name /myapp/prod/db/password \
--value 'REPLACE_ME' \
--type SecureString \
--key-id alias/myapp-prodPass it when you create a secret:
aws secretsmanager create-secret \
--name myapp/prod/api \
--secret-string '{"apiKey":"REPLACE_ME"}' \
--kms-key-id alias/myapp-prodConfirm which key protects each value:
aws ssm describe-parameters \
--parameter-filters "Key=Name,Values=/myapp/prod/db/password" \
--query "Parameters[0].KeyId" --output text
aws secretsmanager describe-secret \
--secret-id myapp/prod/api \
--query KmsKeyId --output textFor a secret that uses the default key, KmsKeyId is absent from the output.
The two-permission rule
With a customer managed key, a caller needs permission in the service and in KMS. Reading a SecureString with decryption requires ssm:GetParameter and kms:Decrypt. Reading a secret requires secretsmanager:GetSecretValue and kms:Decrypt. The policy from the IAM chapter already includes the kms:Decrypt statement for this reason.
The key policy must also allow the use. A key policy is the primary access control for a KMS key. If it contains no statement that enables IAM policies, identity policies alone have no effect. The usual first statement delegates to the account. The second statement below limits the application role to decryption through the two services:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EnableIAMPolicies",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "AppRoleDecryptViaServicesOnly",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/myapp-prod" },
"Action": "kms:Decrypt",
"Resource": "*",
"Condition": {
"StringEquals": {
"kms:ViaService": [
"ssm.us-east-1.amazonaws.com",
"secretsmanager.us-east-1.amazonaws.com"
]
}
}
}
]
}The kms:ViaService condition means the role cannot call kms:Decrypt directly on arbitrary ciphertext. The key can be used only when Parameter Store or Secrets Manager makes the request. This limits the damage if the role's credentials leak.
The first statement lets the account's administrators manage the key and lets IAM policies grant use. Keep an administrative path to the key. If you remove it, you can lock yourself out of the key.
Writers need different KMS actions from readers. Storing a SecureString requires kms:Encrypt. Creating or updating a secret requires kms:GenerateDataKey and kms:Decrypt. Give those only to the roles that write values.
Changing the key on an existing value
You can re-encrypt a parameter by overwriting it with a new key. You must supply the value again:
aws ssm put-parameter \
--name /myapp/prod/db/password \
--value 'REPLACE_ME' \
--type SecureString \
--key-id alias/myapp-prod \
--overwriteFor a secret, change the key with update-secret:
aws secretsmanager update-secret \
--secret-id myapp/prod/api \
--kms-key-id alias/myapp-prodSecrets Manager re-encrypts the versions that carry the staging labels AWSCURRENT, AWSPREVIOUS and AWSPENDING under the new key. Versions that carry no label stay under the old key. Do not delete the old key until you have confirmed that nothing still depends on it.
Failure modes
KMS problems appear as errors on an otherwise valid read. These are the usual causes:
AccessDeniedExceptionmentioningkms:Decrypt: The role's policy lackskms:Decrypt, or the key policy does not allow the role. Check both.- Ciphertext returned instead of the value: You called
get-parameterwithout--with-decryption. The call succeeds, but the value is encrypted and unusable. - Error citing a disabled key or a key pending deletion: KMS refuses all use of the key. Every value encrypted under it becomes unreadable until you re-enable the key.
- Cross-account read fails: The value is encrypted with an AWS managed key, or the key policy does not name the other account. Re-encrypt under a customer managed key and grant the other account in its key policy.
- Deleted key: After the waiting period (7 to 30 days), deletion is permanent. Values encrypted under that key cannot be recovered.
Key rotation is separate from secret rotation
KMS can rotate a customer managed key automatically each year if you turn on key rotation. KMS keeps the old key material, so existing ciphertext still decrypts and you change nothing in your application. This protects the encryption key. It does not change the password or API key stored inside the value. Changing the stored value is secret rotation, which a later chapter covers.
Cost and performance
Every decryption is a KMS request. A customer managed key adds a monthly fee and per-request charges, and KMS enforces request quotas per Region. For this reason, applications should cache values. A later chapter shows how. Secrets Manager also makes its own KMS calls and may reuse data keys, so your read volume does not map one-to-one to KMS requests.
Exercise
A role in account A has secretsmanager:GetSecretValue on a secret in account B. The secret uses the default aws/secretsmanager key, and the read fails. What are the two changes needed to make it work, and what is the permission rule for the reader?
Show answer
First, re-encrypt the secret under a customer managed key in account B, because AWS managed keys cannot be used across accounts. Second, add a statement to that key's policy that allows account A (or its role) to use kms:Decrypt. The reader then needs both the Secrets Manager permission (granted by the secret's resource policy and the role's identity policy) and kms:Decrypt on the key, with the identity policy in account A also allowing the KMS action on the key's ARN.
Exercise
You run aws ssm get-parameter --name /myapp/prod/db/password without --with-decryption. What do you receive, and does the call need kms:Decrypt?
Show answer
You receive the encrypted ciphertext as the value. The call does not decrypt, so it does not need kms:Decrypt. Adding --with-decryption returns the plaintext and requires that permission when a customer managed key is in use.