DynamoDB Create Table

Maybe it’s my background as a Web Developer, but whenever learning a new development stack I want to build a CRUD(Create, Read, Update, Delete) application first thing. Step one in creating a CRUD application for a database is create a table. In this post let’s see how quickly we use DynamoDB Create table from both AWS console and CLI.

DynamoDB is a NoSQL database built by Amazon for both the AWS cloud and off premise. The non-relational database supports both Key-Value and Document models. In this walk through we will focus on the key-value model. If you are interested in learning more DynamoDB Commands checkout 11 DynamoDB CLI commands.

Table Example

In this tutorial let’s create a table for global college team registry. We want to track name, shortname, mascot, colors, and location. For example the University of South California would contain the following:

Name – University of South California

ShortName – USC

Mascot – Trojan

Colors – Cardinal, Gold

Location – Los Angeles, CA

Before loading our data into the table we are going create a table and assign data types to the items.

DynamoDB Create Table From Console

Let’s walk through the steps to creating a table in DynamoDB to track College Teams from the AWS Console. Make sure to log in and navigate to the DynamoDB service.

Create Table – Our table name is ‘collegeteam‘. The table name must be unique per AWS region.

Partition Key (Primary Key) – Primary Key is a combination of the Partition Key (that we are entering here) and Sort Key (if sort key is used). Since we are using both Parition Key and Sort Key DyanmoDB will hash the keys across the AWS region. For our college teams table our Partition Key for sorting our teams is ‘name’ (string).

Add Sort Key -The sort key is additional key to enrich queries associated with the Primary Key. All queries will be associated with the Primary Key. For the college teams table we have ‘name’ as our primary key, lets’ add the ‘location’ as our sort key.

Secondary Indexes – Remember we added additional sort keys tied to our Primary Key? We can use Secondary Indexes when we want to setup indexes independent of the Primary Key. For the college team tables let’s set a secondary index with ‘mascot’ (string) as the Partition Key (Primary Key) and ‘colors’ (string) as the Sort Key. The mascot secondary index will have separate read/write capacity units from the primary index.

Creating DynamoDB Table From CLI

In the first example we used the AWS console to create our college team table, but now let’s use the same example to create a DyanmoDB table from AWS CLI. Before creating the table make sure we have the AWS CLI configured.

DyanmoDB Create Table – All of the different options for the college team table are passed into the ‘aws dynamodb create-table’ command.