How do I query AWS DynamoDB in python?

DynamoDB does not automatically index all of the fields of your object. By default you can define a hash key (subscription_id in your case) and, optionally, a range key and those will be indexed. So, you could do this:

response = table.get_item(Key={'subscription_id': mysubid})

and it will work as expected. However, if you want to retrieve an item based on order_number you would have to use a scan operation which looks through all items in your table to find the one(s) with the correct value. This is a very expensive operation. Or you could create a Global Secondary Index in your table that uses order_number as the primary key. If you did that and called the new index order_number-index you could then query for objects that match a specific order number like this:

from boto3.dynamodb.conditions import Key, Attr

response = table.query(
    IndexName='order_number-index',
    KeyConditionExpression=Key('order_number').eq(myordernumber))

DynamoDB is an very fast, scalable, and efficient database but it does require a lot of thought about what fields you might want to search on and how to do that efficiently.

The good news is that now you can add GSI's to an existing table. Previously you would have had to delete your table and start all over again.


Make sure you've imported this:

from boto3.dynamodb.conditions import Key, Attr

If you don't have it, you'll get the error for sure. It's in the documentation examples.

Thanks @altoids for the comment above as this is the correct answer for me. I wanted to bring attention to it with a "formal" answer.


To query dynamodb using Index with filter:

import boto3
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource('dynamodb', region_name=region)
table = dynamodb.Table('<TableName>')

response = table.query(
    IndexName='<Index>',
    KeyConditionExpression=Key('<key1>').eq('<value>') & Key('<key2>').eq('<value>'),
    FilterExpression=Attr('<attr>').eq('<value>')
)

print(response['Items'])

If filter is not rquired then don't use FilterExpression in query.