When working with Document databases for a product, I was looking for a scalable and managed solution and I went ahead with AWS DocumentDB. AWS DocumentDB has emerged as a strong choice to manage with easy to scale and compatibility with the MongoDB engine. This provides an easier option for users wanting to migrate their infrastructure to AWS, using MongoDB as a database before migration. In this article, I will explain how I developed a custom Terraform provider in Go to manage DocumentDB Index after migration from MongoDB. This article won’t cover information about migration steps. Those can be part of a separate article.
Why I developed Terraform provider to automate database operations?
In my use case, I need to use Terraform to deploy and manage DocumentDB clusters, being a standard option within my project for automation. I managed to deploy the cluster through aws official terraform provider. To elaborate on custom providers, they work as plugins for Terraform to manage different services. They are developed in GO.
After cluster deployment, I found I need to automate DB operations for which i can use scripts. I found, using provider maybe a better approach to keep consistency in the project as most of the infrastructure in project managed by Terraform. Terraform provider framework is well matured but there were all outdated providers for DocumentDB and I did not find any provider supporting all three operations I was looking for: user, roles and Index. So, I decided to contribute to Index automation to a provider which was already supporting user and roles.
In this guide, you will learn how you can also use the same provider to manage your users, roles and Indexes. Before we move further, let me start from fundamentals and steps to use the provider.
What is Amazon DocumentDB?
Amazon DocumentDB is a fully managed document database, compatible with MongoDB workloads. It was released in 2019 and provides high availability, scalability using replica instances. It supports one primary instance and up to 15 replica instances. Unlike DynamoDB which is serverless, we need to provision and manage compute instances for DocumentDB.
Even though AWS handles patching, backup and infrastructure management.
DocumentDB provides instance based and elastic based cluster options. More details on DocumentDB available in the AWS documentation
AWS documentDB can have multiple databases and each database will have its collections. Each collection will have their Index.
How are DocumentDB organized in the AWS?
In AWS, clusters are in the top level of hierarchy that define DocumentDB deployment. We can deploy multiple instances in the cluster. Each cluster can have primary (writer) and multiple replica instances (readers). When the request for read/write operations come to the DocumentDB, they come to the instance endpoint which belongs to the instance with DB and collection.
Replica instances can be available in different regions to provide high availability. Also, instances. All instances belong to single replica set named rs0
We will use inserted data using db.users.insertMany() for our example, later in the article.
What is a collection?
A Database provides logical grouping of collections. It can be compared with tables in the relational database. Data stored in collections are called documents.
Documents can have different structure unlike relational database
Document stored in BSON format (binary json) although it looks like JSON
Now, you can understand for any application with multiple components and databases involved, having multiple Indexes to each collection can have dozens of Indexes in total. Managing them through CLI can be difficult. What I learned from my experience, it is difficult to track which collections have all Index configured, and which if there is no typo in them. It won’t create errors in write/read operations but will impact performance by slowing down the query actions. This can result in incidents and finding Index by logging into each database instance is not how enterprise resources are managed. In this case, an organized automation approach is really needed.
As we discussed, a manual approach is not helpful for several reasons.
Problem with manual approach:
Manual cli based automation involve multiple scripts
No version control mechanism
Environment inconsistencies (dev vs prod index may differ)
No easy rollback strategy
Outdated documentation
Using the terraform provider abstraction layer as the automation part is already written in the provider. This reduces the effort to write operational automation. In our case, we just need to call the provider and mention the resources we want to manage with Terraform.
Terraform uses a state file with if used with a remote state like S3 with version control, it can be easy to rollback. Terraform also provides idempotency and keeps track of the current state of infrastructure, which is database configuration like user, index in our case.
The aim is to provide an overview of the DocumentDB, benefits of using Terraform for automation and then explain how you can use this custom provider to automate Index, users and roles.
Let’s understand how to use the provider now.
Before we start, we have a few prerequisites
Document DB cluster deployed
We can reach cluster endpoint from where we want to run Terraform
Terraform is installed
Note: DocumentDB runs on a private IP cluster so in my test I installed an EC2 instance in the same vPC to reach DocumentDB cluster.
We can connect to the cluster instance using mongosh utility. When you select the cluster instances it will show you required arguments for the connection.
This concludes introduction to custom Terraform provider to manage DocumentDB operations.
We can further manage it through CI/CD pipeline and create subdirectories of different environment. Another better approach is to create Terraform module. I will explain it in next article.
Loading article