Terraform.tfstate don't sync with the configuration

  • I have a bunch of existing DNS records in Cloudflare.
  • I want to start using Terraform to manage them.
  • I used the cf-terraforming tool to import the existing configuration into a Terraform state file (terraform.tfstate ).
  • However, running terraform plan doesn’t detect the imported resources.
  • This leads Terraform to think no resources are managed and wants to create new ones (even though they already exist).

Basically, the import seems successful, but Terraform isn’t recognizing the imported resources. How can I resolved it?

Here is my structure of folder.
cloudflare

  • main.tf
  • provider.tf
    ( _modules )
    - a.tf
    - variables.tf
    main.tf
module "name" {
  source = "./_modules"
}

terraform {
  required_providers {
    cloudflare = {
      source = "cloudflare/cloudflare"
      version = "4.31.0"
    }
  }
}

a.tf

resource "cloudflare_record" "terraform_managed_resource_a06f86aa8dbdfb2e5a598f461fee09fb" {
  name    = "ta"
  proxied = false
  ttl     = 1
  type    = "A"
  value   = "10.0.0.1"
  zone_id = var.zone_id
}

resource "cloudflare_record" "terraform_managed_resource_e78e4843168b8e4babd23b1651e15d8e" {
  name    = "ta"
  proxied = true
  ttl     = 1
  type    = "A"
  value   = "19.24.212.32"
  zone_id = var.zone_id
}

resource "cloudflare_record" "terraform_managed_resource_078e46a08aeeb10763b14f08a7e8cf0d" {
  name    = "12"
  proxied = false
  ttl     = 1
  type    = "TXT"
  value   = "123"
  zone_id = var.zone_id
}


resource "cloudflare_zone" "taiho1805" {
    account_id = var.account_id
    zone = ""
}

terraform.tfstate

{
  "version": 4,
  "terraform_version": "1.7.4",
  "serial": 4,
  "lineage": "c9207915-e08c-9f6d-8c4f-24969a45b1e5",
  "outputs": {},
  "resources": [
    {
      "module": "module.pharmacityio",
      "mode": "managed",
      "type": "cloudflare_record",
      "name": "terraform_managed_resource_078e46a08aeeb10763b14f08a7e8cf0d",
      "provider": "module.pharmacityio.provider[\"registry.terraform.io/cloudflare/cloudflare\"]",
      "instances": [
        {
          "schema_version": 2,
          "attributes": {
            "allow_overwrite": null,
            "comment": "",
            "created_on": "2024-05-07T03:02:38.860819Z",
            "data": [],
            "hostname": "12.taiho1805.com",
            "id": "078e46a08aeeb10763b14f08a7e8cf0d",
            "metadata": {
              "auto_added": "false",
              "managed_by_apps": "false",
              "managed_by_argo_tunnel": "false"
            },
            "modified_on": "2024-05-07T03:02:46.738416Z",
            "name": "12",
            "priority": null,
            "proxiable": false,
            "proxied": false,
            "tags": [],
            "timeouts": null,
            "ttl": 1,
            "type": "TXT",
            "value": "123",
            "zone_id": "8c1f2f37ed851457c3a0c49893f0cb00"
          },
          "sensitive_attributes": [],
          "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjozMDAwMDAwMDAwMCwidXBkYXRlIjozMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMiJ9"
        }
      ]
    },
    {
      "module": "module.pharmacityio",
      "mode": "managed",
      "type": "cloudflare_record",
      "name": "terraform_managed_resource_a06f86aa8dbdfb2e5a598f461fee09fb",
      "provider": "module.pharmacityio.provider[\"registry.terraform.io/cloudflare/cloudflare\"]",
      "instances": [
        {
          "schema_version": 2,
          "attributes": {
            "allow_overwrite": null,
            "comment": "",
            "created_on": "2024-05-07T03:02:29.249016Z",
            "data": [],
            "hostname": "ta.taiho1805.com",
            "id": "a06f86aa8dbdfb2e5a598f461fee09fb",
            "metadata": {
              "auto_added": "false",
              "managed_by_apps": "false",
              "managed_by_argo_tunnel": "false"
            },
            "modified_on": "2024-05-07T03:02:29.249016Z",
            "name": "ta",
            "priority": null,
            "proxiable": false,
            "proxied": false,
            "tags": [],
            "timeouts": null,
            "ttl": 1,
            "type": "A",
            "value": "10.0.0.1",
            "zone_id": "8c1f2f37ed851457c3a0c49893f0cb00"
          },
          "sensitive_attributes": [],
          "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjozMDAwMDAwMDAwMCwidXBkYXRlIjozMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMiJ9"
        }
      ]
    },
    {
      "module": "module.pharmacityio",
      "mode": "managed",
      "type": "cloudflare_record",
      "name": "terraform_managed_resource_e78e4843168b8e4babd23b1651e15d8e",
      "provider": "module.pharmacityio.provider[\"registry.terraform.io/cloudflare/cloudflare\"]",
      "instances": [
        {
          "schema_version": 2,
          "attributes": {
            "allow_overwrite": null,
            "comment": "",
            "created_on": "2024-05-07T03:02:22.093185Z",
            "data": [],
            "hostname": "ta.taiho1805.com",
            "id": "e78e4843168b8e4babd23b1651e15d8e",
            "metadata": {
              "auto_added": "false",
              "managed_by_apps": "false",
              "managed_by_argo_tunnel": "false"
            },
            "modified_on": "2024-05-07T03:02:22.093185Z",
            "name": "ta",
            "priority": null,
            "proxiable": true,
            "proxied": true,
            "tags": [],
            "timeouts": null,
            "ttl": 1,
            "type": "A",
            "value": "19.24.212.32",
            "zone_id": "8c1f2f37ed851457c3a0c49893f0cb00"
          },
          "sensitive_attributes": [],
          "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjozMDAwMDAwMDAwMCwidXBkYXRlIjozMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMiJ9"
        }
      ]
    },
    {
      "module": "module.pharmacityio",
      "mode": "managed",
      "type": "cloudflare_zone",
      "name": "taiho1805",
      "provider": "module.pharmacityio.provider[\"registry.terraform.io/cloudflare/cloudflare\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "account_id": "8d574d85f775136e1e58ec8cf96fc0b3",
            "id": "8c1f2f37ed851457c3a0c49893f0cb00/",
            "jump_start": null,
            "meta": {
              "phishing_detected": false,
              "wildcard_proxiable": false
            },
            "name_servers": [
              "beau.ns.cloudflare.com",
              "isla.ns.cloudflare.com"
            ],
            "paused": false,
            "plan": "free",
            "status": "pending",
            "type": "full",
            "vanity_name_servers": [],
            "verification_key": "",
            "zone": "taiho1805.com"
          },
          "sensitive_attributes": [],
          "private": "eyJzY2hlbWFfdmVyc2lvbiI6IjAifQ=="
        }
      ]
    }
  ],
  "check_results": null
}

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.