Heading image

This post is #4 of a multi-part series on the AWS CDK with TypeScript written during Melbourne lockdown #6.

This post will use the AWS TypeScript CDK to deploy table to DynamoDB on LocalStack with single-table schema design.

The code will build off the work done in the first two articles of the “Working with the TypeScript AWS CDK” series.

The final code can be found here.

Prerequisites

  1. Basic familiarity with AWS CDK for TypeScript.
  2. Read the first two posts in the “Working with the TypeScript AWS CDK” series.
  1. Familiary with DynamoDB. This post will not be an overview of DynamoDB development.

Getting started

We will clone the working repo from the previous blog post as a starting point:

At this stage, we will have the app in a basic working state.

In order to create the CDN stack, we will require the following AWS CDK libraries:

We can install these prior to doing any work:

The repository that we cloned already has a upgrade script supplied. We can use this to ensure our CDK packages are at parity:

We are now at a stage to write our construct.

Writing a single-table construct

In our particular case, I am aiming to write a re-useable construct that has single-table schema design at the forefront as well as being inclusive of two secondary indexes GSI1 and GSI2.

Let’s create a folder lib/construct-single-table-dynamo and add a barrel file and a file for the construct:

We want our construct to do three things:

  1. Instantiating the table.
  2. Add a global secondary index GSI1.
  3. Add a global secondary index GSI2.

As much as possible, we want to invert control to the consumer of the construct while also locking in some best practices around single-table design.

As a starter, let’s prepare our lib/construct-single-table-dynamo/construct-single-table-dynamo.ts with the following code:

This code will enable our construct to take three props:

  1. tableProps to extend our table without allow the user to alter the partitionKey or sortKey.
  2. Optional GSI1Props to extend the first global secondary index without being able to alter the indexName, partitionKey or sortKey.
  3. Optional GSI2Props to extend the second global secondary index without being able to alter the indexName, partitionKey or sortKey.

We will provide sensible defaults here. You are free to alter those props if you want full control after setting sensible defaults!

Let’s now go through and add our table and global secondary indexes.

Instantiating the table

We want to invoke dynamodb.Table to create an instance of a table and pass sensible props for that table.

We will also assign that instance to a publicly available accessor dynamoTable so that the consumer has access to the table when creating our construct within a stack.

Adding the first global secondary index

We can use the addGlobalSecondaryIndex method on an instance of dynamodb.Table to create a global secondary index.

Adding the second global secondary index

We can repeat similar to the above for the second index.

At this stage, we are now able to begin creating DynamoDB constructs in our AWS CDK stacks.

Adding the construct to our app

The repository that we cloned already creates an app with stack AwsCdkWithTypescriptFoundationsStack.

We can see this happen in the file bin/aws-cdk-with-typescript-foundations.ts.

What we can do then is to edit that stack code to create an instance of our constructed DynamoDB table.

Update the file lib/aws-cdk-with-typescript-foundations-stack.ts to have the following code:

This stack will construct our DynamoDB table named my-first-table.

At this stage, we are ready to synthesize and deploy our stack to LocalStack.

Deploying to LocalStack

The docker-compose.yml file already has what is required for us to startup LocalStack.

Run docker compose up for LocalStack to start on Docker.

Once that is started, we can go through our usual lifecycle of the AWS CDK by synthesizing and deploying our app.

At this stage we can check to see our table exists with the awslocal package (or aws with the LocalStack endpoint url http://localhost:4566):

Perfect! We have successfully constructed our DynamoDB table onto LocalStack.

Teardown

As always, we will also teardown the stack:

Summary

Today’s post demonstrated how to create a single-table design DynamoDB table using the AWS TypeScript CDK. It did this by first creating a reusable construct, then followed it up by demonstrating usage of it in an app by deploying to LocalStack.

Tomorrow’s post (as part two) will demonstrate this table in action with DynamoDB Toolbox.

Resources and further reading

  1. AWS CDK for TypeScript
  2. Final code
  3. awslocal package
  4. “Working with the TypeScript AWS CDK” series
  5. AWS CDK With TypeScript Foundations
  6. Using The AWS CDK With LocalStack And aws-cdk-local
  7. DynamoDB

Photo credit: benwksi

Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.

I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to visit my blog and subscribe to my newsletter. Follow me on Twitter.

Senior Engineer @ UsabilityHub. Formerly Culture Amp.