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!
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.
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_NAMEsets the name of the table. Here, we use a random suffix to avoid name conflicts.KeySchemadefines the primary key. In this case,user_idis the only key, and it is a "HASH" key (the partition key).AttributeDefinitionstells DynamoDB thatuser_idis 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:
Now that you have a table, let’s go through the four basic operations: Create, Read, Update, and Delete.
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.
To get an item by its primary key, use get_item:
Sample Output:
To update an item, use update_item. For example, to change the email address:
UpdateExpressiontells DynamoDB what to change.ExpressionAttributeValuesprovides the new value.ReturnValues="ALL_NEW"returns the updated item.
Sample Output:
To delete an item, use delete_item:
Sample Output:
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.
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!
