Cloudflare 中文文档
Turnstile
编辑这个页面
跳转官方原文档
Set theme to dark (⇧+D)

Manage Turnstile with Terraform

Terraform is a tool for building, changing, and versioning infrastructure, and provides components and documentation for building Cloudflare resources. Listed below are examples to help you get started with Turnstile using Terraform. For a more generalized guide on configuring Cloudflare and Terraform, visit our Getting Started with Terraform and Cloudflare blog post.

​​ Create a Turnstile widget with Terraform

​​ Create an API token

Create an API Token with the Account > Turnstile > Edit permission. Next, you need to export this secret in our environment variables:

​​ Create a Turnstile widget

See the example configuration below when creating a Turnstile widget.

​​ Initialize Terraform and the Cloudflare provider

Run the command terraform init to set up your Terraform working directory, enabling it to interact with Cloudflare services. This process involves downloading the required provider plugins, establishing backend storage for your state files, and creating a local .terraform directory to store configuration data.

​​ Review the Terraform plan

You can run terraform plan, which will output any proposed changes. This will prompt you for your Cloudflare Account ID. Make sure to review the plan carefully.

​​ Apply the Terraform changes

Once the changes look accurate and you are comfortable moving forward, apply them using the terraform apply command.

You have successfully created a Turnstile widget. Go to the Cloudflare dashboard to view its configuration and analytics in a user-friendly interface.

​​ Retrieve the secret key

Use terraform output to get your secret key.

​​ Import a Turnstile widget

​​ Prerequisites

Before you can import the Turnstile widget in Terraform, you must:

​​ Import Turnstile widgets in Terraform

Run the cf-terraforming generate command and add the output below to your .tf file.

Run the cf-terraforming import command and the resulting commands below.