A command line utility to facilitate terraforming your existing Cloudflare resources.
A command line utility to facilitate terraforming your existing Cloudflare resources.
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.
…Cloudflare supports two authentication methods to the API:
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='[email protected]'
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_IDcf-terraforming supports the following environment variables:
Alternatively, if using a config file, then specify the inputs using the same
names the flag names. Example:
cat ~/.cf-terraforming.yaml
email: "[email protected]"
key: "<key>"
#or
token: "<token>"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.
terraform init has run and providers installed). See the provider documentation if you have not yet setup the Terraform directory.brew tap cloudflare/cloudflare
brew install cloudflare/cloudflare/cf-terraforming[!NOTE] If you have installed an older version of
cf-terraformingvia Homebrew, you may need to first uninstallcf-terraformingand then install it to pick up the updated install process and address the signing/notarisation issues.
go install github.com/cloudflare/cf-terraforming/cmd/cf-terraforming@latestIf you use another OS, you will need to download the release directly from GitHub Releases or build the Go source.
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_IDInternally, 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.
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"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.
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_ai_gateway | account | |
| cloudflare_ai_search_instance | account | |
| cloudflare_ai_search_namespace | account | |
| cloudflare_ai_search_token | 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 |
No open issues yet, or sync has not completed.