Only getting partial Secrets list from boto3

I’m having an odd problem with querying AWS SecretsManager using the Python3 boto3 library. When I get a list of secrets I consistenty only get 10 when there’s more than that listed in the web console and it doesn’t include the ones I need. Sample code below.

import json
import boto3
import base64

session = boto3.session.Session('secretsmanager', 'eu-west-2')
client = boto3.client('secretsmanager')

secrets=client.list_secrets()

length=len(secrets['SecretList'])
print('Secrets found: '+str(length))

Has anyone else seen this issue?

Edit: after looking at the docs again it looks like it may be because I need to specify a NextToken parameter but I can’t find any examples of what this should look like.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager.html#SecretsManager.Client.list_secrets

Edit2: it looks like I need to use pagination
Edit3: looks like you can’t paginate this call. Instead use NextToken parameter like:

secrets=client.list_secrets(MaxResults=10)
moreSecrets=client.list_secrets(MaxResults=10, NextToken=secrets['NextToken'])

Get the next link from the initial boto3 request


import boto3


def list_all_secrets():
    '''
       list all names of secrets in an account
       Note: This does not show you the secret string
       just the name of the secrets are shown.
    '''
    results = []
    client = boto3.client('secretsmanager')
    secrets = client.list_secrets()
    next_token = secrets.get('NextToken', False)
    if 'SecretList' in secrets:
        results = [s['Name'] for s in secrets['SecretList']]

    while next_token:
        secrets = client.list_secrets(NextToken=next_token)
        if 'SecretList' in secrets:
            for secret in secrets['SecretList']:
                results.append(secret['Name'])
        next_token = secrets.get('NextToken', False)

    return results