how.wtf

Full scan of DynamoDB table in Python

· Thomas Taylor

A full scan of DynamoDB in Python is possible by using the LastEvaluatedKey.

Scanning DynamoDB using LastEvaluatedKey

According to the boto3 scan documentation,

If LastEvaluatedKey is present in the response, you need to paginate the result set. For more information, see Paginating the Results in the Amazon DynamoDB Developer Guide.

Simply check for the LastEvaluatedKey in the response using response.get('LastEvaluatedKey').

 1import boto3
 2
 3dynamodb = boto3.resource(
 4    'dynamodb',
 5    aws_session_token=token,
 6    aws_access_key_id=access_key_id,
 7    aws_secret_access_key=secret_access_key,
 8    region_name=region
 9)
10
11table = dynamodb.Table('table-name')
12
13response = table.scan()
14data = response['Items']
15while response.get('LastEvaluatedKey') in response:
16    response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
17    data.extend(response['Items'])

In the example above, the list of items (data) is extended with each page of items.

#aws   #aws-cli   #python  

Reply to this post by email ↪