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:
- Sign in to the AWS console.
- In the search bar at the top of the screen type “DynamoDB” .
- Choose the “Tables” option at the left of the screen.
- Click the “Create table” button.
- Fill in the form.
- Scroll down and click the “Create table” button.
- Wait for the table to be created.
- 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:
- Fill in the form.
- In the “Settings” section, choose the “Customize settings” option. 3.Click the “Create local index” button.
- 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.
- Click the “Create table” button.
- 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:
- Click the table’s name.
- Go to the “Indexes” tab and click the “Create index” button.
- Fill in the form. Keep in mind that the “Partition key” and “Sort key” names must be fields in the table.
- Scroll down and click on the “Create index” button.
- Go to the “Overview” tab and click the “View items” button located in the “Items summary”.
- Click the “Create item” button.
- Add values to the “Partition key” and the “Sort key”.
- Add a new attribute, which is the GSI’s primary key. After that, click on the “Create item” button.
- The item is created! Go to the table’s overview by clicking the “View table details” button.
- 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
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!