Skip to main content

🌐 Managing Cloudflare DNS with Terraform

ZONE_ID="" API_TOKEN=""

tf vars file for token

cloudflare_api_token = "" cloudflare_zone_id = ""

Using Azure Blob Storage for State Backend

terraform init -migrate-state export ARM_ACCESS_KEY="" This repository automates the management of Cloudflare DNS records using Terraform.
It includes scripts to import existing DNS records, generate Terraform configurations, and apply changes efficiently.

📂 Directory Structure


🛠️ Setup Requirements

To use this Terraform setup for managing Cloudflare DNS, you need:

1️⃣ Cloudflare API Token

Generate an API token with the following permissions:
  • Zone.Zone Read
  • Zone.DNS Read & Write
💡 How to generate API Token:
  1. Go to Cloudflare Dashboard
  2. Navigate to My Profile > API Tokens
  3. Create a custom token with the above permissions
  4. Copy and save the token securely (you’ll need it for Terraform)

2️⃣ Cloudflare Zone ID

Each Cloudflare domain (zone) has a unique Zone ID. 💡 How to find your Zone ID:
  1. Go to Cloudflare Dashboard
  2. Select your domain
  3. Under Overview, find the Zone ID at the bottom of the page.

🚀 Step-by-Step Usage

1️⃣ Configure Terraform Provider

Edit the provider.tf file with your API token:
Alternatively, set it as an environment variable:

2️⃣ Generate Terraform Resource Blocks

Use the tf-gen.sh script to fetch existing Cloudflare DNS records and create Terraform configuration:
This will generate import.tf containing Terraform resource blocks for each DNS record.

3️⃣ Import Existing Cloudflare DNS Records

Run the import.sh script to import existing records into Terraform state:
This prevents Terraform from trying to recreate records that already exist.

4️⃣ Apply Terraform Configuration

After importing, run Terraform to verify and apply changes:

🔄 Handling DNS Record Changes

  • To add new records: Modify main.tf and run terraform apply.
  • To update existing records: Change main.tf and run terraform plan to preview changes.
  • To remove records: Delete the record from main.tf and run terraform apply.

🔥 Troubleshooting

🛑 Terraform is trying to recreate existing records

If terraform plan shows it will recreate existing records, it’s likely because:
  • The import was not done correctly
  • The imported state differs from the generated Terraform code
✅ Solution: Ensure you run import.sh before terraform plan.

📜 Example Terraform Configuration

A typical DNS record managed with Terraform looks like this:

🎯 Next Steps

  • Automate DNS updates using CI/CD
  • Manage multiple Cloudflare accounts using workspaces
  • Enhance security by using environment variables for sensitive data

👤 Author

Ahmad Raza - ahmadraza.in
For more guides, visit: docs.ahmadraza.in 🚀

📌 What This README Covers

✔️ Terraform setup for Cloudflare DNS
✔️ Generating Terraform configuration from existing records
✔️ Importing existing DNS records into Terraform state
✔️ Managing DNS records efficiently
✔️ Troubleshooting common issues