How to Use LastEvaluatedKey in DynamoDB for Pagination
In DynamoDB,
LastEvaluatedKey is used to paginate results by providing the key of the last item returned in a query or scan. You pass this key as ExclusiveStartKey in the next request to continue fetching the next set of items.Syntax
The LastEvaluatedKey is part of the response from a Query or Scan operation. To get the next page of results, you use it as the ExclusiveStartKey in the next request.
- LastEvaluatedKey: The key of the last item returned in the current response.
- ExclusiveStartKey: The key you provide in the next request to start scanning or querying from that point.
python
response = dynamodb_client.query(
TableName='YourTable',
KeyConditionExpression='PartitionKey = :pk',
ExpressionAttributeValues={':pk': {'S': 'someKey'}},
ExclusiveStartKey=response.get('LastEvaluatedKey') # Use last key from previous response
)Example
This example shows how to paginate through all items in a DynamoDB table using LastEvaluatedKey. It fetches items page by page until no more items remain.
python
import boto3 dynamodb = boto3.client('dynamodb') # Initial request without ExclusiveStartKey response = dynamodb.scan(TableName='YourTable') items = response.get('Items', []) # Loop while LastEvaluatedKey exists while 'LastEvaluatedKey' in response: response = dynamodb.scan( TableName='YourTable', ExclusiveStartKey=response['LastEvaluatedKey'] ) items.extend(response.get('Items', [])) print(f"Total items fetched: {len(items)}")
Output
Total items fetched: 150
Common Pitfalls
- Not checking if
LastEvaluatedKeyexists before using it causes errors. - Passing
LastEvaluatedKeydirectly without using it asExclusiveStartKeyin the next request. - Assuming all results come in one response; DynamoDB paginates large results.
- Forgetting to accumulate results across pages.
python
## Wrong way: Using LastEvaluatedKey as a filter or ignoring it response = dynamodb.scan(TableName='YourTable') # Incorrect: Not using ExclusiveStartKey for next page next_response = dynamodb.scan(TableName='YourTable') # No pagination ## Right way: Use LastEvaluatedKey as ExclusiveStartKey if 'LastEvaluatedKey' in response: next_response = dynamodb.scan( TableName='YourTable', ExclusiveStartKey=response['LastEvaluatedKey'] )
Quick Reference
| Term | Description |
|---|---|
| LastEvaluatedKey | Key of the last item returned in a query or scan response. |
| ExclusiveStartKey | Key provided in the next request to continue from the last item. |
| Pagination | Process of fetching data in chunks using LastEvaluatedKey and ExclusiveStartKey. |
| Scan/Query | DynamoDB operations that can return LastEvaluatedKey if results are large. |
Key Takeaways
Use LastEvaluatedKey from a response as ExclusiveStartKey in the next request to paginate.
Always check if LastEvaluatedKey exists before using it to avoid errors.
Accumulate results across pages to get the full dataset.
DynamoDB paginates large query or scan results automatically using LastEvaluatedKey.
Do not assume all data is returned in a single response; handle pagination properly.