How To Use Terraform to Provision and Configure Distributed YugabyteDB Clusters

Terraform is a popular Infrastructure as Code tool that simplifies the process of creating, managing, and updating infrastructure components. In this blog post, I’ll explore how to use Terraform to effectively provision and configure distributed YugabyteDB Managed clusters.

I will guide you through the process of configuring the YugabyteDB Managed Terraform provider, defining variables, initializing the Terraform project, and adjusting configurations as needed. Let's dive in!

Steps to Configure YugabyteDB Clusters With Terraform

Before we begin, ensure you have:

  1. Access to a YugabyteDB Managed account
  2. Terraform CLI installed on your local machine

Step 1: Configure YugabyteDB Managed Terraform Provider and Authentication Token

To use YugabyteDB Managed Clusters in Terraform, we must first configure the YugabyteDB Managed Terraform provider using an authentication token.

You can create an authentication token using the YugabyteDB Managed Access Control Panel. Follow the steps in the API keys documentation to generate a token.

Create API key

Once you have your token, create a file called main.tf with the following configuration:

YAML
 
terraform {
  required_providers {
    ybm = {
      source  = "yugabyte/ybm"
      version = "1.0.2"
    }
  }
}

variable "auth_token" {
  type        = string
  description = "API authentication token"
  sensitive   = true
}

provider "ybm" {
  host            = "cloud.yugabyte.com"
  use_secure_host = true
  auth_token      = var.auth_token
}


Then, create a file named terraform.tfvars to store your authentication token securely:

YAML
 
auth_token = "your-authentication-token"


Finally, initialize your Terraform project by running the following command:

Shell
 
terraform init


You should see the message below if everything is initialized correctly:

Shell
 
Initializing the backend...

Initializing provider plugins...
- Finding yugabyte/ybm versions matching "1.0.2"...
- Installing yugabyte/ybm v1.0.2...
- Installed yugabyte/ybm v1.0.2 (self-signed, key ID 0409E86E13F86B59)

Terraform has been successfully initialized!


Step 2: Provision a YugabyteDB Managed Cluster

Now that we have configured the YugabyteDB Managed Terraform provider, let's create a three-node cluster in the US-East region of the Google Cloud Platform.

Add the following configuration snippet to your main.tf file, replacing the cluster credentials with the ones you’d like to use:

YAML
 
resource "ybm_cluster" "single_region_cluster" {
  cluster_name = "my-terraform-cluster"
  cloud_type   = "GCP"
  cluster_type = "SYNCHRONOUS"
  cluster_region_info = [
    {
      region    = "us-east1"
      num_nodes = 3
    }
  ]
  cluster_tier    = "PAID"
  fault_tolerance = "ZONE"
  node_config = {
    num_cores    = 2
    disk_size_gb = 50
  }
  credentials = {
    username = "myUsername"
    password = "mySuperStrongPassword"
  }
}


After updating the configuration, apply the changes using the following command:

Shell
 
terraform apply


Your multi-zone YugabyteDB Managed cluster should be up and running within a few minutes.

Multi-zone YugabyteDB Managed cluster

Step 3: Scale Your YugabyteDB Managed Cluster

Scaling the cluster (both horizontally and vertically) is a breeze with the YugabyteDB Terraform provider. Let's look at how you can scale the cluster to six nodes and provision more CPUs and disk space for each node instance.

To begin, update your main.tf file by changing the cluster_region_info.num_nodes to 6 nodes, node_config.num_cores to 4 CPUs, and node_config.disk_size_gb to 100GBs:

YAML
 
resource "ybm_cluster" "single_region_cluster" {
  # ...
  cluster_region_info = [
    {
      # ...
      num_nodes = 6
    }
  ]
  # ...
  node_config = {
    num_cores    = 4
    disk_size_gb = 100
  }
  # ...
}


After updating the configuration, apply the changes again:

Shell
 
terraform apply


Your six-node cluster with more CPUs and storage per instance will be upgraded and ready in just a few minutes. Note: the infrastructure upgrade happens as a rolling upgrade, without impacting your applications.

Six-node cluster with more CPUs and storage per instance

In Conclusion

This short guide shows how easy it is to use Terraform to provision and manage distributed YugabyteDB Clusters. With just a few configuration changes, we can configure the YugabyteDB Managed Terraform provider, set up a three-node cluster in the US-East region of the Google Cloud Platform, and scale the cluster to six nodes while adjusting for the increased load. 

To find out more about YugabyteDB Managed Terraform provider, you can check the technical documentation.

 

 

 

 

Top