Sub topic 5
Encrypting values with KMS keys
This chapter assumes you know what Parameter Store SecureString parameters and Secrets Manager secrets are, and that you can read basic IAM policies. Here you will learn which KMS key protects each value, how to choose between AWS managed and customer managed keys, and which permission errors to expect.
How encryption works for both services
Neither service stores your value in plaintext. Both use AWS Key Management Service (KMS) for envelope encryption. KMS creates a data key, the service encrypts your value with that data key, and the data key is itself stored encrypted under a KMS key. Your application never handles the KMS key directly. Only the encrypt and decrypt calls pass through it.
Two things follow from this:
- Reading a protected value needs permission on the service and on the KMS key.
- The key's own policy decides who can use it, in addition to IAM.
AWS managed key or customer managed key
Each service has a default key. Parameter Store uses alias/aws/ssm and Secrets Manager uses aws/secretsmanager. You can instead choose a customer managed key that you create in KMS.
| Aspect | AWS managed key | Customer managed key |
|---|---|---|
| Cost | No monthly key charge | Monthly charge per key, plus request charges |
| Key policy | Fixed by AWS | You write it |
| Rotation | AWS rotates it | Optional automatic rotation, which you enable |
| Cross-account use | Not possible | Possible, if the key policy allows it |
| Access control | Anyone in the account with service permission | Can be limited to specific roles |
Use a customer managed key when you need to restrict who can decrypt, share values across accounts, or meet a rule that requires you to control the key. Otherwise the default is fine.
Choosing a key
For Parameter Store, pass --key-id when you create a SecureString:
aws ssm put-parameter \
--name /myapp/prod/db/password \
--value 'REPLACE_ME' \
--type SecureString \
--key-id alias/myapp-prodFor Secrets Manager, pass --kms-key-id:
aws secretsmanager create-secret \
--name myapp/prod/db \
--secret-string '{"username":"app","password":"REPLACE_ME"}' \
--kms-key-id alias/myapp-prodAn alias is a friendly name that points to a key. Policies that name the key ARN are still the safest, because an alias can be repointed.
Permissions you need
The caller needs kms:Decrypt on the key to read a value. To write one, the caller needs kms:GenerateDataKey (Secrets Manager) or kms:Encrypt (Parameter Store) as well. The IAM policy shown earlier in this tutorial grants kms:Decrypt on one key. That works only if the key policy also lets the account use IAM for access, which the default key policy does.
Common errors
AccessDeniedExceptionmentioningkms:Decrypt: the role has service permission but no permission on the key. Add the KMS statement or fix the key policy.- Reading works in one account but fails in another: the AWS managed key cannot be shared. Move the value to a customer managed key and allow the other account in its key policy.
- Reading works for plain String parameters but fails for SecureString: only the SecureString needs KMS, so the missing permission is on the key.
- Deleting a key: values encrypted under it become unreadable once the key is gone. KMS enforces a waiting period of 7 to 30 days before deletion, so use it to check that nothing still depends on the key.
Exercise
A Lambda role can call secretsmanager:GetSecretValue on a secret, but the call fails with AccessDeniedException naming kms:Decrypt. The secret uses a customer managed key. What two places can you check, and what should you look for?
Show answer
Check the role's IAM policy for a kms:Decrypt statement that names the key's ARN. Check the key policy to confirm it lets the account's IAM policies grant access, or names the role directly. One of the two is missing the permission.