@@ -23,7 +23,7 @@ Before you begin, you must have:
2323
2424Create a ` main.tf ` file.
2525
26- ``` tf title="cloudflare.tf"
26+ ``` tf
2727terraform {
2828 required_providers {
2929 cloudflare = {
@@ -51,3 +51,140 @@ variable "account_id" {
5151
5252### 2. Define widgets
5353
54+ ``` tf
55+ resource "cloudflare_turnstile_widget" "login_form" {
56+ account_id = var.account_id
57+ name = "Login Form Widget"
58+ domains = ["example.com", "www.example.com"]
59+ mode = "managed"
60+ region = "world"
61+ }
62+
63+ resource "cloudflare_turnstile_widget" "api_protection" {
64+ account_id = var.account_id
65+ name = "API Protection"
66+ domains = ["api.example.com"]
67+ mode = "invisible"
68+ region = "world"
69+ }
70+
71+ # Output the sitekeys for use in your application
72+ output "login_sitekey" {
73+ value = cloudflare_turnstile_widget.login_form.sitekey
74+ }
75+
76+ output "api_sitekey" {
77+ value = cloudflare_turnstile_widget.api_protection.sitekey
78+ }
79+ ```
80+
81+ ### 3. Environment variables
82+
83+ Create a ` .env ` file or set environment variables.
84+
85+ ``` shell
86+ export TF_VAR_cloudflare_api_token=" your-api-token"
87+ export TF_VAR_account_id=" your-account-id"
88+ ```
89+
90+ ---
91+
92+ ## Terraform commands
93+
94+ ### Initialize and plan
95+
96+ ``` shell title="Initialize Terraform"
97+ terraform init
98+ ```
99+
100+ ``` shell title="Plan changes"
101+ terraform plan
102+ ```
103+
104+ ``` shell title="Apply configuration"
105+ terraform apply
106+ ```
107+
108+ ### Manage changes
109+
110+ ``` shell title="Update widget configuration"
111+ terraform plan
112+ ```
113+
114+ ``` shell title="Apply changes"
115+ terraform apply
116+ ```
117+
118+ ``` shell title="Destroy widgets"
119+ terraform destroy
120+ ```
121+
122+ ---
123+
124+ ## Advanced Terraform configuration
125+
126+ ### Multiple environments
127+
128+ ``` tf
129+ locals {
130+ environments = {
131+ dev = {
132+ domains = ["dev.example.com"]
133+ mode = "managed"
134+ }
135+ staging = {
136+ domains = ["staging.example.com"]
137+ mode = "non_interactive"
138+ }
139+ prod = {
140+ domains = ["example.com", "www.example.com"]
141+ mode = "invisible"
142+ }
143+ }
144+ }
145+
146+ resource "cloudflare_turnstile_widget" "app_widget" {
147+ for_each = local.environments
148+
149+ account_id = var.account_id
150+ name = "App Widget - ${each.key}"
151+ domains = each.value.domains
152+ mode = each.value.mode
153+ region = "world"
154+ }
155+ ```
156+
157+ ### Widget with Enterprise features
158+
159+ ``` tf
160+ resource "cloudflare_turnstile_widget" "enterprise_widget" {
161+ account_id = var.account_id
162+ name = "Enterprise Form"
163+ domains = ["enterprise.example.com"]
164+ mode = "managed"
165+ region = "world"
166+ offlabel = true # Remove Cloudflare branding
167+ bot_fight_mode = true # Enable bot fight mode
168+ }
169+ ```
170+
171+ ---
172+
173+ ## Import existing widgets
174+
175+ Use ` cf-terraforming ` to import existing widgets.
176+
177+ ``` shell title="Install cf-terraforming"
178+ go install github.com/cloudflare/cf-terraforming/cmd/cf-terraforming@latest
179+ ```
180+
181+ ``` shell title="Generate Terraform configuration from existing widgets"
182+ cf-terraforming generate \
183+ --resource-type cloudflare_turnstile_widget \
184+ --account $ACCOUNT_ID
185+ ```
186+
187+ ``` shell title="Import existing widget"
188+ terraform import cloudflare_turnstile_widget.existing_widget \
189+ $ACCOUNT_ID /$WIDGET_SITEKEY
190+ ```
0 commit comments