Traveling across the globe with Python & DynamoDB

Stacey Montes
Women in Technology
8 min readApr 17, 2023
Photo by Stacey from Canva

Hello to all my followers and ‘Welcome’ to any new techies out there! I appreciate y’all checking in week after week! It motivates me to keep on writing articles!

I know I don’t personally know a lot of you, but you should know besides my curiosity to learn, I LOVE traveling! So fair warning, this article may cause you to want to book your next travel destination! All jokes aside, this week we are diving deeper into Python, and specifically with DynamoDB within AWS. As always, I will explain in detail the script and provide you all the tools you will need to follow along! Let’s discuss some basics before taking off for flight around the globe with DynamoDB.

Credit

Python — a universal computer programming language that can be used to build just about anything, such as websites and software, automation tasks, and so much more. Professional developers use Python for backend web development, data analysis, artificial intelligence and scientific computing. It's easy-to-understand syntax makes it popular among beginners, and its versatility has earned it the top spot as the most popular introductory language at top U.S. universities. References

Boto3 — is the name of the Python software development kit (SDK) for AWS. It allows you to directly create, update, and delete AWS resources from your Python scripts. The Boto project started as a customer-contributed library to help developers build Python-based applications in the cloud, converting application programming interface (API) responses from AWS into Python classes. Reference Boto3 AWS Documentation

DynamoDB — DynamoDB is a fully managed, serverless, key-value NoSQL database designed to run high-performance applications at any scale. You can use DynamoDB to create a database table that can store and retrieve any amount of data across any level of request traffic. It automatically spreads the data and traffic for the table over servers to handle the request capacity and the amount of data stored, while maintaining consistent and fast performance. We will be using it today to add 10 must-visit destinations around the world! References

For this article we are going to be using ‘Search’ and ‘Query’ options in DynamoDB. Let’s quickly recap the differences of these. DynamoDB offers two ways to access information stored: Query and Scan.

A Query will rely on the primary-key to find information. Query can point directly to a particular item and retrieve them in a fast and efficient way.

Scan will browse table items from start to finish. This can be expensive and inefficient, and we don’t want that! Unless you really need to browse the entire set of items, you should use the Query option.

Querying by the primary-key is the most efficient manner of retrieving data from a DynamoDB table. References

AWS Cloud9 — is a cloud-based integrated development environment (IDE) that lets you write, run, and debug your code with just a browser. It includes a code editor, debugger, and terminal. Cloud9 comes prepackaged with essential tools for popular programming languages, including JavaScript, Python, PHP, and more, so you don’t need to install files or configure your development machine to start new projects. References

Now that we have a better understanding of the topics in this article, let’s get ready to travel!

Credit

Scenario: You work for a startup company that has developed a new application that provides users with personalized recommendations for places to travel. The application is built on AWS infrastructure and uses DynamoDB as the database. Your role as a DevOps engineer is to write Python scripts that will create a knowledge base using AWS DynamoDB. The knowledge base will contain the 10 must-visit places! You also must scan and query the table, delete an item, and then delete the table.

Prerequisites:

  • Computer with internet access
  • Basic Python knowledge
  • Cloud9 familiarity (or IDE of choice)
  • AWS Account with IAM user
  • AWS familiarity

Let’s get to finding out about these must-visit destinations for any upcoming vacations! First things first, we need to install Boto3 on Cloud9. We are going to be running each of our Python scripts on Cloud9 and then checking on the AWS console to ensure the script is correct! Here is the AWS Boto3 documentation for DynamoDB!

So let’s go ahead and log into our AWS account and search for Cloud9. Go ahead and create an environment and launch it. From there you are going to want to run the following code:

pip install boto3

Note: If you are using another IDE, you may have additional steps to complete in order to have communication between your IDE and your AWS account.

  1. Create a DynamoDB table

In this step we are going to be using Cloud9 to run our Python script in order to create a DynamoDB table. Remember that we are going to create a knowledge base with 10 must-visit destinations! So we must include ‘Country’ and ‘City’.

In the above script, the code creates a DynamoDB table named Bucket_List with a partition key attribute named Country and a sort key attribute named City. You'll need to replace these values with your own table name, partition key, and sort key attributes.

You can adjust the provisioned throughput values based on your expected usage. In this example, it’s set to 1 read capacity units and 1 write capacity units. When you run this code, you’ll see the current status of the table printed to the console. Let’s now head on over to our AWS console to see if it worked!

Success!

2. Add items to the DynamoDB table

Time for the fun part! Adding the must-visit destinations! If any of my followers have been to these locations let me know how it was! Now that we have our table, let’s go ahead and try to add a destination. For simplicity’s sake, I am going to input 1 destination first to ensure my code is functioning. Afterwards, I will add the remaining must-visit destinations to the script! Below is my entire script to add all 10 destinations.

1 destination was added

This is a great sign! Let’s check AWS console now to ensure our first destination was added.

Success!

Yes! Now we can add the remaining 9 destinations! Let’s go! Ok, I’m not going to lie, this script took me a bit when adding each of the destinations. I had lot’s of indentation errors. But now you won’t have that issue! Let’s run it on Cloud9.

Success! Now time to check AWS console!

In this script we created a batch_writer object for the table object we created. The batch_writer is a context manager that allows you to write multiple items to the table in a batch. The with statement ensures that the batch_writer is properly closed when the block of code is finished.

By the way, what do you think of those must-visit destinations? If you want to add places or even things to do in any of these, let me know! I am already getting a travel itch!

Credit

3. Scan the DynamoDB table

Now we have to scan the table! Let’s break down this script.

Line 5 will retrieve the DynamoDB table named ‘Bucket_List’ and stores it in a variable called ‘table’. Line 8 then calls the scan method of thetableobject, which retrieves all items in the table, and stores the response in a variable called response.

Line 10’s code then extracts the list of items from the response object by accessing the Items key and storing it in a variable called items. And finally, the code iterates over the items list using a for loop and prints each item in the list.

Moment of truth when we run the code!

4. Query the DynamoDB table

Now it’s time toQuerythe table for a specific item. Let’s look for ‘Las Coloradas, Mexico’. This place has bubble-gum pink waters! Don’t believe me, check it out yourself!

Before I finalized this code, I ran into a lot of errors. The main one resulted in this output

After doing some researching, I was able to figure it out. The error indicates that the Key object is not imported or defined in the current module. To fix this error, you need to import the Key object from the boto3.dynamodb.conditions module. This allows us to use Key to define the key condition expression for the query. I am referring to Line 4 in the script. Once I added that, it worked!

Success!

5. Delete an item

We are so close to the finish line! Can you believe you are learning about Python, Boto3, and travel destinations?! I certainly can’t! But regardless, here is the script for deleting an item below!

Using Cloud9, let’s run the code!

Now time to check the AWS console to ensure it deleted. We should only have 9 destinations now.

6. Delete the table

Last step for this article! Unless you include booking your travel plans to any of these GORGEOUS destinations! But here we are going to want to delete the DynamoDB table using Python. Let’s check it out!

Remember that my DynamoDB table is called Bucket_List. You can adjust this code to delete different tables in different regions. Note that once a table is deleted, its data is permanently lost, so use this code with caution.

Let’s run it on Cloud9!

Now let’s check our AWS console to ensure it has been deleted!

Success!

Well ladies and gentlemen, believe it or not you have just created many Python scripts that create, scan, query, deletes an item, and deletes a DynamoDB table! This is a HUGE deal! And the best part? You didn’t have to spend hours researching or perfecting the script like I did 😂

Remember, this is just 1 of MANY different approaches to complete this task! Keep joining me along this journey as I keep diving deeper and deeper in Python! Safe travels!

Credit

Stacey Montes is a Cloud/DevOps Engineer who is transitioning from the healthcare field. Join her along her journey by following her on LinkedIn.

--

--

Stacey Montes
Women in Technology

DevOps/Cloud Engineer | AWS Certified Developer | Terraform Certified Associate