On this page

Managing Experiments With Terraform

Define Statsig experiments as Terraform resources, including variants, allocation, targeting, and scorecard metrics, for fully reproducible setups.

You can create a .tf file (Terraform File) to configure your Statsig experiments. All features of console/v1/experiments are supported. The layout is very similar to the JSON body of a /experiments request.

Require the Statsig provider and update the version to match your installed release:

go
terraform {
  required_providers {
    statsig = {
      version = "x.x.x"
      source  = "statsig-io/statsig"
    }
  }
}

Basic example

The following creates a basic experiment resource:

go
resource "statsig_experiment" "my_experiment" {
  name        = "my_experiment"
  description = "A short description of what we are experimenting on."
  id_type     = "userID"
  allocation  = 10
  status      = "setup"
  groups {
    name                  = "Test Group"
    size                  = 50
    parameter_values_json = jsonencode({ "a_string" : "test_string", "a_bool" : true })
  }
  groups {
    name                  = "Control Group"
    size                  = 50
    parameter_values_json = jsonencode({ "a_string" : "control_string", "a_bool" : false })
  }
}

Changing experiment status

You can update the status field to four possible values: setup, active, decision_made, and abandoned.

For code examples of how the Setup -> Run -> Ship flow works, refer to the Terraform Acceptance Tests for experiments.

Status: setup

The experiment isn't ready. The Statsig SDK and HttpAPI don't serve any values.

Status: active

The experiment is running. Statsig returns values to users and collects analytics data.

Status: decision_made

The experiment is complete and you've selected a group to ship. This state requires you to set the launched_group_id field to the GroupID found on console.statsig.com.

Status: abandoned

The experiment doesn't serve any values and doesn't collect analytics data.

  • You can only create an experiment with the status "setup" or "active".

  • You can only transition to "decision_made" from "active".

You can find a full experiment example in the open source Github repo https://github.com/statsig-io/terraform-provider-statsig/blob/main/examples/resources/statsig_experiment/resource.tf.

Was this helpful?