CloudWatch Log of Lambda Function
I'm using the below code to scan with pagination a dynamodb table to pull 5 records from a maximum of 20 records. When I invoke the api through postman GET method it is pulling the same 5 records each time. Does it mean that my lambda code doesn't work properly or do I need to customize my API call in postman?
import boto3
import os
import json
def lambda_handler(event, context):
client = boto3.client('dynamodb', region_name='ap-southeast-1')
pagination_config={
"MaxItems":20,
"PageSize": 5
}
paginator = client.get_paginator('scan')
response_iterator = paginator.paginate(
TableName="Users",
PaginationConfig=pagination_config
)
for page in response_iterator:
Items = page['Items']
print(Items)
print("--------------------------")
from botocore.paginate import TokenEncoder
encoder = TokenEncoder()
for page in response_iterator:
if "LastEvaluatedKey" in page:
encoded_token = encoder.encode({"ExclusiveStartKey": page["LastEvaluatedKey"]})
pagination_config = {
"MaxItems": 20,
"PageSize": 5,
"StartingToken": encoded_token
}
Items = page['Items']
return {
'statusCode': 200,
'headers': {},
'body': json.dumps(Items)
}