Using Python and Boto3 to Create an AWS DynamoDB Table

Matt Dixon
6 min readMar 4, 2023

--

Creating a DynamoDB Table using Boto3 and Python

For this week’s project, we’ve been tasked with using our Python and Boto3 skills to create a DynamoDB table for something of our choosing. Since I love guitar, I’m going to add my favorite guitar players and what band they currently play with.

Project Parameters:

  1. Create a DynamoDB table for something of your choosing (e.g. movies, food, games).
  2. Using the Gist (https://gist.github.com/zaireali649/0ec6b90155120cf508223788b7b86efc) as a starting point, use boto3 and Python to add 10 or more items to the table.
  3. Use boto3 and Python to scan the DynamoDB table.

Let’s do a quick recap — Amazon DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services (AWS). It offers a fast and flexible data model with consistent, single-digit millisecond latency at any scale. DynamoDB provides reliable performance and automatic scaling of throughput capacity to meet the demands of even the largest applications. It also offers built-in security features, such as encryption at rest and in transit, and integrates seamlessly with other AWS services. DynamoDB is a popular choice for applications that require low-latency data access, high scalability, and ease of management.

Boto3 is the AWS software development kit (SDK) for Python, which provides an easy-to-use Python interface to interact with various AWS services. Boto3 allows developers to write Python code to create, configure, and manage AWS services, such as Amazon S3, EC2, DynamoDB, and many others. This powerful SDK affords developers with the ability to provision infrastructure through code.

I’ll be demonstrating this week’s project in AWS Cloud9 and already have my environment configured and setup so I won’t be covering that particular step today. I will cover that we need to install Boto3 in order to continue.

To use Boto3 with Python and AWS services, we’ll first need to install the Boto3 library in our Python environment using pip. Once installed, you can use Boto3 to connect to an AWS service by creating a client or resource object. We’ll use the following command to install Boto3:

pip install boto3

I’ve already done and completed that step so I haven’t screen captured it here.

I’ve written my code and used my access key id and secret access key so that my Python program can connect to AWS. I’ve blanked it out here. I’ve also gotten in the habit of running chmod 744 on my scripts and code that connects to AWS so that I have read / write / execute and group and others have read-only access.

Python script for project to create DynamoDB table.

I’ve executed the code and it’s run successfully so we’ll head back over to DynamoDB and verify that the “Guitar_Players” table has been created. We can see that the table status in the terminal window is listed as “Creating”.

Guitar_Players Table created successfully!

Great! Now we’re going to populate this table with some data to include 19 of my favorite Guitar players in a Python dictionary that includes key pairs of players and the bands they play with. The project requirement is 10, but between the fact that I like to go a bit further than what’s required, I had a hard time narrowing down my favorite guitar players to only 10, so 19 it is!

Python code to add 19 of my favroite guitar players into the table.

We can see that the table status has changed to “Active” in the terminal window now after the code has been executed. Let’s head back over to DynamoDB and have a look at the table, “Guitar_Players”. From the DynamoDB dashboard, we’ll select the table, “Guitar_Players” and click Explore Items.

Exploring the table Guitar_Players

This is perfect — from here we can see that the table has 19 items returned, which is the exact number of key pairs in the Python dictionary. Awesome!

Next, we’re going to write some Python code to scan the DynamoDB table from the terminal and get the contents of the table and display them.

Scanning the DynamoDB table with Python

The scan using Python code was successful. Notice that we got a scanned item count of 19, an HTTP Status Code of 200, indicating a successful scan. I ran the scan but since I have 19 items in the table, they wouldn’t all fit cleanly on the screen so I redirected the output from the Python code execution to a text file listed below:

{
"Items": [
{
"Player": {
"S": "Tom Petty"
},
"Band": {
"S": "Tom Petty & The Heartbreakers"
}
},
{
"Player": {
"S": "Mick Mars"
},
"Band": {
"S": "Motley Crue"
}
},
{
"Player": {
"S": "Vivian Campbell"
},
"Band": {
"S": "Def Leppard"
}
},
{
"Player": {
"S": "Jack Sizemore"
},
"Band": {
"S": "Jason Aldean"
}
},
{
"Player": {
"S": "Carl Bell"
},
"Band": {
"S": "Fuel"
}
},
{
"Player": {
"S": "Kurt Allison"
},
"Band": {
"S": "Jason Aldean"
}
},
{
"Player": {
"S": "Sameer Bhattacharya"
},
"Band": {
"S": "Flyleaf"
}
},
{
"Player": {
"S": "Dave Mustaine"
},
"Band": {
"S": "Megadeth"
}
},
{
"Player": {
"S": "Jimi Hendrix"
},
"Band": {
"S": "The Jimi Hendrix Experience"
}
},
{
"Player": {
"S": "Eric Clapton"
},
"Band": {
"S": "Cream"
}
},
{
"Player": {
"S": "Nate Milstein"
},
"Band": {
"S": "Nashville-based Guitarist"
}
},
{
"Player": {
"S": "Mark Knopfler"
},
"Band": {
"S": "Dire Straits"
}
},
{
"Player": {
"S": "Phil Colen"
},
"Band": {
"S": "Def Leppard"
}
},
{
"Player": {
"S": "Keith Urban"
},
"Band": {
"S": "Keith Urban Band"
}
},
{
"Player": {
"S": "Dan Estrin"
},
"Band": {
"S": "Hoobastand"
}
},
{
"Player": {
"S": "Dallas Perry"
},
"Band": {
"S": "The Vanished"
}
},
{
"Player": {
"S": "Slash"
},
"Band": {
"S": "Guns N' Roses"
}
},
{
"Player": {
"S": "Todd Howard"
},
"Band": {
"S": "Granger Smith"
}
},
{
"Player": {
"S": "Eric Weaver"
},
"Band": {
"S": "12 Stones"
}
}
],
"Count": 19,
"ScannedCount": 19,
"ResponseMetadata": {
"RequestId": "0JS5O2HQHD06B6A84LD4D04DPRVV4KQNSO5AEMVJF66Q9ASUAAJG",
"HTTPStatusCode": 200,
"HTTPHeaders": {
"server": "Server",
"date": "Sat, 04 Mar 2023 08:40:00 GMT",
"content-type": "application/x-amz-json-1.0",
"content-length": "1190",
"connection": "keep-alive",
"x-amzn-requestid": "0JS5O2HQHD06B6A84LD4D04DPRVV4KQNSO5AEMVJF66Q9ASUAAJG",
"x-amz-crc32": "284693514"
},
"RetryAttempts": 0
}
}

This was another super fun project! I’ve found that I LOVE Python because it’s so easy to follow proper code syntax, write in a pretty high level natural spoken type of language and here, we’ve seen how simple it is to interact wtih AWS Services. Python is awesome because it handles anything from simple tasks out of the box, and there’s thousands of libraries that are a simple import statement away to extend the capabilities into complex problem solving machine.

Thanks for tuning in and be sure to check out my next project which will be coming in a few days.

--

--