Search code examples
pythonamazon-web-servicesbotoamazon-kms

boto3 set marker for kms list_aliases() command


Some of the AWS accounts I'm using have a lot of KMS keys that have aliases tied to them.

My problem is that if the list_aliases() command returns too many results, the results are truncated and the script fails if the value it's searching for is beyond the truncation point.

I tried this to get 200 results back, but it did not work:

alias_list = (kms_client.list_aliases(Marker='200')
botocore.errorfactory.InvalidMarkerException: An error occurred (InvalidMarkerException) when calling the ListAliases operation: Could not deserialize marker '200'

How do I set the marker for the list_aliases() command?


Solution

  • First, per the documentation list_aliases returns 1 to 100 results. This is set using Limit, not Marker.

    Example:

    alias_list = (kms_client.list_aliases(Limit=100)
    

    Second, results beyond the first 100 require using the Marker value that is returned by the first call to list_aliases(). Below is a simple example of how to get the marker and use it to get the next 1-100 values.

    Disclaimer, this code doesn't actually do anything with the retrieved aliases and I've not tested it.

    def get_kms_alias(kms_client, limit=100, marker=""):
    
        if marker:
            alias_request = (kms_client.list_aliases(Limit=limit, Marker=marker))
        else:
            alias_request = (kms_client.list_aliases(Limit=limit))
    
        print(alias_request["Aliases"])
        alias_truncated = alias_request["Truncated"]
    
        if truncated in "True":
            marker = alias_request["NextMarker"]
            get_kms_alias(kms_client, limit, marker)
    
        return None