Marcelo Tataje
Marcelo Tataje

Reputation: 3881

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.

Upvotes: 5

Views: 9580

Answers (3)

DanH
DanH

Reputation: 43

Since none of the responses directly answered the OP's question about how to use "NextToken"... This is an example of how I use it to load a list of paths into a dictionary that I use within my application...

import boto3
import os

class Settings:

parameters = None

def __init__(self, paths):

    if Settings.parameters is None:
        Settings.parameters = dict()

    ssm_client = boto3.client('ssm', region_name="eu-central-1")

    for path in paths:

        workingPath = path + '/' + os.getenv("ENVIRONMENT")
        response = ssm_client.get_parameters_by_path(Path=workingPath, WithDecryption=True, Recursive=True)

        self.load_dictionary(workingPath, response['Parameters'])

        while 'NextToken' in response:
            response = ssm_client.get_parameters_by_path(Path=workingPath, WithDecryption=True, Recursive=True, NextToken=response['NextToken'])
            self.load_dictionary(workingPath, response['Parameters'])

    ssm_client.close
    ssm_client = None

def load_dictionary(self, path, parameters):
    for parameter in parameters:
        self.parameters[parameter['Name'].replace(path + '/', '')] = parameter['Value']

Upvotes: 0

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

Upvotes: 1

smartinski
smartinski

Reputation: 61

I remember running into this at some point.

You want to use a paginator - https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ssm.html#SSM.Paginator.GetParametersByPath

This is how I used it:

import boto3

client = boto3.client('ssm',region_name='eu-central-1')

paginator = client.get_paginator('get_parameters_by_path')

response_iterator = paginator.paginate(
    Path='/some/path'
)

parameters=[]

for page in response_iterator:
    for entry in page['Parameters']:
        parameters.append(entry)

And you would get a list like [{"Name": "/some/path/param, "Value": "something"}] in parameters with all the parameters under the path.

*edit: response would be much richer than just the Name, Value keys. check the paginator docs!

Upvotes: 6

Related Questions