Search code examples
pythonboto3aws-ssmaws-parameter-store

Python Boto3 Get Parameters from SSM by Path using NextToken


I've been working with boto3 for a while in order to gather some values from the Parameter Store SSM, this is the code I use, which is very simple:

def get_raw_parameters_group_by_namespace(namespace_path):
    raw_params_response = None

    try:
        if not namespace_path:
            raise Exception('Namespace path should be specified to get data')
        raw_params_response = ssm_ps.get_parameters_by_path(Path = namespace_path)
    except Exception as e:
        raise Exception('An error ocurred while trying to get parameters group: ' + str(e))

    return raw_params_response

I used to have around 7 to 10 parameters in SSM and that method worked fine, however, we needed the add some additional parameters these days and the number of them increased to 14, so I tried adding a property in the boto3 ssm method called "MaxResults" and set it to 50:

ssm_ps.get_parameters_by_path(Path = namespace_path, MaxResults = 50)

but I get the following:

"error": "An error ocurred while trying to get parameters group: An error occurred (ValidationException) when calling the GetParametersByPath operation: 1 validation error detected: Value '50' at 'maxResults' failed to satisfy constraint: Member must have value less than or equal to 10."

After talking with the team, increasing the quota in the account is not an option, so I wonder to know if probably using the "NextToken" property would be a good option.

I am not sure on how this can be used, I have searched for examples, but I could not find something useful. Does anyone know how to use NextToken please? Or an example on how is it supposed to work?

I tried something like:

raw_params_response = ssm_ps.get_parameters_by_path(Path = namespace_path, NextToken = 'Token')

But I am not sure on the usage of this.

Thanks in advance.


Solution

  • Let me suggest using this library (I'm the author): AWStanding

    You can achieve this easily, without worrying about pagination:

    import os
    from awstanding.parameter_store import load_path
    
    load_path('/stripe', '/spotify')
    STRIPE_PRICE = os.environ.get('STRIPE_PRICE', 'fallback_value')
    STRIPE_WEBHOOK = os.environ.get('STRIPE_WEBHOOK', 'fallback_value')
    SPOTIFY_API_KEY = os.environ.get('SPOTIFY_API_KEY', 'fallback_value')
    
    print(f'price: {STRIPE_PRICE}, webhook: {STRIPE_WEBHOOK}, spotify: {SPOTIFY_API_KEY}')
    
    >>> price: price_1xxxxxxxxxxxxxxxxxxxxxxx, webhook: fallback_value, spotify: fallback_value