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:
nextToken = None: botocore.exceptions.ParamValidationError: Parameter validation failed: Invalid type for parameter nextToken, value: None, type: <class 'NoneType'>, valid types: <class 'str'>
nextToken = 'null': botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the ListFindings operation: Pagination token exception in operation 'ListFindings': invalid payload wrapping schema -24855
nextToken = empty string: An error occurred (ValidationException) when calling the ListFindings operation: Pagination token exception in operation 'ListFindings': invalid payload (no encryption schema version)
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?
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)