Automating and managing your ConfigCat resources with Terraform

Automating and managing your ConfigCat resources with Terraform

It can be time-consuming to create and manage the infrastructure that drives your software applications as they grow and become larger. Also, what about ongoing updates and releases of new features? Luckily, there is a solution to this problem in the form of a tool designed by Hashicorp called Terraform. This allows us to define our infrastructure in a central configuration file without having to create it on every provider platform we use.

What is Terraform?

Terraform is a popular Infrastructure as Code tool that allows you to automate and manage your infrastructure by defining how it should be in an easy-to-read configuration file.

Upon executing the file, Terraform does the heavy lifting for you (that you would have otherwise had to do manually) by connecting to your cloud providers of choice to provision your infrastructure on the fly. By also keeping track of the state of your infrastructure changes along the way, Terraform can figure out what parts of your infrastructure have remained the same from what has changed and only executes those changes.

Using ConfigCat with Terraform

In addition to being the go-to choice for many companies managing their infrastructure as code, Terraform can also be ideal for managing and provisioning your ConfigCat resources programmatically. Using the ConfigCat Feature Flags Provider for Terraform, you can gain the same level of control and management over these resources similarly as you do with your other infrastructure components.

Here are some reasons why you might want to consider using ConfigCat and Terraform together for your next infrastructure build:

  • Supports a multienvironment setup - If your company already has multiple environments like test, staging, and production for other elements of their infrastructure you can define a similar setup using the ConfigCat provider in your Terraform config file.

  • Best for managing multiple products - Even though the ConfigCat dashboard is easy and intuitive to navigate, you can often lose track of all your products and their respective configurations. It would be better to define them with Terraform. With this approach, you can prevent overlapping and duplications to ensure that your resources are used efficiently with less clutter.

  • Collaboration, and centralized infrastructure management - Terraform scripts usually live in a git repository, making it ideal for managing your ConfigCat resources in a central place. With the added benefit of git operations, you can perform actions such as branching, pull requests and approve strategy.
    For example - Suppose you were tasked with creating a new environment in ConfigCat. You can either do it manually without supervision or you can open a pull request with your modified Terraform scripts and that pull request can be approved. This way you have strict control over who can create resources and or what resources are created.

Live Demo

Let's examine an example of how you can use Terraform to set up a feature flag resource from scratch.

To get started, check that you have Terraform installed and configured on your computer. It is also quite handy to install the Terraform VS Code extension for Terraform if you’re using the VS Code Editor to follow along.

The sample ConfigCat infrastructure that we're about to set up will include the following:

  • An Organization - Because organizations can contain specific information about a company or individual's billing makes them read-only from Terraform. It is recommended that you first create an empty organization in your ConfigCat dashboard.

  • A Product - Represents your application, and includes your Configs, Environments, and Team members.

  • An Environment - Represents the environment in your development lifecycle like production, staging, development, etc.

  • A Feature flag - A feature toggle (boolean value) that you can turn on or off to enable or disable a feature.

Finding your credentials

To grant Terraform access to your ConfigCat account you’ll need to create your ConfigCat public API credentials here.

Integrating with Terraform

  1. With a blank folder opened in your code editor, create two files: variables.tf and main.tf.

  2. In the variables.tf file, add the credentials you created:

variable "configcat_basic_auth_username" {
  type = string
  default = "INPUT_YOUR_AUTH_USERNAME_HERE"
}

variable "configcat_basic_auth_password" {
  type = string
  default = "INPUT_YOUR_AUTH_PASSWORD_HERE"
}
  1. In the main.ts file:

a. Define the ConfigCat provider along with your credentials:

terraform {
  required_providers {
    configcat = {
      source = "configcat/configcat",
      version = "~> 1.0"
    }
  }
}

provider "configcat" {
  basic_auth_username = var.configcat_basic_auth_username
  basic_auth_password = var.configcat_basic_auth_password
}

b. Define the following resources to be created:

data "configcat_organizations" "my_organizations" {

}
resource "configcat_product" "my_product" {
  organization_id = data.configcat_organizations.my_organizations.organizations.0.organization_id
  name = "My product"
  description = "My example product"
}

resource "configcat_environment" "my_environment" {
  product_id = configcat_product.my_product.id
  name = "Production"
  description = "The production environment"
  color = "blue"
}

resource "configcat_config" "my_config" {
  product_id = configcat_product.my_product.id
  name = "My config"
  description = "My example config"
}

resource "configcat_setting" "is_awesome" {
  config_id = configcat_config.my_config.id
  key = "isMyAwesomeFeatureEnabled"
  name = "My awesome feature flag"
  hint = "this is the hint for my awesome feature flag"
  setting_type = "boolean"
}

Execute this with Terraform using the plan and apply commands respectively, and within a few seconds, you should see the specified resources created in your Dashboard.

ConfigCat resources created by terraform

Final thoughts

Terraform takes care of what has to be created, updated, and destroyed based on changes you make to your Terraform config file. Using it with ConfigCat has its proven benefits as we discussed earlier. In my opinion, it supercharges your development, feature releases, and canary deployment workflows while giving you high-level and automated management over your current infrastructure within a simple .tf config file.

Why not give it a go? Sign up for a free-tier ConfigCat account here and stay on top of the latest posts and announcements from ConfigCat on Twitter, Facebook, LinkedIn, and GitHub.

Did you find this article valuable?

Support Chavez Harris by becoming a sponsor. Any amount is appreciated!