Skip to content
AnswerWell geo Documentation

Research · 7 min read · Answerwell

Terraform Initialization, DNS Records, Zone Settings and State

How to use Terraform with the Cloudflare provider: install, authenticate, create DNS and zone settings, import resources, and run init, plan, and apply safely.

This page summarizes Cloudflare’s documentation on managing zones and related resources with Terraform. Terraform is infrastructure-as-code software: you declare the desired end state in configuration files, then use a standard workflow to create, change, or remove resources through the Cloudflare API.

Prerequisites: install Terraform and create an API token

Section titled “Prerequisites: install Terraform and create an API token”

Before you write configuration, install the Terraform CLI and create a Cloudflare API token with permission to edit the resources you intend to manage.

On macOS, Cloudflare documents Homebrew:

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

On Linux, an example path is the distribution package manager:

sudo apt install terraform

See Get started — installing Terraform for platform-specific steps.

The Initialize Terraform tutorial assumes you have already added your domain to Cloudflare and created an API token with permissions to edit resources for that tutorial. You also need your zone ID, account ID, and domain name when defining resources.

Cloudflare provider: how to declare required_providers

Section titled “Cloudflare provider: how to declare required_providers”

A minimal working directory starts with a main.tf (or split files in one folder). Terraform evaluates every .tf file in the directory as one configuration.

For a first DNS record, Cloudflare’s Initialize Terraform tutorial uses the cloudflare/cloudflare provider at v5:

terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 5"
}
}
}
provider "cloudflare" {
api_token = "<YOUR_API_TOKEN>"
}

Terraform code snippets below refer to the v5 SDK only.

— Cloudflare, 1 – Initialize Terraform

You must keep a provider "cloudflare" block even when the token is not hardcoded in the file, so Terraform installs the plugin. The Track your history page notes that an empty provider block is enough when authentication comes from the environment.

cloudflare_dns_record: create a proxied DNS record with Terraform

Section titled “cloudflare_dns_record: create a proxied DNS record with Terraform”

A typical first resource points a hostname at an origin IP. The tutorial creates www as an A record, proxied through Cloudflare:

resource "cloudflare_dns_record" "www" {
zone_id = "<YOUR_ZONE_ID>"
name = "www"
content = "203.0.113.10"
type = "A"
ttl = 1
proxied = true
comment = "Domain verification record"
}

To prevent accidentally exposing your Cloudflare credentials, do not save this file in your version control system. The next tutorial will cover best practices for passing in your API token.

— Cloudflare, 1 – Initialize Terraform

Use variables for zone_id, account_id, and domain instead of literals before committing; the track history tutorial shows that pattern.

terraform init, plan, and apply: standard workflow for Cloudflare

Section titled “terraform init, plan, and apply: standard workflow for Cloudflare”

These commands are the core lifecycle for any Cloudflare Terraform project.

terraform init: download the Cloudflare provider

Section titled “terraform init: download the Cloudflare provider”

Initialization resolves provider constraints, downloads plugins, and creates or updates .terraform.lock.hcl:

terraform init

For larger repos (for example AWS plus Cloudflare WAN configurations), Cloudflare also documents:

terraform fmt -check -recursive
terraform validate
terraform test

Initialization resolves the provider constraints, downloads the AWS and Cloudflare providers, and creates or updates .terraform.lock.hcl. Validation checks the Terraform syntax and provider schemas without creating resources. The tests use mock providers and do not create resources.

— Cloudflare, Amazon AWS Transit Gateway — Initialize and validate Terraform

terraform plan: preview creates and updates

Section titled “terraform plan: preview creates and updates”

terraform plan shows what would change without applying it. After your first configuration, expect a plan that adds the DNS record (symbols like + create in the output).

terraform apply: create or update Cloudflare resources

Section titled “terraform apply: create or update Cloudflare resources”
terraform apply

Approve with yes when prompted. After apply, Cloudflare suggests verifying DNS with dig and HTTPS with curl, inspecting state with terraform show, and checking DNS > Records in the dashboard—see Initialize Terraform — Apply and verify.

To tear everything down:

terraform destroy

Both terraform apply and terraform destroy prompt for confirmation unless you pass -auto-approve (Terraform tunnel guide — Deploy).

API token authentication: environment variables and version control

Section titled “API token authentication: environment variables and version control”

Hardcoding api_token in main.tf is fine for a local tutorial step; for anything you commit, remove the token from files and use environment variables.

The Cloudflare provider v5 reads the token from CLOUDFLARE_API_TOKEN automatically when the provider block omits it:

provider "cloudflare" {
# API token will be read from CLOUDFLARE_API_TOKEN environment variable
}
export CLOUDFLARE_API_TOKEN="your-api-token-here"

Put secrets in terraform.tfvars (or another var file) and exclude that file from git. Mark IDs as sensitive where appropriate:

To prevent accidentally exposing sensitive credentials, do not save terraform.tfvars in your version control system. For example, if your version version control is git, add terraform.tfvars to your .gitignore file.

— Cloudflare, Ansible deployment guide — Assign values to the variables

After switching from hardcoded values to variables, the next terraform plan may show in-place updates (for example name or zone_id formatting). Cloudflare describes that as normal when migrating configuration style (Track your history).

cloudflare_zone_setting: HTTPS and TLS zone settings in Terraform

Section titled “cloudflare_zone_setting: HTTPS and TLS zone settings in Terraform”

Zone-level HTTPS controls can be managed as separate resources. The Configure HTTPS settings tutorial adds three settings:

Resource setting_id Example value Effect (tutorial intent)
cloudflare_zone_setting.tls_1_3 tls_1_3 on Enable TLS 1.3
cloudflare_zone_setting.automatic_https_rewrites automatic_https_rewrites on Enable automatic HTTPS rewrites
cloudflare_zone_setting.ssl ssl strict SSL mode strict

A plan for that step typically shows 3 to add. The tutorial applies after optionally confirming TLS 1.3 was off before the change (using curl with TLS 1.3). Treat that as a before/after check, not a requirement for every zone.

Import existing Cloudflare resources with cf-terraforming

Section titled “Import existing Cloudflare resources with cf-terraforming”

Resources created in the dashboard can be brought under Terraform with import. Cloudflare documents cf-terraforming generating terraform import commands you run manually:

cf-terraforming import --resource-type "cloudflare_record" --email $CLOUDFLARE_EMAIL --key $CLOUDFLARE_API_KEY --zone $CLOUDFLARE_ZONE_ID

Run each suggested terraform import … line. After a successful import, terraform plan should not try to re-create those records; grep output may show No changes. Infrastructure is up-to-date.

Tunnels, Access, and multi-provider stacks (what Terraform can manage)

Section titled “Tunnels, Access, and multi-provider stacks (what Terraform can manage)”

Beyond DNS and zone settings, Cloudflare documents Terraform for Cloudflare Tunnel, Access applications, GCP VMs, and optional Ansible provisioners—see Terraform tunnel deployment guide, Publish applications with Terraform, and Ansible with Terraform.

Terraform is an infrastructure as code software tool that allows you to deploy services from different providers using a standardized configuration syntax. When creating a Terraform configuration file, you define the final state of the configuration rather than the step-by-step procedure.

— Cloudflare, Terraform tunnel deployment guide

Typical Cloudflare-side resources in those guides include cloudflare_zero_trust_tunnel_cloudflared, cloudflare_dns_record (CNAME to *.cfargotunnel.com), cloudflare_zero_trust_tunnel_cloudflared_config with ingress rules, and optionally cloudflare_zero_trust_access_application plus policies.

If you manage Access or DNS through Terraform, dashboard edits can desynchronize state:

If you need to modify the Access application, Access policy or DNS record, you must make the changes via Terraform. Changes made via the dashboard will break Terraform’s state. To prevent this from happening, set the dashboard to read-only.

— Cloudflare, Publish applications with Terraform

For tunnels behind strict firewalls, verify outbound connectivity on port 7844 before deploying (Ansible guide — tip).

Other product-specific Terraform entry points in this documentation set include Basin Pipelines Terraform reference and Turnstile widget management; each uses its own minimum provider version and resources.

Common mistakes when managing Cloudflare with Terraform

Section titled “Common mistakes when managing Cloudflare with Terraform”
  • Committing api_token, global API keys, or terraform.tfvars with live secrets.
  • Mixing provider v4 and v5 resource schemas (cloudflare_record vs cloudflare_dns_record, different Access policy shapes) in one stack.
  • Editing DNS, zone settings, Tunnel, or Access in the dashboard after Terraform owns them, then seeing unexpected plans or drift.
  • Skipping terraform plan before apply on production zones.
  • Using encrypted SSH private keys with Terraform provisioners that expect unencrypted keys (Ansible tunnel guide).
  1. Confirm the Terraform CLI runs (terraform -version) using the install method from Get started.
  2. From your configuration directory, run terraform init and terraform validate (and terraform plan) to ensure the Cloudflare provider resolves and the schema matches your resources.
  3. After terraform apply, verify DNS with dig on the hostname you manage and HTTP(S) with curl, as in Initialize Terraform.
  4. Compare live configuration with terraform show and the Cloudflare dashboard DNS > Records (or Zero Trust views for Tunnel and Access, where applicable).
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.