Flask + Amazon DynamoDB = CRUD Application.


Hey! How’s Everyone Doing?

I recently learned about Amazon DynamoDB for a competition on HackerEarth. I was amazed by how good this database is and how easy it is to work with DynamoDB by creating simple CRUD operation with none other than our favorite language — JavaScript …. lol, just kidding its Python. Why wait? Let us begin.

As usual, the first thing before starting development of any application or project is Requirement Gathering. So We will need a couple of things to start with this project.

  1. Amazon DynamoDB
  2. Python and Java

That’s it you are done.

Now let us talk about the application which comes under the Planning Phase. We will create a simple CRUD operation with Flask and Amazon DynamoDB.

Let us use book information data for the CRUD operation.

Our book data will consist of JSON where bookID will be our Primary Key; the rest of all data can be optional.

  1. First, we need to start our DynamoDB, so head towards the downloaded DynamoDB folder and run this command in Command Prompt
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar

output should look like this —

2. Install the python dependencies.

pip install boto3 flask

3. Create a Table and Load the data using a python file.

python import_data.py

4. Now create a new python file for the flask app. — app.py and run.

python app.py

5. Start with the first function that is the hello world and open localhost:5000 on the browser to check the response. It should show Hello World!

6. Create data table item object.

Now, we will start with the CRUD operation.

  1. Create an Item in DynamoDB. Here bookID is very important since it is our primary key so without bookID, we cannot create an item in our table.

2. Read an Item from DynamoDB.

3. Update an Item from DynamoDB.

4. Delete an Item from DynamoDB.

All the endpoints —

url1 = "http://localhost:5000/add_book"
url2 = "http://localhost:5000/get_book/10"
url3 = "http://localhost:5000/update_book/10"
url4 = "http://localhost:5000/remove_book/10"

You can test all the endpoints if you like

  • Create an Item in DynamoDB.
data = {
'authors': 'J.K. Rowling',
'average_rating': '4.20',
'bookID': '10',
'isbn': '439827604',
'language_code': 'eng',
'price': '100',
'ratings_count': '27410',
'title': 'Harry Potter Collection (Harry Potter #1-6)'
response = requests.post(url4, data=json.dumps(data))
  • Read an Item from DynamoDB.
response = requests.get(url2)
  • Update an Item from DynamoDB.
data = {
'price': '100',
'average_rating': '4.20'
response = requests.post(url3, data=json.dumps(data))
  • Delete an Item from DynamoDB.
response = requests.get(url4)

That’s it. You are done. You created your first application with DynamoDB and Flask. You can find full source code here. Cheers happy learning.

Automation with AI ❤️