This article explains a concept on how to create a DynamoDB table, then write, retrieve, query, scan, and delete the items from it, and lastly delete the DynamoDB table with Python script using boto3.
In my next article, I am going to explain another concept on how DynamoDB integrates with Lambda function.
Last month, for the AWS Community Day India Virtual Edition 2022 which is going to be held on 11th & 12th, November 2022, I had proposed an article combining both the concepts for submission when they announced for the Call for Speakers. Waiting eagerly to be accepted!
Please visit my GitHub Repository for DynamoDB articles on various topics being updated on constant basis.
Why DynamoDB
In the past, database options were very limited. The relational databases were the only databases which were widely used, thus limiting the types of applications that you could build. They were not perfectly suited for any one particular task.
In the last 3-4 decades, we have seen a rapid shift with the introduction of internet-enabled applications. This allowed to go for the databases that can be built faster and go global. Additionally, the rise of cloud computing has changed this scenario completely, because we can build more scalable and resilient applications in an economical way.
These changes have led to the rise of the purpose-built databases. Now no need to go for the relational databases anymore. Based on the needs of your applications, you can choose a database tailored to your needs.
DynamoDB is one of those databases which is used into building internet-scale applications, high-traffic web apps, e-commerce systems, entertainment video streaming content apps,and gaming applications.
What is DynamoDB?
As per Amazon "DynamoDB is a fully managed, multi-Region, multi-active, durable database with built-in security, backup and restore, and in-memory caching for internet-scale applications."
It is a fast and flexible non-relational serverless database service for single-digit millisecond performance any scale.
Being serverless is a very big reason DynamoDB is so popular, because it enables customers/users to offload the administrative burdens of operating and scaling distributed databases to AWS so that they don’t have to worry about hardware provisioning, setup and configuration, throughput capacity planning, replication, software patching, or cluster scaling. AWS Handles everything!
Additionally, DynamoDB has a pay-per-use pricing option that fits well in the serverless world.
If we talk about the use cases, the world’s top-growing enterprises are using Amazon’s Dynamodb service to manage their big data. Enterprises such as Airbnb, Toyota, Capital One, Disney, Netflix, and many more are utilizing the features and functions of this platform to manage their customer base and their data.
No matter what type of enterprise you own, the Dynamodb service is there for you to manage the data and to take full control of it. It is the most effective solution for enterprises of all sizes.
The best thing about this service is it stores data over the cloud. The cloud-storage service makes the data storage free from hassle. It also reduces the time for managing those data as the service manages the data automatically.
I am going to show you how to create an application for the entertainment video streaming content.
Features of DynamoDB
The main components of the DynamoDB are tables, items and attributes.
Tables
A table is a collection of items. For example, in our application, the table "movies" contain the data for Title and Director.
Items
An item is a group of attributes that is uniquely identifiable.
Items in DynamoDB are similar in many ways to rows, records, or tuples in other database systems.
Attributes
Each item is a collection of attributes, like key-value pairs. An attribute is a data element.
Attributes in DynamoDB are similar in many ways to fields or columns in other database systems.
Primary Keys/Partition Keys
Simple Primary Key:
- Uniquely identify each item in a table and is mandatory.
- Also known as Partition Key/Hash Key.
Composite Primary Key: As the name indicates it is a combination of a Partition Key and optionally a Sort Key.
Sort Key:
It is a part of the primary key, and as the name indicates it is used to sort among all the items sharing the same partition key.
Data types:
The only data types allowed for primary key attributes are string, number, or binary.
Secondary indexes:
A secondary index provides more flexibility in querying the data in the table.
DynamoDB doesn't require that you use indexes, except in a more larger tables.
I am not using any secondary indexes for this application.
Read/Write Capacity Modes:
- On-demand mode Is a flexible billing option which offers
pay-per-request
pricing for read and write requests per second so that you pay
only for what you use without capacity planning.
Specify throughput capacity in terms of read request units
(RRUs) and write request units (WRUs).
- Provisioned mode Is default and free-tier eligible. It controls how you are charged for read and write throughput
per second
and how you manage capacity.
Specify throughput capacity in terms of read capacity units
(RCUs) and write capacity units (WCUs).
DynamoDB Streams
DynamoDB Streams is an optional feature that captures data modification events in DynamoDB tables, such as adding, updating and deleting data.
Each event is represented by a stream record.
What is boto3?
Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python.
It supports all current AWS cloud services, including Elastic Compute Cloud (EC2), DynamoDB, AWS Config, Cloud Watch, and Simple Storage Service (S3).
It allows you to directly create, update, and delete AWS resources from your Python scripts.
I have used extensively the boto3 documentation for this session
Boto3 documentation — Boto3 Docs 1.24.56 documentation
Let's get started!
The code for this article is in my GitHub Repository.
Pre-requisites:
Amazon DynamoDB can be integrated with other AWS services.
- AWS user account with admin access, not a root account.
- Cloud9 IDE comes with Python and boto3 installed OR
- if it is not installed, Install boto3
python3 -m pip install boto3
- AWS Command Line Interface (AWS CLI) enables you to interact with AWS services using commands in your command-line shell.
Objectives
- Create DynamoDB with Primary key/Partition Key and a Sort Key
- Add 4 items to the table
- Create and run the Python scripts to list, print, query, scan, delete and put items to the DynamoDB table.
- Delete the DynamoDB table.
Steps of Implementing the Objectives
- Create a directory called Dynamodb_boto3_lambda on the
Cloud9 environment
mkdir <directory name> cd Dynamodb_boto3_lambda
- To use Boto3, you must first import AWS Python SDK.
- Then get the client representing DynamoDB.
- Get the service resource you are going to use.
- Use the DynamoDB.ServiceResource and DynamoDB.Table resources in order to create Python scripts.
- Copy the corresponding code from my GitHub directory, save the files in Cloud9 directory that you have just now created.
- Change permissions for all the python scripts to make it
executable
chmod 744 DynamoDB.py
- Execute all the python files in the order starting from
1-11
./python_file_name.py
Python scripts:
- 1_create_table.py - Get the method create_table() to create table "movies". and print data from table - Create DynamoDB with Primary key/Partition Key and a Sort Key
- 2_add_items_batch.py - get the method put_item to create for adding the items to the table
- 3_add_items.py - get the method put_item to create for adding the items to the table
- 4_list_table.py - List the table
- 5_print_table_data.py - print data from table
- 6_query_table.py - you can query the items in the table using the DynamoDB.Table.query() method.
- 7_scan_table.py - scan the table
- 8_scan_table_json.py - scan the formatted table
- 9_delete_items.py - get the method delete_item() and create the file
- 10_put_items.py - add an item to the table
- 11_delete_table.py - delete the table
What we have done so far
Successfully created and manipulated a DynamoDB table with different methods using Python scripts and boto3.
At the end of this, you should feel confident in your ability that you used DynamoDB in your application.
I like python and is fun to do projects with it.
Top comments (2)
Thanks for this :)
you are welcome