How to import/download already existing cloudflare resource(s) using terraform?

Viewed 282

If I have existing cloudflare settings (using web console), how to import/download existing cloudflare resource(s),e.g DNS records using terraform? So, I could use it as a starting point to modify/update it.

Thanks.

3 Answers

Each Terraform resource has an example of importing existing data. For example, the record import

$ terraform import cloudflare_record.default ae36f999674d196762efcc5abb06b345/d41d8cd98f00b204e9800998ecf8427e

Your steps will look like this:

  1. Describe a Terraform configuration that contains the resources you want to manage with Terraform.
  2. Initialize the created configuration using terraform init and after (this is convenient) quickly get the addresses of the resources that you need for import using terraform plan.
  3. Next, you need to get the IDs of existing resources, they will also be required for import. For the Cloudflare Zone resource, this is easy to do, just refer to this instruction, for other resources, for example, for Cloudflare record you will either need to use cf-terraforming or Cloudflare API.
  4. Import your resources using the terraform import RESOURCE_ADDRESS RESOURCE_ID command.

Another way is to delete your resources and recreate them with Terraform instead of importing them. This can make your infrastructure unavailable for some time.

I highly recommend that you read these guides:

  1. https://developers.cloudflare.com/terraform/advanced-topics/import-cloudflare-resources/.
  2. https://github.com/alex-feel/terraform-cloudflare-zone/wiki/Migration (it is written for a module, but is equally applicable to "raw" resources).
Related