Search code examples
pythonaws-lambdaboto3

AWS Lambda Python Boto3 - Item count dynamodb table


I am trying to count the total number of items in the Dynamobd table. Boto3 documentation says

item_count attribute. 
(integer) --
The number of items in the specified table. DynamoDB updates this value approximately every six hours. Recent changes might not be reflected in this value.

I populated about 100 records into that table. output shows 0 records

 import json
 import os
 import boto3
 from pprint import pprint

 tableName = os.environ.get('TABLE')
 fieldName = os.environ.get('FIELD')

 dbclient = boto3.resource('dynamodb')

 def lambda_handler(event, context):               
            tableresource = dbclient.Table(tableName)        
            count = tableresource.item_count
            print('total items in the table are ' + str(count))
 

Solution

  • As you saw in the AWS documentation, item_count is:

    The number of items in the specified table. DynamoDB updates this value approximately every six hours. Recent changes might not be reflected in this value.

    It looks like you added the new items very recently so that count will not be accurate right now. You would have to wait for 6 hours max, 3 hours on average to get an updated item count

    This is generally how large-scale distributed systems like S3 and DynamoDB work. They don't offer you an instantaneous, accurate count of objects or items because it's difficult to maintain that count accurately and the cost to calculate it instantaneously is prohibitive in the general case.