Moving a hand-built Cloudflare account into Terraform without downtime
Most Cloudflare accounts start in the dashboard. It works, but nobody can say why a setting has its value, and every change is a click with no review and no history. Our own account was like that: two zones with DNS records, several Cloudflare Tunnels to a server running n8n, Access applications and a policy in front of them, four response header rules, HSTS, SSL and cache settings. Some of it…
Moving a Cloudflare account into Terraform without any downtime required careful planning and execution. The process involved utilizing Terraform's S3 backend for state management and generating a Cloudflare API token with specific permissions. The team created a module called "cloudflare-site" to handle the configuration of individual zones, including DNS records, tunnels, access policies, header rules, and settings.
To import the existing Cloudflare resources into Terraform, the team generated import blocks for each resource type, ensuring that the IDs matched the exact format required by the Cloudflare provider. The import process involved fetching the IDs from the Cloudflare API, such as record IDs using GET /zones/zone-id/dns_records. For zone settings, the team adopted the existing settings rather than recreating them, as the API always returns an existing setting and cannot create a new one.
After the import, the team verified that the Terraform plan was empty, indicating that the configuration matched the existing Cloudflare account. Any differences observed during the plan review were noted and addressed, such as setting Cloudflare's default values explicitly to avoid false null attributes. Additionally, the team ensured that HSTS settings, rulesets, and header rules were imported and configured correctly, matching the existing configuration in Cloudflare.
By following this methodical approach and adhering to the rule of maintaining an empty plan after each step, the team successfully migrated their Cloudflare account to Terraform without any service disruptions.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to
More from Dev.to on urgent.news
Report a problem with this story
Get the day's news by email. Daily, weekly or monthly. Sign up