Introduction: DynamoDB in the AWS Ecosystem

Welcome to the next step in your AWS development journey! So far, you have learned how to securely access AWS services from EC2 instances and how to work with S3 for cloud file storage. In this lesson, we will focus on DynamoDB, Amazon’s managed NoSQL database service.

DynamoDB is designed for fast and flexible data storage and retrieval. It is often used for applications that need to handle large amounts of data with low latency, such as user profiles, session data, or product catalogs. By the end of this lesson, you will know how to create a DynamoDB table and perform the four basic operations: Create, Read, Update, and Delete (CRUD).

This lesson will build on your experience with the boto3 library in Python but will focus only on what you need to get started with DynamoDB. Let’s get started!

Recall: Using boto3 to Access AWS Services

Before we dive into DynamoDB, let’s quickly remind ourselves how we use boto3 to connect to AWS services in Python. You have already used boto3 to work with S3, and the process is very similar for DynamoDB.

To use boto3, you typically import the library and create a resource or client for the AWS service you want to use. For example, to work with S3, you wrote:

For DynamoDB, you will use:

This line creates a DynamoDB resource object, which you will use to interact with your tables. On CodeSignal, boto3 is already installed, so you do not need to worry about setup here, but remember how to do this for your own environment.

Creating a DynamoDB Table in Python

Let’s start by creating a DynamoDB table. In DynamoDB, a table is where your data is stored. Each table needs a name and a primary key. The primary key uniquely identifies each item in the table.

Here’s how you can create a table named Users_<random> with a primary key called user_id:

Let’s break this down:

  • TableName=TABLE_NAME sets the name of the table. Here, we use a random suffix to avoid name conflicts.
  • KeySchema defines the primary key. In this case, user_id is the only key, and it is a "HASH" key (the partition key).
  • AttributeDefinitions tells DynamoDB that user_id is a string ("S").
  • BillingMode='PAY_PER_REQUEST' means you pay only for what you use, which is good for learning and small projects.

After creating the table, you need to wait until it is ready to use. Here’s a helper function to do that:

You call this function after creating the table:

Performing CRUD Operations on DynamoDB

Now that you have a table, let’s go through the four basic operations: Create, Read, Update, and Delete.

Create: Adding a New Item

To add a new item (a row) to your table, use the put_item method:

This adds a user with ID u-123, name Ada, and email ada@example.com to the table.

Read: Retrieving an Item

To get an item by its primary key, use get_item:

Sample Output:

Update: Modifying an Existing Item

To update an item, use update_item. For example, to change the email address:

  • UpdateExpression tells DynamoDB what to change.
  • ExpressionAttributeValues provides the new value.
  • ReturnValues="ALL_NEW" returns the updated item.

Sample Output:

Delete: Removing an Item

To delete an item, use delete_item:

Sample Output:

Cleaning Up: Deleting the Table

It’s important to clean up resources you no longer need. To delete the table:

Sample Output:

Deleting unused tables helps you avoid unnecessary costs and keeps your AWS account tidy.

Summary and Practice Preview

In this lesson, you learned how to create a DynamoDB table and perform the four basic CRUD operations using Python and boto3:

  • Creating a table with a primary key
  • Adding, reading, updating, and deleting items
  • Cleaning up by deleting the table

You saw step-by-step code examples and learned what each part does. In the next practice exercises, you will get hands-on experience with these operations. Remember to pay attention to table names and primary keys, and always clean up your resources when you are done. Good luck, and enjoy working with DynamoDB!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal