Exercise: Amazon DynamoDB

Natasha Ong
This is some text inside of a div block.
4 min read

Exercise Overview:

In this exercise, let's say you are a music producer wanting to store information about iconic 2000's artists and songs. Because there's going to be a lot of artists in this, you want to make sure there is some way you can search within the database you make.

You will create a table in DynamoDB to store information about a music library. You will query the music library and then delete the DynamoDB table.


By the end of this exercise, you should be able to do the following:

  • Create an Amazon DynamoDB table.
  • Enter data into an Amazon DynamoDB table.
  • Query an Amazon DynamoDB table.
  • Delete an Amazon DynamoDB table.

Task 0: Accessing the AWS Management Console

  1. Sign in to your IAM user and open the AWS Management Console.
  2. In the top right corner, select the Region in which you want to create the table. In this exercise, choose N.Virginia.
  3. Navigate to DynamoDB. If you can't find it, simply search DynamoDB in the search bar at the top. You could also click on the nine dots on the top left-hand corner of the console, then select the Database option on the left-hand panel that pops up. DynamoDB should be the second option in the list.

Task 1: Create a new table

In this task, you will create a new table named Music in DynamoDB. Each table requires a partition key (or a primary key) that is used to partition data across DynamoDB servers. A table can also have a sort key. The combination of a partition key and a sort key uniquely identifies each item in a DynamoDB table.

  1. Choose Create table.
  2. For the Table name, enter Music
  3. For the Partition key, enter Artist and leave String selected in the dropdown list.
  4. For the Sort key - optional, enter Song and leave String selected.
  5. Your table will use the Default settings for indexes and provisioned capacity. Customize settings is not required for us as we won't be using advanced features!
  6. Scroll down, and choose Create table.

Task 2: Add data

In this task, you will add data to the Music table. You want this table to be a go-to space for people to check artists and details about their songs!

A table is a collection of data on a particular topic.
Each table contains multiple items. An item is similar to a row in a spreadsheet. Each item is uniquely identifiable by a primary key, which is composed of one or two attributes (more on attributes below). Specifically, the primary key could be a single-attribute partition key or a combination of a partition key and a sort key.
Each item consists of one or more attributes. An attribute is a fundamental data element, something that does not need to be broken down any further. For example, an item in a Music table contains attributes such as song and artist. Attributes in DynamoDB are similar to columns in other database systems, but each item (row) can have different attributes (columns).
  1. Choose the Music table.
  2. Choose Actions, and then choose Create item.
  3. For the Artist value, enter Bruno Mars
  4. For the Song value, enter 24K Magic

These are the only required attributes, which means these are the only things every single item in the Music table must have. But, you know a thing or two extra about Bruno Mars' song. How can we record this? You will now add additional attributes.

5. To create an additional attribute, choose Add new attribute. This is a dropdown in the top right corner of the Attributes section.

6. In the dropdown list, select String. A new attribute row will be added:

  • Attribute Name: Album
  • Value: 24K Magic

Let's say we want to add a new attribute - this time we want to say the year the song was produced.

Which of the following attributes should we use... String, Number, Boolean or List? Take your pick before we move on and find out!

7. To add another new attribute, choose Add new attribute.

8. In the dropdown list, choose Number:

  • Attribute Name: Year
  • Value: 2016

9. Choose Create item.

The item has now been added to the Music table.

Let's create another item, using the following attributes:

This item has an additional attribute called Genre. This is an example of each item in a non-relational database being capable of having different attributes. There is no need for you to pre-set the attributes to fill in a non-relational database! 

To create a third item, use the following attributes:

Once again, this item has a new LengthSeconds attribute identifying the length of the song. We love the flexibility of a NoSQL database. 👑

Task 3: Modify an existing item

Oops, turns out the album by Taylor Swift was launched in the year 2023, not 2022. Let's correct this data!

  1. In the DynamoDB console, under Tables, choose Explore Items.
  2. Choose Music.

3. Click on Taylor Swift

4. Change the Year from 2022 to 2023.

5. Choose Save and close. Nice, the Taylor Swift item is now updated.

Task 4: Query the table

Let's say you have thousands and thousands of artists and songs in your database. How do we quickly find our Taylor Swift item? This is where querying comes in.

There are two ways to query a DynamoDB table: query and scan. A query is the most efficient way to retrieve data from a DynamoDB table. Alternatively, you can scan for an item. This option involves looking through every item in a table, so it is less efficient and can take significant time for larger tables.

A query operation finds items based on the primary key and optionally the sort key. It is fully indexed, so it runs very fast.

  1. In the same page (with the header Music), expand the dropdown called Scan/Query items, and choose Query.

Fields for the Artist (Partition key) and Song (Sort key) are now displayed.

2. Enter the following details:

  • Artist (Partition key): Taylor Swift
  • Song (Sort key): Enchanted

3. Choose Run.

The song quickly appears in the list. You might need to scroll down to see this result.

4. Scroll up on the page, and choose Scan.

5. Expand Filters, and enter the following values:

6. For the Attribute name, enter Year

7. For Type, choose Number.

8. For Value, enter 2007

9. Choose Run.

Magic! Only the song released in 2007 is displayed:

Task 5: Delete the table

In this task, you will delete your Music table, which will also delete all the data in the table.

1. In the DynamoDB dashboard, under Tables, choose Update settings.

2. Choose the Music table if it is not already selected.

3. Choose Actions, and then choose Delete table.

4. On the confirmation panel, enter confirm and choose Delete.

The table was successfully deleted.

Congratulations! You now have successfully:

  • Created an Amazon DynamoDB table.
  • Entered data into an Amazon DynamoDB table.
  • Queried an Amazon DynamoDB table.
  • Deleted an Amazon DynamoDB table.