MCPcopy Index your code
hub / github.com/cloudflare/cf-terraforming

github.com/cloudflare/cf-terraforming @v0.27.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.27.0 ↗ · + Follow
49 symbols 184 edges 12 files 18 documented · 37%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Cloudflare Terraforming

Overview

cf-terraforming is a command line utility to facilitate terraforming your existing Cloudflare resources. It does this by using your account credentials to retrieve your configurations from the Cloudflare API and converting them to Terraform configurations that can be used with the Terraform Cloudflare provider.

This tool is ideal if you already have Cloudflare resources defined but want to start managing them via Terraform, and don't want to spend the time to manually write the Terraform configuration to describe them. The intention is that this would be a one-time HCL generation of whichever resource(s) you want to begin managing exclusively through Terraform.

Read the announcement blog for further details on using cf-terraforming in your workflow.

[!WARNING] This tool is not intended for use in CI.

[!NOTE] If you would like to export resources compatible with Terraform < 0.12.x, you will need to download an older release as this tool no longer supports it.

Usage

Usage:
  cf-terraforming import [flags]

Flags:
  -h, --help   help for import

Global Flags:
  -a, --account string                      Target the provided account ID for the command
  -c, --config string                       Path to config file (default "/Users/vaishak/.cf-terraforming.yaml")
  -e, --email string                        API Email address associated with your account
      --hostname string                     Hostname to use to query the API
  -k, --key string                          API Key generated on the 'My Profile' page. See: https://dash.cloudflare.com/profile
      --modern-import-block                 Whether to generate HCL import blocks for generated resources instead of terraform import compatible CLI commands. This is only compatible with Terraform 1.5+
      --provider-registry-hostname string   Hostname to use for provider registry lookups. Deprecated: this is no longer needed to be configured for custom registries.
      --resource-id key                     Resource type and IDs mapping in the format of key to comma separated values. Example: `cloudflare_zone_setting=always_online,cache_level,...`
      --resource-type string                Comma delimitered string of which resource(s) you wish to generate
      --terraform-binary-path string        Path to an existing Terraform binary (otherwise, one will be downloaded)
      --terraform-install-path string       Path to an initialized Terraform working directory (default ".")
  -t, --token string                        API Token
  -v, --verbose                             Specify verbose output (same as setting log level to debug)
  -z, --zone string                         Target the provided zone ID for the command

Authentication

Cloudflare supports two authentication methods to the API:

  • API Token - gives access only to resources and permissions specified for that token (recommended)
  • API key - gives access to everything your user profile has access to

Both can be retrieved on the user profile page.

[!TIP] We recommend that you store your Cloudflare credentials (API key, email, token) as environment variables as demonstrated below.

# if using API Token
export CLOUDFLARE_API_TOKEN='Hzsq3Vub-7Y-hSTlAaLH3Jq_YfTUOCcgf22_Fs-j'

# if using API Key
export CLOUDFLARE_EMAIL='user@example.com'
export CLOUDFLARE_API_KEY='1150bed3f45247b99f7db9696fffa17cbx9'

# specify zone ID
export CLOUDFLARE_ZONE_ID='81b06ss3228f488fh84e5e993c2dc17'

# now call cf-terraforming, e.g.
cf-terraforming generate \
  --resource-type "cloudflare_record" \
  --zone $CLOUDFLARE_ZONE_ID

cf-terraforming supports the following environment variables:

  • CLOUDFLARE_API_TOKEN - API Token based authentication
  • CLOUDFLARE_EMAIL, CLOUDFLARE_API_KEY - API Key based authentication

Alternatively, if using a config file, then specify the inputs using the same names the flag names. Example:

cat ~/.cf-terraforming.yaml
email: "email@domain.com"
key: "<key>"
#or
token: "<token>"

Example usage

cf-terraforming generate \
  --zone $CLOUDFLARE_ZONE_ID \
  --resource-type "cloudflare_record"

will contact the Cloudflare API on your behalf and result in a valid Terraform configuration representing the resource you requested:

resource "cloudflare_record" "terraform_managed_resource" {
  name = "example.com"
  proxied = false
  ttl = 120
  type = "A"
  value = "198.51.100.4"
  zone_id = "0da42c8d2132a9ddaf714f9e7c920711"
}

Some resource require an ID to be passed in to be able to either generate the hcl block or import command. The resources which require an id are listed in the table below for the v5 provider. Example usage:

cf-terraforming generate \
  --zone $CLOUDFLARE_ZONE_ID \
  --resource-type "cloudflare_hostname_tls_setting" \
  --resource-id "cloudflare_hostname_tls_setting=ciphers"

Define --terraform-binary-path on the generate command which will ensure we're reusing the installed version of terraform instead of fetching a new one each time, if you're seeing issues.

Prerequisites

  • A Cloudflare account with resources defined (e.g. a few zones, some load balancers, spectrum applications, etc)
  • A valid Cloudflare API key and sufficient permissions to access the resources you are requesting via the API
  • An initialised Terraform directory (terraform init has run and providers installed). See the provider documentation if you have not yet setup the Terraform directory.

Installation

Homebrew

brew tap cloudflare/cloudflare
brew install cloudflare/cloudflare/cf-terraforming

[!NOTE] If you have installed an older version of cf-terraforming via Homebrew, you may need to first uninstall cf-terraforming and then install it to pick up the updated install process and address the signing/notarisation issues.

Go

go install github.com/cloudflare/cf-terraforming/cmd/cf-terraforming@latest

If you use another OS, you will need to download the release directly from GitHub Releases or build the Go source.

Importing with Terraform state

cf-terraforming has the ability to generate the configuration for you to import existing resources.

Depending on your version of Terraform, you can generate the import block (Terraform 1.5+) using the --modern-import-block flag or the terraform import compatible CLI output (all versions).

This command assumes you have already ran cf-terraforming generate ... to output your resources.

# All versions of Terraform
cf-terraforming import \
  --resource-type "cloudflare_record" \
  --email $CLOUDFLARE_EMAIL \
  --key $CLOUDFLARE_API_KEY \
  --zone $CLOUDFLARE_ZONE_ID
# Terraform 1.5+ only
cf-terraforming import \
  --resource-type "cloudflare_record" \
  --modern-import-block \
  --email $CLOUDFLARE_EMAIL \
  --key $CLOUDFLARE_API_KEY \
  --zone $CLOUDFLARE_ZONE_ID

Using non-standard Terraform binaries

Internally, we use terraform-exec library to run Terraform operations in the same way that the CLI tooling would. If a terraform binary is not available on your system path, we will attempt to download the latest to use it.

Should you have the binary stored in a non-standard location, want to use an existing binary, or you wish to provide a Terraform compatible binary (such as tofu), you need to provide the --terraform-binary-path flag or CLOUDFLARE_TERRAFORM_BINARY_PATH environment variable to instruct cf-terraforming which you expect to use.

CDKTF

If you'd like to use cdktf for your project resources, you can pipe the output from cf-terraforming into cdktf convert in order to correctly generate CDKTF output automatically.

Example:

cf-terraforming generate \
  --resource-type "cloudflare_record" \
  --zone "0da42c8d2132a9ddaf714f9e7c920711" \
| cdktf convert --language "typescript" --provider "cloudflare/cloudflare"

Supported Resources

v5

Any resource that is released within the Terraform Provider is automatically supported for generation and import. The cf-terraforming cli tool should be able to generate the HCL config for resource in the version 5 of the provider. Certain resources generated might not pass terraform validate command due to inconsistencies with the schema. These are known issues and will be addressed in the later releases.

Generate

Any resources not listed may have known issues. The HCL config may still be generated but might need manual modifications.

Resource Type Identifier Type CLI Flags Example
cloudflare_account account
cloudflare_account_member account
cloudflare_account_subscription account
cloudflare_address_map account
cloudflare_api_shield_discovery_operation zone
cloudflare_api_shield_operation zone
cloudflare_api_shield_operation_schema_validation_settings zone cloudflare_api_shield_operation_schema_validation_settings=8255d5da-5a46-4928-ad00-01de7d48c1e7
cloudflare_api_shield_schema zone
cloudflare_api_shield_schema_validation_settings zone
cloudflare_argo_smart_routing zone
cloudflare_argo_tiered_caching zone
cloudflare_authenticated_origin_pulls zone cloudflare_authenticated_origin_pulls=jotsqcjaho.terraform.cfapi.net
cloudflare_authenticated_origin_pulls_certificate zone
cloudflare_bot_management zone
cloudflare_calls_sfu_app account
cloudflare_calls_turn_app account
cloudflare_certificate_pack zone
cloudflare_content_scanning_expression zone
cloudflare_custom_hostname zone
cloudflare_custom_hostname_fallback_origin zone
cloudflare_d1_database account
cloudflare_dns_firewall account

Core symbols most depended-on inside this repo

Shape

Function 47
Method 1
Struct 1

Languages

Go100%

Modules by API surface

internal/app/cf-terraforming/cmd/util.go14 symbols
internal/app/cf-terraforming/cmd/custom_processing.go8 symbols
internal/app/cf-terraforming/cmd/util_test.go6 symbols
internal/app/cf-terraforming/cmd/root.go4 symbols
internal/app/cf-terraforming/cmd/import.go4 symbols
internal/app/cf-terraforming/cmd/generate_test.go4 symbols
internal/app/cf-terraforming/cmd/post_process.go3 symbols
internal/app/cf-terraforming/cmd/generate.go3 symbols
internal/app/cf-terraforming/cmd/version.go1 symbols
internal/app/cf-terraforming/cmd/import_test.go1 symbols
cmd/cf-terraforming/main.go1 symbols

For agents

$ claude mcp add cf-terraforming \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page