ZONE_ID="" API_TOKEN=""
cloudflare_api_token = "" cloudflare_zone_id = ""
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.
.
├── account-personal/ # Contains different accounts/domains
│ ├── ahmadraza.in/ # Terraform configs for ahmadraza.in
│ ├── kubecloud.in.net/ # Terraform configs for kubecloud.in.net
│ └── users/ # Other Cloudflare-related user settings
├── cf-terraforming/ # Terraforming scripts for Cloudflare
├── import.sh # Script to import existing DNS records into Terraform state
├── import.tf # Terraform resource blocks generated from Cloudflare records
├── main.tf # Main Terraform configuration
├── output.tf # Terraform outputs
├── provider.tf # Cloudflare provider configuration
├── README.md # Documentation (You're reading it!)
├── secret.md # Stores API keys and sensitive information (DO NOT SHARE!)
├── terraform.tfstate # Terraform state file (tracks deployed resources)
├── terraform.tfstate.backup # Backup of Terraform state
├── terraform.tfvars # Terraform variables
├── tf-gen.sh # Script to generate Terraform resource blocks
└── variables.tf # Variable definitions for Terraform
To use this Terraform setup for managing Cloudflare DNS, you need:
Generate an API token with the following permissions:
- Zone.Zone Read
- Zone.DNS Read & Write
💡 How to generate API Token:
- Go to Cloudflare Dashboard
- Navigate to My Profile > API Tokens
- Create a custom token with the above permissions
- Copy and save the token securely (you'll need it for Terraform)
Each Cloudflare domain (zone) has a unique Zone ID.
💡 How to find your Zone ID:
- Go to Cloudflare Dashboard
- Select your domain
- Under Overview, find the Zone ID at the bottom of the page.
Edit the provider.tf
file with your API token:
provider "cloudflare" {
api_token = "your-cloudflare-api-token"
}
Alternatively, set it as an environment variable:
export CLOUDFLARE_API_TOKEN="your-cloudflare-api-token"
Use the tf-gen.sh
script to fetch existing Cloudflare DNS records and create Terraform configuration:
bash tf-gen.sh
This will generate import.tf
containing Terraform resource blocks for each DNS record.
Run the import.sh
script to import existing records into Terraform state:
bash import.sh
This prevents Terraform from trying to recreate records that already exist.
After importing, run Terraform to verify and apply changes:
terraform init
terraform plan
terraform apply
- To add new records: Modify
main.tf
and runterraform apply
. - To update existing records: Change
main.tf
and runterraform plan
to preview changes. - To remove records: Delete the record from
main.tf
and runterraform apply
.
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
.
Add this module to your Terraform project:
module "cloudflare_dns" {
source = "<path-to-this-module>"
cloudflare_zone_id = var.cloudflare_zone_id
records = [
{
name = "app"
type = "A"
content = "198.51.100.4"
ttl = 1
proxied = true
},
{
name = "www"
type = "CNAME"
content = "app.example.com"
ttl = 300
proxied = false
comment = "Non-proxied www record"
}
]
}
Inputs:
cloudflare_zone_id
: The Cloudflare Zone ID for your domain.records
: List of DNS records to create (see example above).
Outputs:
dns_records
: Map of all created DNS records and their details.
Provider: You must configure the Cloudflare provider in your root module, e.g.:
provider "cloudflare" {
api_token = var.cloudflare_api_token
}
Example terraform.tfvars
:
cloudflare_api_token = "your-token"
cloudflare_zone_id = "your-zone-id"
- Automate DNS updates using CI/CD
- Manage multiple Cloudflare accounts using workspaces
- Enhance security by using environment variables for sensitive data
Ahmad Raza - ahmadraza.in
For more guides, visit: docs.ahmadraza.in 🚀
✔️ 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