Cloudflare

cf-terraform

  • Retrieve records from cloudflare to convert to local terraform states

Cloudflare DNS

Export records from cloudflare

# if using API Token from Account Icon > Profiles > API Token
export CLOUDFLARE_API_TOKEN="Hzsq3Vub-7Y-hSTlAaLH3Jq_YfTUOCcgf22_Fs-j"

# if using API Key
export CLOUDFLARE_EMAIL='[email protected]'
export CLOUDFLARE_API_KEY='1150bed3f45247b99f7db9696fffa17cbx9'

# specify zone ID from Account Home > Select Domain > Select Zone ID from API Section on bottom right
export CLOUDFLARE_ZONE_ID='81b06ss3228f488fh84e5e993c2dc17'

# now call cf-terraforming, from terraform folder
cf-terraforming generate \
  --resource-type "cloudflare_dns_record" \
  --zone $CLOUDFLARE_ZONE_ID

|- Alternately, can use config file:

  • Then, save generated records to .tf file. But these records haven't become terraform state yet. Follow next step to import them.

Import records into terraform state

  • Above commands will generate a list of terraform import commands. Then copy them and paste to run the generated terraform import commands

  • After that check state list using: terraform state list

Terraform

  • terraform plan [-out=$filename]: to dry run

  • TF_LOG=DEBUG terraform plan: dry run with debug log

  • terraform apply [$filename - from terraform plan]: to apply

Using Environment Variable as Variable

  • Define env variable with TF_VAR Prefix: export TF_VAR_api_token="Token"

    • Terraform will automatically recognize TF_VAR_api_token as the value for the api_token variable.

  • Define Variable in Terraform file: variables.tf

  • Use variable in terraform code

Another way to use env variable

  • This method allows Terraform to read any environment variable, even if it doesn’t follow the TF_VAR_ naming convention.

  • Another way:

Remove Terraform State

  • terraform state list

  • terraform state rm $resource_address

  • rm -rf terraform.tfstate terraform.tfstate.backup: reset terraform state completely

  • terraform init: reinitialize terraform which make it lose track of all existing resources

Revert to a previous state

  • copy terraform.tfstate.backup terraform.tfstate

  • terraform refresh

Compare terraform state

  • terraform show -json terraform.tfstate > current_state.json

  • terraform show -json terraform.tfstate.backup > backup_state.json

  • diff -u backup_state.json current_state.json

  • or sort keys to ensure consistent ordering before comparing

Check differences in terraform state list

Troubleshooting Terraform

Fixing Existing Records

Rename resource in terraform state

  • terraform state mv cloudflare_record.terraform_managed_resource_ad1bc14241c6e688af36a14842c1d83a cloudflare_record.routerlocal

  • then, terraform apply

Importing Existing Record

terraform import cloudflare_record.routerlocal $zone_id/$record_id: import to terraform local state, order is $localname $zoneid/$remotename

Allow Overwriting

Cloudflare Zero Trust Tunnel

Exporting Tunnel

  • Define Terraform Config main.tf

  • Generate Terraform Config

  • Then copy generated config to terraform main.tf file above

  • Import Tunnel Config into Terraform State

  • Then copy the terraform import command of the tunnel to be imported, then paste and run it

Reference

  • https://www.youtube.com/watch?v=FmYvrxYvBP0&t=3s

  • https://github.com/cloudflare/cf-terraforming

  • https://blog.cloudflare.com/getting-started-with-terraform-and-cloudflare-part-1/

Last updated