pythonamazon-web-servicessecurityaws-lambdaboto3

What is Boto3 Inspector v2 list_findings' nextToken initial value?


I am using boto3 Python to list findings from Inspector v2, using the list_findings() method inside a loop, according to AWS Boto3 Inspector2 Docs I have to set the value of this parameter to null for the first request to a list action but keep getting error in all these cases for variable next_token:

here is my code:

import boto3
inspector = boto3.client("inspector2")
next_token = "" # I change the value of this variable 
while True:
    response = inspector.list_findings(
            filterCriteria={
                'findingStatus': [
                    {
                        'comparison': 'EQUALS',
                        'value': 'ACTIVE'
                    },
                ],
                'findingType': [
                    {
                        'comparison': 'EQUALS',
                        'value': 'PACKAGE_VULNERABILITY'
                    },
                ],
            },
        nextToken=next_token
    )
    next_token= response.get("nextToken") 
    
    # Some Code Here 
    
    if next_token == None:
        break

I am confused about what should the value of nextToken be for the first request?


Solution

  • It would be easier to use paginate as explained in AWS docs:

    # Create a client
    inspector = boto3.client("inspector2")
    
    # Create a reusable Paginator
    paginator = inspector.get_paginator('list_findings')
    
    # Create a PageIterator from the Paginator
    page_iterator = paginator.paginate(filterCriteria={
                    'findingStatus': [
                        {
                            'comparison': 'EQUALS',
                            'value': 'ACTIVE'
                        },
                    ],
                    'findingType': [
                        {
                            'comparison': 'EQUALS',
                            'value': 'PACKAGE_VULNERABILITY'
                        },
                    ],
                })
    
    for page in page_iterator:
        print(page)