Reputation: 488
I need to extract the items from dynamodb which is matching id, name, role
Above 3 are just attributes of the table they are not part key or sort key
Below is the code
import boto3
from boto3.dynamodb.conditions import Attr
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('mytable')
def lambda_handler(event, context):
response_list = []
scan_response = table.scan(
FilterExpression=Attr('id').eq(event['id']) & Attr(
'name').eq(event['name']) & Attr(
'role').eq('Manager')
)
response_list.extend(scan_response['Items'])
while scan_response.get('LastEvaluatedKey'):
scan_response = table.scan(
FilterExpression=Attr('id').eq(event['id']) & Attr(
'name').eq(event['name']) & Attr(
'role').eq('Manager')
)
response_list.extend(scan_response['Items'])
print( response_list)
My loop is running infinitely on the first item only. where is the issue?
Upvotes: 0
Views: 1398
Reputation: 200476
You aren't passing the LastEvaluatedKey
into the scan()
call, so it is starting the scan over from the beginning each time.
Change it to this:
while scan_response.get('LastEvaluatedKey'):
scan_response = table.scan(
ExclusiveStartKey=scan_response.get('LastEvaluatedKey'),
FilterExpression=Attr('id').eq(event['id']) & Attr(
'name').eq(event['name']) & Attr(
'role').eq('Manager')
)
Upvotes: 2