DynamoDB + AWS CDK - The Ultimate Guide w/ Examples
Written by Chameera Dulanga
Published on July 3rd, 2022
Time to 10x your DynamoDB productivity with Dynobase [learn more]
Why DynamoDB with AWS CDK?
AWS CDK provides separate libraries in all the above languages for DynamoDB. You can use those libraries to create and manage DynamoDB tables easily. In addition, it allows configuring billing models, scaling options, global tables, encryption, and many other DynamoDB features with minimum effort. So, let's see how we can use AWS CDK with DynamoDB.
In this article, I will be using AWS CDK for TypeScript to demonstrate examples.
So, make sure you have:
You should be able to run the following commands without any issues:
Step 1 - Initializing a New Project with AWS CDK
After installation, you can create a new project using AWS CDK as below:
To get started, AWS CDK provides 3 templates named app, lib, and sample-app. The above command generates a project using the sample-app template.
Once the project is initialized, you will find a file named
aws-cdk-dynamodb-stack.ts in the
lib folder. By default, it will look like the below:
Since we are focusing on DynamoDB, you can remove all the code related to SNS and SQS and clear the file for the next steps.
Step 2 - Creating a New DynamoDB Table using AWS CDK
With AWS CDK, you can easily create a DynamoDB table by only passing 2 properties:
DynamoDB provides 2 billing modes, and the Pay Per Request mode has been selected in the above example. If you wish to use Provisioned mode, you can simply change the
billingMode property to
In addition, you can configure read and write capacities, sort key, table class, local secondary index, and point in time recovery when creating a DynamoDB table using AWS CDK. However, it is not mandatory to configure all these properties.
Similarly, you can easily use tools like Dynobase to create DynamoDB tables with few clicks. Learn More.
Step 3 - Auto Scaling Configurations
If you choose Provisioned billing mode for your table, you must manually configure your table's read and write capacity units. With AWS CDK, you can easily create scale-up or scale-down events for specific times of the day.
Step 4 - Creating Global Tables using AWS CDK
DynamoDB Global Tables play a vital role in disaster recovery. With AWS CDK, you can easily configure Global Tables. The below code examples shows the minimum configuration you need to create Global Tables in 3 different regions.
By default, Global Tables will have Pay Per Request billing mode. But, if you need, you can change it to the Provisioned mode by including the
billingMode: dynamodb.BillingMode.PROVISIONED property in the table definition.
In addition, default Global Tables will have a default replication timeout period of 30 minutes. However, if you have a large-scale table, it is advised to increase the timeout duration using
Step 5 - DynamoDB Table Encryption using AWS CDK
As we all know, DynamoDB supports encryption at rest. When creating a table, you can select AWS-owned CMK, AWS-managed CMK or Customer-managed CMK as the encryption option. By default, AWS DynamoDB tables are encrypted with a master key owned by AWS.
The below examples show how to encrypt your DynamoDB tables with Customer managed and AWS manages CMKs using AWS CDK.
You can also create your own encryption key using AWS KMS and pass it to the table definition as shown below:
Step 6 - Kinesis Stream using AWS CDK
You can also configure AWS Kinesis Stream on your DynamoDB table using AWS CDK. It can be used to capture item-level changes in the DynamoDB tables.
Step 7 - Deploying
As the final step, you need to deploy all the changes in your project using
cdk deploy command. It will take some time to provision the resources in your AWS account, and you will be able to monitor them in the AWS management console.