Search code examples
amazon-web-servicesamazon-dynamodb-streams

Aws DynamoDb Stream for NextShardIterator key in response


i am using dynamodbstream calling with method response = client.get_records(ShardIterator=shard_iterator, Limit=1)

initially response contain response['NextShardIterator'] key

but after some time I got KeyError: 'NextShardIterator' why it is absent in response


Solution

  • Instead of writing code like the following:

    shard_iterator = response['NextShardIterator']
    
    if not shard_iterator:
        break
    

    Write the following instead:

    if 'NextShardIterator' in response:
        shard_iterator = response['NextShardIterator']
    else:
        break
    

    Or you can use dict.get with a default value, as follows:

    shard_iterator = response.get('NextShardIterator', None)
    
    if not shard_iterator:
        break