Create and manipulate DynamoDB table with different methods using Python scripts and boto3
Tags: python, database, boto3, aws
Published: 2022-10-07 16:03:33



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hv79t648iuygcv9zzn0s.png)


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](https://github.com/awsmine/DynamoDB_projects.git) 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](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html)

---

**_Let's get started!_**

The code for this article is in my [GitHub Repository](https://github.com/awsmine/boto3_python_projects/tree/main/Dynamodb_boto3_lambda).


## 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

1. Create a directory called _Dynamodb_boto3_lambda_ on the 
   Cloud9 environment
**`mkdir <directory name>
   cd Dynamodb_boto3_lambda`**
2. To use Boto3, you must first import AWS Python SDK.
3. Then get the client representing DynamoDB.
4. Get the service resource you are going to use.
5. Use the DynamoDB.ServiceResource and DynamoDB.Table resources 
   in order to create Python scripts.
6. Copy the corresponding code from my [GitHub directory](https://github.com/awsmine/boto3_python_projects/tree/main/Dynamodb_boto3_lambda), save the files in Cloud9 directory that you have just now created.
7. Change permissions for all the python scripts to make it   
   executable
**`chmod 744 DynamoDB.py`**
8. 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
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zjjbgceu5s258ypx7gdk.png)
- **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.