How do I list CMK grants and principals by Region in AWS KMS?
Last updated: 2020-09-10
I want to list customer master key (CMK) grants and principals for my AWS Key Management Service (AWS KMS) accounts by AWS Region.
You can retrieve the number of grants a CMK has and the principles for each one by using the AWS Command Line Interface (AWS CLI) or AWS SDKs. Be sure that you install and configure the AWS CLI with policy permissions to perform list-keys and list-grants.
Note: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent version of the AWS CLI.
Run the following commands to list your CMK and grants:
Note: Replace enter your region with your AWS Region.
aws kms list-keys --region enter your region aws kms list-grants --region enter your region --key-id 1234abcd-12ab-34cd-56ef-1234567890ab
To query against all your CMKs for a specific AWS Region, run this command:
for key in $(aws kms list-keys --region enter your region --query 'Keys.KeyId' --output text);do aws kms list-grants --region <enter your region> --key-id $key; done
Note: This example uses the built-in AWS CLI --query option to filter elements from the output.
Run this command to list the number of grants each principal has for a KMS key:
aws kms list-grants --region enter your region --key-id 1234abcd-12ab-34cd-56ef-1234567890ab | jq '.Grants.GranteePrincipal' -r | sort | uniq -c;