Listing Sagemaker Endpoints in Boto3

Unintuitively, the Boto3 SageMaker list_endpoints() function lists the endpoints on the client side regardless of your filter. That means that if you don’t set MaxResults and if you don’t use the NextToken, then it doesn’t always get all of the endpoints and you can miss something.

Here’s a snippet that loops through all of the endpoints and aggregates them in a variable called matched endpoints.

ENDPOINT_NAME_TAG = "some-endpoint-name"
client = boto3.client("sagemaker")
try:
    response = client.list_endpoints(
        NameContains=ENDPOINT_NAME_TAG,
    )
 
    matched_endpoints = []
    while True:
        matched_endpoints.extend(response['Endpoints'])
        if 'NextToken' in response:
            response = client.list_endpoints(
                NameContains=ENDPOINT_NAME_TAG,
                MaxResults=100,
                NextToken=response['NextToken']
            )
        else:
            break