DynamoDB + CloudFormation - The Ultimate Guide w/ Examples
Written by Nishani Dissanayake
Published on May 17th, 2022
Time to 10x your DynamoDB productivity with Dynobase [learn more]
Cloud services are widely used in modern web applications, and it is common to have multiple cloud services used for a single application. Although these services bring many advantages, handling and provisioning them individually is a challenging task.
As a result, various tools and services were introduced to provision cloud resources easily. As developers, it is essential to understand these services to increase development productivity. So, in this article, I will discuss how to manage AWS DynamoDB using CloudFormation with examples.
What is CloudFormation in AWS?
AWS CloudFormation is a service that allows developers to create, provision, and manage collections of AWS services easily. It can manage the entire infrastructure of your application using a single file and quickly replicate the required resources in any environment with few clicks.
How CloudFormation Works with DynamoDB
CloudFormations can be used with DynamoDB to automate the operations, including table creation and autoscaling. For example, DynamoDB global tables are used to seamlessly replicate data over multiple regions. With CloudFormation, we can enable automated deployments for global tables and replicate the changes over multiple regions in real-time.
You can use a JSON file or a YAML file to define the CloudFormation template. AWS CloudFormation provides a set of properties to create and manage DynamoDB tables and its services.
The below example shows the properties available in a DynamoDB CloudFormation template.
Benefits of Using CloudFormation with DynamoDB
Deployment Speed Is High
We can deploy multiple instances of the same resource within seconds using a CloudFormation template. Thus, this method is much faster than manually setting up the deployments.
CloudFormation templates are much more efficient when scaling up the resources and applications. So even if there was no idea to deploy multiple instances initially, you can use CloudFormation templates to scale the resources later.
CloudFormation templates help to reduce the human errors that can happen when manually configuring resources. Also, CloudFormation templates contribute to the security of the applications and resources when designed with security in mind.
DynamoDB Create Table Using CloudFormation
CloudFormation templates can be used to create replicas of the same table across multiple environments within seconds. The below example shows a CloudFormation template for creating a DynamoDB table.
DynamoDB Global Table Using CloudFormation
DynamoDB global tables replicate the same table over multiple regions to ensure uninterrupted and fast accessibility to data. The below example shows how to create DynamoDB global tables replicas 2 regions (us-west-1 & us-east-1 ) using a CloudFormation template.
However, it is essential to have at least one table replica in the same region where we deploy the CFT stack.
DynamoDB GSI Using CloudFormation
DynamoDB has 2 types of secondary indexes: Global secondary index and Local secondary index. These indexes allow developers to perform queries on non-key attributes.
The "IndexName" is the name of the global secondary index of the table. It is crucial to make this index name unique from all the tables' other indexes. "KeySchema" contains all the attribute names and key types of the complete key schema of the GSI. Key type "HASH" represents the partition key, where "RANGE" indicates the sort key.
DynamoDB Autoscaling Using CloudFormation
DynamoDB auto scaling allows changing the provisioned capacity for the tables and GSIs dynamically. The below example shows how to configure autoscaling using an AWS CloudFormation template.
The first step is to set up the scalable target to set boundaries for writes on the table.
Then we have to create the scaling policy.
DynamoDB Local Secondary Index Using CloudFormation
There are instances where we need to query data using an alternative sort key instead of the primary key. In such instances, we can create local secondary indexes on a DynamoDB table and execute queries over these indexes.
Just like with the GSI, the "IndexName" carries the unique name of the local secondary index with key types, "HASH" and "RANGE." Finally, the "Projection" indicates the attributes copied to the local secondary index from the table. The primary key attributes and index key attributes get projected by default, so this represents any additional attributes to project to the local secondary index.
DynamoDB Stream Using CloudFormation
DynamoDB Stream captures and logs the information on item-level modifications made to a DynamoDB table. The below example of a CloudFormation template shows how to set up DynaamoDB Streams by updating an existing table.
DynamoDB Backup Using CloudFormation
DynamoDB PITR (Point in Time Recovery) allows you to restore a DynamoDB table up to any point in the last 35 days. The below code shows how to enable PITR using a CloudFormation template.
Refer to this documentation for more details on AWS backup management with CloudFormation.
DynamoDB On-Demand Using CloudFormation
DynamoDB on-demand is one of the two capacity allocations available in DynamoDB. The below example shows how to create a DynamoDB table with on-demand capacity mode using a CloudFormation template.
PAY_PER_REQUEST statement defines the on-demand capacity method.
DynamoDB TTL Using CloudFormation
DynamoDB TTL (Time to Live) allows to automatically expire items from a DynamoDB table. You can enable TTL in a DynamoDB table using the below CloudFront template configuration.
DynamoDB Alarms Using CloudFormation
We can monitor our DynamoDB databases by using different metrics. These metrics enable the DynamoDB user to understand the workload evolvement with the database. So, we can create CloudFront alarms to get alerts when the metrics fall out of levels. We can automate this by using CloudFormation.
Alternatives To Using CloudFormation for DynamoDB
In this article, I discussed how to use AWS CloudFormation templates with DynamoDB. The experience of working with CloudFormation will surely help you when dealing with multiple cloud resources. You can easily provision and manage them in multiple environments and drastically reduce the effort spent on infrastructure.
I hope you found this article insightful. Thank you for reading.