Cf-terraforming state exporter tool

Get the source code

Read the blog post

Cf-terraforming is a command line utility written in Go that lets you download your Cloudflare API resources into Terraform HCL config format.

This makes it ideal for users who want to migrate their Cloudflare setup into Terraform but don't want to write all the config files from scratch.

We're currently extending cf-terraforming to export and save terraform state as well, which will streamline the process of migrating even further.

Example usage


Let’s say you’re migrating your Cloudflare configuration to Terraform and you want to describe your Spectrum applications. You simply call cf-terraforming with your credentials, zone, and the spectrum_application command, like so:

go run cmd/cf-terraforming/main.go --email $CLOUDFLARE_EMAIL --key $CLOUDFLARE_TOKEN --account $CLOUDFLARE_ACCT_ID spectrum_application

Cf-terraforming will contact the Cloudflare API on your behalf and define your resources in a format that Terraform understands:

resource"cloudflare_spectrum_application""1150bed3f45247b99f7db9696fffa17cbx9" {
    protocol = "tcp/8000"
    dns = {
        type = "CNAME"
        name = "example.com"
    }
    ip_firewall = "true"
    tls = "off"
    origin_direct = [ "tcp://37.241.37.138:8000", ]
}

Technical details and takeaways


Cf-terraforming required us to become intimately familiar with Terraform's HCL format as well as Terraform's state management system.

It works by wrapping Cloudflare Go and contacting the Cloudflare API on the user's behalf.

For command flag parsing and CLI scaffolding we used the excellent Viper and Cobra libraries.

There are two modes you can run cf-terraforming in: the default mode will generate Terraform HCL for the resources you specify. The second mode allows you to export the actual Terraform state file contents for your given resources.

In order to pull this off, we made use of Cobra's pre and post run hooks. This allows us to encapsulate state generation logic within each command, and leave the state file scaffolding to a post run function.