0
0
DynamodbHow-ToBeginner ยท 4 min read

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 LastEvaluatedKey exists before using it causes errors.
  • Passing LastEvaluatedKey directly without using it as ExclusiveStartKey in 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

TermDescription
LastEvaluatedKeyKey of the last item returned in a query or scan response.
ExclusiveStartKeyKey provided in the next request to continue from the last item.
PaginationProcess of fetching data in chunks using LastEvaluatedKey and ExclusiveStartKey.
Scan/QueryDynamoDB 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.