|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + posthog = { |
| 4 | + source = "posthog/posthog" |
| 5 | + } |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +provider "posthog" { |
| 10 | + # Configuration can be provided via: |
| 11 | + # - Environment variables: POSTHOG_API_KEY, POSTHOG_PROJECT_ID, POSTHOG_HOST |
| 12 | + # - Or explicitly in the provider block: |
| 13 | + # api_key = "your-api-key" |
| 14 | + # project_id = "12345" |
| 15 | + # host = "https://us.posthog.com" # Optional, defaults to US cloud |
| 16 | +} |
| 17 | + |
| 18 | +# Example 1: Simple Boolean Feature Flag |
| 19 | +# A basic on/off toggle that's enabled for all users |
| 20 | +resource "posthog_feature_flag" "simple_boolean" { |
| 21 | + key = "simple-boolean-flag" |
| 22 | + name = "Simple Boolean Flag" |
| 23 | + active = true |
| 24 | + |
| 25 | + filters = jsonencode({ |
| 26 | + groups = [{ |
| 27 | + properties = [] |
| 28 | + rollout_percentage = 100 |
| 29 | + }] |
| 30 | + }) |
| 31 | + |
| 32 | + tags = ["managed-by:terraform"] |
| 33 | +} |
| 34 | + |
| 35 | +# Example 2: Gradual Rollout with Percentage |
| 36 | +# Roll out a feature to 25% of users using the convenience field |
| 37 | +resource "posthog_feature_flag" "gradual_rollout" { |
| 38 | + key = "gradual-rollout-25-percent" |
| 39 | + name = "Gradual Rollout 25%" |
| 40 | + active = true |
| 41 | + rollout_percentage = 25 |
| 42 | + tags = ["managed-by:terraform"] |
| 43 | +} |
| 44 | + |
| 45 | +# Example 3: Multivariate Feature Flag (A/B/C Test) |
| 46 | +# Test multiple variants with different rollout percentages |
| 47 | +resource "posthog_feature_flag" "multivariate_test" { |
| 48 | + key = "multivariate-abc-test" |
| 49 | + name = "Multivariate A/B/C Test" |
| 50 | + active = true |
| 51 | + |
| 52 | + filters = jsonencode({ |
| 53 | + groups = [{ |
| 54 | + properties = [] |
| 55 | + rollout_percentage = 100 |
| 56 | + }] |
| 57 | + multivariate = { |
| 58 | + variants = [ |
| 59 | + { |
| 60 | + key = "control" |
| 61 | + name = "Control" |
| 62 | + rollout_percentage = 34 |
| 63 | + }, |
| 64 | + { |
| 65 | + key = "variant-a" |
| 66 | + name = "Variant A" |
| 67 | + rollout_percentage = 33 |
| 68 | + }, |
| 69 | + { |
| 70 | + key = "variant-b" |
| 71 | + name = "Variant B" |
| 72 | + rollout_percentage = 33 |
| 73 | + } |
| 74 | + ] |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + tags = ["managed-by:terraform"] |
| 79 | +} |
| 80 | + |
| 81 | +# Example 4: Feature Flag with User Property Targeting |
| 82 | +# Enable for users with specific properties (email domain and plan type) |
| 83 | +resource "posthog_feature_flag" "user_property_targeting" { |
| 84 | + key = "user-property-targeting" |
| 85 | + name = "User Property Targeting" |
| 86 | + active = true |
| 87 | + |
| 88 | + filters = jsonencode({ |
| 89 | + groups = [ |
| 90 | + { |
| 91 | + # Group 1: Target enterprise customers |
| 92 | + properties = [ |
| 93 | + { |
| 94 | + key = "plan" |
| 95 | + type = "person" |
| 96 | + value = ["enterprise", "business"] |
| 97 | + operator = "exact" |
| 98 | + } |
| 99 | + ] |
| 100 | + rollout_percentage = 100 |
| 101 | + }, |
| 102 | + { |
| 103 | + # Group 2: Gradual rollout to all other users |
| 104 | + properties = [] |
| 105 | + rollout_percentage = 10 |
| 106 | + } |
| 107 | + ] |
| 108 | + }) |
| 109 | + |
| 110 | + tags = ["managed-by:terraform"] |
| 111 | +} |
| 112 | + |
| 113 | +# Example 5: Feature Flag Dependency |
| 114 | +# Enable a feature only for users who have another feature flag enabled |
| 115 | +resource "posthog_feature_flag" "flag_dependency" { |
| 116 | + key = "flag-dependency-example" |
| 117 | + name = "Flag Dependency Example" |
| 118 | + active = true |
| 119 | + |
| 120 | + filters = jsonencode({ |
| 121 | + groups = [{ |
| 122 | + properties = [{ |
| 123 | + key = "$feature/${posthog_feature_flag.simple_boolean.key}" |
| 124 | + type = "person" |
| 125 | + value = ["true"] |
| 126 | + operator = "exact" |
| 127 | + }] |
| 128 | + rollout_percentage = 100 |
| 129 | + }] |
| 130 | + }) |
| 131 | + |
| 132 | + tags = ["managed-by:terraform"] |
| 133 | +} |
| 134 | + |
| 135 | +# Example 6: Remote Configuration Flag |
| 136 | +# Feature flag with a JSON payload for dynamic configuration |
| 137 | +resource "posthog_feature_flag" "remote_config" { |
| 138 | + key = "remote-config-payload" |
| 139 | + name = "Remote Config with Payload" |
| 140 | + active = true |
| 141 | + |
| 142 | + filters = jsonencode({ |
| 143 | + groups = [{ |
| 144 | + properties = [] |
| 145 | + rollout_percentage = 100 |
| 146 | + }] |
| 147 | + payloads = { |
| 148 | + "true" = jsonencode({ |
| 149 | + api_endpoint = "https://api.example.com/v2" |
| 150 | + timeout_ms = 5000 |
| 151 | + max_retries = 3 |
| 152 | + features = { |
| 153 | + caching_enabled = true |
| 154 | + rate_limit = 1000 |
| 155 | + } |
| 156 | + }) |
| 157 | + } |
| 158 | + }) |
| 159 | + |
| 160 | + tags = ["managed-by:terraform"] |
| 161 | +} |
| 162 | + |
| 163 | +# Outputs to reference the created feature flags |
| 164 | +output "simple_boolean_flag_id" { |
| 165 | + description = "ID of the simple boolean feature flag" |
| 166 | + value = posthog_feature_flag.simple_boolean.id |
| 167 | +} |
| 168 | + |
| 169 | +output "multivariate_test_flag_id" { |
| 170 | + description = "ID of the multivariate test feature flag" |
| 171 | + value = posthog_feature_flag.multivariate_test.id |
| 172 | +} |
0 commit comments