What is Boto3?
Boto3 is a Python library for AWS (Amazon Web Services), which helps interacting with their services including DynamoDB - you can think of it as DynamoDB Python SDK. It empowers developers to manage and create AWS resources and DynamoDB Tables and Items.
If you're looking for similar guide but for Node.js, you can find it here
List of DynamoDB Boto3 Query Examples
- Connecting Boto3 to DynamoDB
- Create Table
- Get All Items / Scan
- Get Item
- Batch Get Item
- Put Item
- Query Set of Items
- Update Item
- Conditionally Update Item
- Increment Item Attribute
- Delete Item
- Delete All Items
- Query with Sorting
- Query Pagination
- Run DynamoDB Local
Connecting to DynamoDB with boto3 is simple if you want to do that using Access and Secret Key combination:
Keep in mind that using access and secret keys is against best security practices, and you should instead use IAM roles/policies to interact with DynamoDB. Boto3, if ran on Lamba function or EC2 instance, will automatically consume IAM Role attached to it.
DynamoDB structures data in tables, so if you want to save some data to DynamoDB, first you need to create a table. You can do that using AWS Console, AWS CLI or using boto3, like this:
Keep in mind that provisioning a table takes some before it's active. If you want to know when it's ready to be used, you can use waiter function.
To get all items from DynamoDB table, you can use Scan operation. The problem is that Scan has 1 MB limit on the amount of data it will return in a request, so we need to paginate through the results in a loop.
You can apply
FilterExpression attribute in order to filter the results like this:
To get a single item from DynamoDB using Partition Key (and Sort Key if using composite key), you can use GetItem operation.
Keep in mind to replace
sortKeyName with actual keys from your table.
If you want to retrieve multiple items identified by a key(s) in one call, use
batch_get_item call with the following syntax:
Keep in mind that
batch_get_item is limited to 100 items and 16 MB of data.
To write a single item into the DynamoDB Table, use
Alternative way to get a collection of items is the
Query method. Query is much faster than Scan because it uses Indexes. It should be your preferred way to get a collection of items with the same partition key.
Keep in mind that
Query can return up to 1MB of data and you can also use
FilterExpressions here to narrow the results on non-key attributes.
If you don't know how to construct your Query, use Dynobase with Query Code Generation feature which will automatically generate it for you.
update_item operation consists of three primary attributes:
Key- which object should be updated
ExpressionAttributeValues- map with new values
UpdateExpression- how these new values should be applied to the object in the table
They can be used like this:
Moreover, you can also add a
ConditionExpression parameter, which restricts the update logic only if the evaluated expression equals
Incrementing a Number value in DynamoDB item can be achieved in two ways:
- Fetch item, update the value with code and send a
Putrequest overwriting item
While it might be tempting to use first method because Update syntax is unfriendly, I strongly recommend using second one because of the fact it's much faster (requires only one request) and atomic (imagine value updated by other client after you fetched item).
To do that using single
update_item operation, use following syntax:
Deleting a single item from DynamoDB table is similar to
Key argument accepts primary key and sort/range key if table has composite key.
Unfortunately, there's no easy way to delete all items from DynamoDB just like in SQL-based databases by using
DELETE FROM my-table;. To achieve the same result in DynamoDB, you need to query/scan to get all the items in a table using pagination until all items are scanned and then perform
delete operation one-by-one on each record.
Fortunately, this is possible just with 3 clicks using Dynobase.
Unfortunately, DynamoDB offers only one way of sorting the results on the database side - using the sort key. If your table does not have one, your sorting capabilities are limited to sorting items in application code after fetching the results. However, if you need to sort DynamoDB results on sort key descending or ascending, you can use following syntax:
Scan operation, Query returns results up to 1MB of items. If you need to fetch more records, you need to issue a second call to fetch the next page of results. If
LastEvaluatedKey was present in response object, this table has more items like requested and another call with
ExclusiveStartKey should be sent to fetch more of them:
If you need to use DynamoDB offline locally, you can use DynamoDB local distributed by AWS or DynamoDB from Localstack. Connecting to it is as easy as changing the
endpoint parameter in
© 2020 Dynobase