Example: Cloudflare Zones
Brief overview & GitHub example repo that shows how to create & manage zones on Cloudflare, with Terraform.
Example Repo
https://github.com/boomam/example-terraform-cloudflare-zones
Pre-Reqs
Terraform & Git is installed.
Windows (10 & 11) - 'Chocolatey'
Main overview for install steps are here
- Open an administrative powershell window.
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
- Test Choco has installed
choco
If you see a choco output, you're good to continue!
MacOS - 'Home Brew'
Main overview for install steps are here
- Open Terminal.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Test brew has installed
brew
If you see a brew output, you're good to continue!
Install Terraform
Run, either -
Windows
choco install terraform
MacOS
brew install terraform
Modify example files
In the example repo, you will need to modify several files for the example code to work.
Enter your information
First, modify variables.tf
and enter your cloudflare API key, ZoneID & email address
Create your records
Open and duplicate/edit each file that begins in 'records_', changing/adding/removing example DNS records as needed.
Initialize Terraform
Browse to where you have the TF example files downloaded too, and run -
terraform init
This step does a basic check of your files, and pre-downloads files (providers) needed to perform the operations against your cloudflare account.
Plan
This step 'scans' your files for syntax, and shows the planned operation if ran afresh.
terraform plan
On the output for 'plan' and 'apply', a '+' means it will be created, '-' means it will be deleted, '=' means there is no change.
Apply
This step is similar to 'plan', but at the end will ask if you want to apply the configuration to your cloudflare account.
terraform apply
Feeling lazy?
Luckily, Cloudflare are one of the most productive, comprehensive companies around, and their documentation and toolsets are top notch, as such, not only is their terraform provider documentation great, but they also have a 'reverse terraform' module you can use to dump existing accounts to make things easier to build out.
You can read up on it here
Useful Links
- Cloudflare: Finding your account ID & Zone ID
- Cloudflare: Creating an API Token
- Cloudflare Terraform