Demo: Creating My First Table in DynamoDB

Learn how to create a DynamoDB database. Additionally, you’ll learn how to add an LSI and/or a GSI to it.

In this lesson, we’ll create some DynamoDB tables. We’ll start by creating a table with the default configuration and another one with a Local Secondary Index. Additionally, we’ll also add a Global Secondary Index to the first table.

Creating a table with a default configuration

Creating a table in DynamoDB is a straightforward process. To do so, you can follow these steps:

  1. Sign in to the AWS console.
  2. In the search bar at the top of the screen type “DynamoDB” .
  3. Choose the “Tables” option at the left of the screen.
  4. Click the “Create table” button.
  5. Fill in the form.
  6. Scroll down and click the “Create table” button.
  7. Wait for the table to be created.
  8. Your table is now created!

Creating a table with an LSI

To create a table with an LSI, let’s start from point number five in the previous steps:

  1. Fill in the form.
  2. In the “Settings” section, choose the “Customize settings” option. 3.Click the “Create local index” button.
  3. Fill in the form. Keep in mind that the “Sort key” name must be a field in the table. After filling out the form, click the “Create Index” button.
  4. Click the “Create table” button.
  5. The table with an LSI is created.

Adding a GSI to an existing table

Once a table is created, it is possible to add a GSI to it. To do so, it is important to execute the following steps:

  1. Click the table’s name.
  2. Go to the “Indexes” tab and click the “Create index” button.
  3. Fill in the form. Keep in mind that the “Partition key” and “Sort key” names must be fields in the table.
  4. Scroll down and click on the “Create index” button.
  5. Go to the “Overview” tab and click the “View items” button located in the “Items summary”.
  6. Click the “Create item” button.
  7. Add values to the “Partition key” and the “Sort key”.
  8. Add a new attribute, which is the GSI’s primary key. After that, click on the “Create item” button.
  9. The item is created! Go to the table’s overview by clicking the “View table details” button.
  10. Go to the “Indexes” tab, where you can find the new GSI.

Example of a table creation

To create a table with on-demand capacity, execute the following command:

aws dynamodb create-table \
--table-name my-table-cli \
--attribute-definitions AttributeName=id,AttributeType=S AttributeName=name,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH AttributeName=name,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST
Terminal 1
Terminal
Loading...

Key takeaways

In this lesson, we explored how to create tables in DynamoDB and how to add an LSI and GSI. Hopefully, these steps were quite straightforward to follow!