How to Query DynamoDB Using Lambda
To demonstrate how to do this, we’ll use the boto3 python library. This is the library you’ll use to interact with AWS. Most commonly, you’ll use this library in conjunction with lambda functions to execute code.
Executing a Scan
The most simple way to get data from DynamoDB is to use a scan. A scan will return all of the records in your database. You can execute a scan using the code below:
import boto3 dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('table-name') data = table.scan()
To be frank, a scan is the worst way to use DynamoDB. If you’re using a scan in your code, it’s most likely a glaring error and going to cripple your performance at scale. When determining how to query your DynamoDB instance, use a query. Not a scan.
Executing a Query
When you’re making use of DynamoDB in a production environment, you’ll want to use queries. When executing a query, it’s important to understand the KeyConditionExpression. Essentially, the KeyConditionExpression are the parameters that you want to query for.
import boto3 dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('table-name') data = table.query( KeyConditionExpression=Key('movie').eq('Big') )
In a real world environment, we recommend having a primary key and a sort key in your Dynamo database, labeled ‘pk’ and ‘sk’ respectfully. Because Dyanmo is geared towards a single table design, this gives you more flexibility when saving items to the database.
One of the biggest hurdles of NoSQL database design for those coming from a relational environment is the single table design pattern. It’s quirky, but trust us, it’s the best way to architect DynamoDB applications, and it’s incredibly flexible.
Often, you’ll want to access your data and sort in ways that are outside of your standard primary and sort key. That’s where indexes come in. Indexes allow you to implement other data access while still maintaining a flexible single table design.
For instance, let’s say we want to query all movies that are set to release in the future. To do this, we’ll need to create an index in DynamoDB and query that index. Once the index is created, we can query the data like so:
import boto3 import time timestamp = int(time.time()) dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('table-name') data = table.query( IndexName="pk-releaseDate-index", KeyConditionExpression=Key('pk').eq('movie') & Key('releaseDate').gte(timestamp) )