Heading image

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:

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.

  1. Instantiating the table.
  2. Add a global secondary index GSI1.
  3. Add a global secondary index GSI2.
  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.

Instantiating the table

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

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.

Adding the construct to our app

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

Deploying to LocalStack

The docker-compose.yml file already has what is required for us to startup 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.

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

--

--

--

Senior Engineer @ UsabilityHub. Formerly Culture Amp.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Yes, More Callbacks — The Kernel Extension Mechanism

2018 in retrospect.

Istio by Example

DFINITY 2019 — Community Review

#AzureNotes: Mind map of Azure Compute services

How to Package Java Projects in Python Tar files

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dennis O'Keeffe

Dennis O'Keeffe

Senior Engineer @ UsabilityHub. Formerly Culture Amp.

More from Medium

How to build and package Node.js Lambda code along with dependencies in Docker

Accelerating AWS deployment when using Typescript

Setup Gitlab CI/CD Pipeline to Host Next.js App in AWS Lambda

TypeScript Types for AWS Lambda