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.
- Basic familiarity with AWS CDK for TypeScript.
- Read the first two posts in the “Working with the TypeScript AWS CDK” series.
- Familiary with DynamoDB. This post will not be an overview of DynamoDB development.
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
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:
- Instantiating the table.
- Add a global secondary index
- Add a global secondary index
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:
tablePropsto extend our table without allow the user to alter the
GSI1Propsto extend the first global secondary index without being able to alter the
GSI2Propsto extend the second global secondary index without being able to alter the
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
We can see this happen in the file
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
At this stage, we are ready to synthesize and deploy our stack to LocalStack.
Deploying to LocalStack
docker-compose.yml file already has what is required for us to startup LocalStack.
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.
Perfect! We have successfully constructed our DynamoDB table onto LocalStack.
As always, we will also teardown the stack:
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
- AWS CDK for TypeScript
- Final code
- “Working with the TypeScript AWS CDK” series
- AWS CDK With TypeScript Foundations
- Using The AWS CDK With LocalStack And
Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.