October 20, 2023

Harness Terraform Provider: Managing Feature Flags with Terraform

Table of Contents

Harness integrates with Terraform to manage feature flags as code, enabling streamlined deployment pipelines and automated feature management. This integration enhances collaboration, ensures consistent infrastructure management, and offers granular control over feature rollouts.

Understanding Feature Flags

Before we dive into the details of the harness_platform_feature_flag resource, let's briefly understand what Feature Flags are and why they are essential for modern software delivery.

Feature Flags are a technique that allows you to toggle the visibility of certain features or code sections in your application without redeploying it. This approach provides several benefits, including controlled rollouts, A/B testing, and quickly responding to issues by disabling specific features.

Harness Continuous Delivery and Terraform Integration

Harness seamlessly integrates with Terraform, enabling you to define and manage Feature Flags as code. This integration streamlines your deployment pipelines, enhances collaboration between development and operations teams, and ensures consistent, automated management of your feature flags.

Using the Harness Platform Feature Flag Resource

The harness_platform_feature_flag resource empowers you to define and manage Feature Flags within your Harness environment. Let's explore some usage examples:

Boolean Feature Flag

resource "harness_platform_feature_flag" "mybooleanflag" {
    org_id               = "your-org-id"  
    project_id           = "your-project-id"  
    kind                 = "boolean"  
    name                 = "MY_FEATURE"  
    identifier           = "MY_FEATURE"  
    permanent            = false  
    default_on_variation = "Enabled"  
    default_off_variation = "Disabled"
    
    variation {
        identifier  = "Enabled"    
        name        = "Enabled"    
        description = "The feature is enabled"    
        value       = "true"
        }
        
    variation {
        identifier  = "Disabled"    
        name        = "Disabled"    
        description = "The feature is disabled"    
        value       = "false"  
        }
     }

In this example, we create a boolean Feature Flag named "MY_FEATURE" with two variations: "Enabled" and "Disabled."

Multivariate Feature Flag

resource "harness_platform_feature_flag" "mymultivariateflag" {
    org_id               = "your-org-id"
    project_id           = "your-project-id"
    kind                 = "int"
    name                 = "FREE_TRIAL_DURATION"
    identifier           = "FREE_TRIAL_DURATION"
    permanent            = false
    default_on_variation = "trial7"
    default_off_variation = "trial20"
    
    variation {
    	identifier  = "trial7"    
    	name        = "7 days trial"    
    	description = "Free trial period 7 days"    
    	value       = "7"
        }
    
    variation {    
    	identifier  = "trial14"    
        name        = "14 days trial"    
        description = "Free trial period 14 days"    
        value       = "14"
        }
    
    variation {    
		identifier  = "trial20"    
   		name        = "20 days trial"    
    	description = "Free trial period 20 days"    
    	value       = "20"  
    }
}

In this example, we create a multivariate Feature Flag named "FREE_TRIAL_DURATION" with three variations: "7 days trial," "14 days trial," and "20 days trial."

Assign Targets and Target Groups

You can also assign targets or target groups to your Feature Flags:

resource "harness_platform_feature_flag" "mytargetedflag" {
    org_id               = "your-org-id"  
    project_id           = "your-project-id"  
    kind                 = "boolean"  
    name                 = "MY_TARGETED_FEATURE"  
    identifier           = "MY_TARGETED_FEATURE"  
    environment          = "MY_ENVIRONMENT"  
    permanent            = false  
    default_on_variation = "Enabled"  
    default_off_variation = "Disabled"
    
    variation {
        identifier  = "Enabled"    
    	name        = "Enabled"    
    	description = "The feature is enabled"    
    	value       = "true"
        }
    
    variation {
        identifier  = "Disabled"    
        name        = "Disabled"    
        description = "The feature is disabled"    
        value       = "false"
        }
    
	add_target_rule {    
	    variation = "Enabled"    
        targets   = ["target1", "target2"]
        }
}

In this example, we create a Feature Flag "MY_TARGETED_FEATURE" and assign it to specific targets in the "MY_ENVIRONMENT."

Advanced Configuration

Harness and Terraform provide extensive options for configuring Feature Flags:

  • Permanent Flags: You can mark a Feature Flag as permanent, ensuring it is never flagged as stale.
  • Git Integration: Harness supports Git integration for managing your Feature Flags.

Understanding Feature Flag Target Groups

Before we dive into the details of the harness_platform_feature_flag_target_group resource, let's clarify what Feature Flag Target Groups are and why they are essential.

Feature Flag Target Groups are collections of target destinations that determine where specific feature flags should be enabled or disabled. These groups allow you to define granular rules for targeting environments, users, or accounts, making managing feature flag rollouts and experimentation easier.

Using the Harness Platform Feature Flag Target Group Resource

The harness_platform_feature_flag_target_group resource enables you to define and manage Feature Flag Target Groups as code. Here's a basic example of how to use it:

resource "harness_platform_feature_flag_target_group" "example_target_group" {
    org_id       = "your-org-id"  
    project_id   = "your-project-id"  
    identifier   = "MY_FEATURE_GROUP"  
    environment  = "MY_ENVIRONMENT"  
    name         = "MY_FEATURE_TARGET_GROUP"  
    account_id   = "MY_ACCOUNT_ID"  
    included     = ["target_id_1"]  
    excluded     = ["target_id_2"]    
    
rule {
    attribute = "MY_ATTRIBUTE"    
    operator  = "equal"    
    value     = "MY_VALUE"  
    }
}

In this example, we create a Feature Flag Target Group named "MY_FEATURE_TARGET_GROUP" associated with the feature identified as "MY_FEATURE_GROUP." We specify the organization, project, environment, account ID, included and excluded targets, and a rule for targeting specific destinations based on attributes.

Advanced Configuration

Harness and Terraform provide extensive options for configuring Feature Flag Target Groups:

  • Rule-Based Targeting: Use rules to include or exclude targets based on user roles, account IDs, or other custom attributes.
  • Inclusion and Exclusion: Define lists of targets to include or exclude within the target group to achieve fine-grained control over feature flag rollouts.

Benefits of Managing Feature Flag Target Groups with Terraform

  • Infrastructure as Code: Manage Feature Flag Target Groups alongside your infrastructure, ensuring version control and consistency across environments.
  • Granular Control: Create precise rules for targeting environments, users, or accounts, enhancing the accuracy of your feature flag management.
  • Traceability: Easily track changes to Feature Flag Target Groups, audit configurations, and make rollbacks if necessary.
  • Automation: Leverage Terraform's automation capabilities to create, modify, or delete Feature Flag Target Groups as needed.

Using the Harness Platform Feature Flag Target Resource

The harness_platform_feature_flag_target resource enables you to define and manage Feature Flag Targets as code. Here's a basic example of how to use it:

resource "harness_platform_feature_flag_target" "example_target" {
    org_id       = "your-org-id"  
    project_id   = "your-project-id"  
    identifier   = "MY_FEATURE"  
    environment  = "MY_ENVIRONMENT"  
    name         = "MY_FEATURE_TARGET"  
    account_id   = "MY_ACCOUNT_ID"  
    attributes   = { "foo": "bar" 
    }
}

This example defines a Feature Flag Target named "MY_FEATURE_TARGET" associated with the feature identified as "MY_FEATURE." We specify the organization, project, environment, account ID, and any additional attributes you want to associate with this target.

Advanced Configuration

Harness and Terraform offer extensive options for configuring Feature Flag Targets:

  • Granular Control: Specify the precise environment or account where the feature should be enabled or disabled.
  • Attributes: Use attributes to add custom metadata or information to your targets.

Benefits of Managing Feature Flag Targets with Terraform

  • Infrastructure as Code: Manage Feature Flag Targets alongside your infrastructure, ensuring version control and consistency across environments.
  • Collaboration: Facilitate collaboration between development, operations, and QA teams using a single codebase for infrastructure and feature flag management.
  • Traceability: Easily track changes to Feature Flag Targets, audit configurations, and make rollbacks if necessary.
  • Automation: Leverage Terraform's automation capabilities to create, modify, or delete Feature Flag Targets as needed.

Conclusion

Feature Flags are a fundamental part of modern software development practices, allowing you to effectively control feature rollouts and experimentation. Using the Harness Terraform Provider and the harness_platform_feature_flag resource, you can define, manage, and automate your Feature Flags, ensuring controlled and efficient feature management.

Start using Harness and Terraform to manage your Feature Flags as code today, and empower your development and operations teams with precise control over feature releases and experiments.

Sign up for a free trial of Harness Feature Flags today and start experiencing the benefits of reliable release management.

Feature Management & Experimentation