How To Create a DynamoDB table, add items, scan, query and delete the table using boto3 and Python.
Welcome to my week 14 post in the Level Up In Tech Program. In this article, we will be using boto3 and Python to help create, add items to the table, scan, query and delete the table. You will need to be logged in to your AWS console, have access to your GitHub, and install boto3 and CLI onto your Cloud 9 environment. You also need to create your new Python template on Cloud9 and save it, and we will later push it to GitHub to save the code.
What Is Boto3?
This Python library provides a comprehensive interface for interacting with various AWS services, allowing developers to automate tasks and integrate AWS services into their Python applications.
Amazon DynamoDB is a fully managed, serverless, key-value NoSQL database designed to run high-performance applications at any scale. DynamoDB offers built-in security, continuous backups, automated multi-Region replication, in-memory caching, and data import and export tools.
Prerequisite
· IAM user with programmatic access
· Install boto3
· Install AWS CLI and configure AWS
Let’s dive into the project!
Section1 > Step 1: Create a Table
We need to upload the Python code to create your table onto your IDE. My table will list the top 10 Hulu Movies for the month. Once you run your code- you will see it is “creating” on the terminal. If successful, you can locate your table on the Dynamo DB console!
Step 2: Connect Cloud 9 Environment Dynamo DB table
Use the code below to access dynamodb using your was credentials.
Step 3: Add Items to Your Table
Use the code in the image below to add items to your table.
Step 4: Head to your table using the Dynamodb console and make sure your items are there:
Section 2 > Query, Remove, Scan, and Delete Table.
Step 1: Query
In order to perform our remaining tasks, we need to get the data from the table using query :
As you can see from the image above, my query was for the movie that ranked number 7, which returned ‘Hating game.’
Step 2: Remove
If we don’t want to see the movie ranked number 7, we will remove it. That way, we only have 1,2,3,4,5,6,8,9, and 10 excluding 7:
Step 3: Scan
Now let's check to see if it was deleted from our table by performing a scan: using the code below.
As you can see, Hating Game #7 has been excluded from our table.
Step 4: Delete Table
When you are ready to delete the table (make sure you are ready before you perform this step!) then, you will input the code below to your Cloud9:
As you can see, we successfully deleted the table.
This lab is now complete, and we have completed this project. Thanks for taking the time to read my article for the week.
The link below is the link to my GitHub repo for all the codes used in this project: https://github.com/Abidoye95/aws-boto3-automation/blob/main/dynamo_dbtables.py