即使资源存在,将 gcp 资源导入 terraform 也会失败

ano*_*ain 4 google-cloud-platform terraform terraform-provider-gcp

我正在尝试将现有的 gcp 计算实例导入到terraform import命令将现有的 gcp 计算实例导入到其中。\n但是我遇到了此错误,该错误表明在使用导入命令时资源不存在:

\n
 terraform import google_compute_instance.tf-instance-2 my_project_id\n\n
Run Code Online (Sandbox Code Playgroud)\n
google_compute_instance.tf-instance-2: Import prepared!\n  Prepared google_compute_instance for import\ngoogle_compute_instance.tf-instance-2: Refreshing state... [id=projects/qwiklabs-gcp-02-67a8ccc33dba/zones/us-central1-a/instances/qwiklabs-gcp-02-67a8ccc33dba]\n\xe2\x95\xb7\n\xe2\x94\x82 Error: Cannot import non-existent remote object\n\xe2\x94\x82\n\xe2\x94\x82 While attempting to import an existing object to "google_compute_instance.tf-instance-2", the provider detected that no object exists with the given id. Only pre-existing objects can be imported; check that the id is correct and that it is\n\xe2\x94\x82 associated with the provider's configured region or endpoint, or use "terraform apply" to create a new remote object for this resource.\n\xe2\x95\xb5\n\n\n
Run Code Online (Sandbox Code Playgroud)\n

但是当我列出可用的 gcloud 计算实例时tf-instance-2(我尝试导入的实例)时,就在那里。

\n
\nNAME: tf-instance-1\nZONE: us-central1-a\nMACHINE_TYPE: n1-standard-1\nPREEMPTIBLE:\nINTERNAL_IP: 10.128.0.3\nEXTERNAL_IP: 34.121.38.65\nSTATUS: RUNNING\n\nNAME: tf-instance-2\nZONE: us-central1-a\nMACHINE_TYPE: n1-standard-1\nPREEMPTIBLE:\nINTERNAL_IP: 10.128.0.2\nEXTERNAL_IP: 35.184.192.60\nSTATUS: RUNNING\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试导入的实例是由 GCP 的 Codelab 自动创建的。\n我的 main.tf 仅包含 3 个块,terraform, google provider and google_compute_instance资源。\n我尝试过的操作:

\n
    \n
  • 更改 terraform 和 google 提供程序的版本
  • \n
  • terraform initterraform init -refconfigure在运行导入命令之前。
  • \n
  • 确保实例的所有属性都在 terraform 中。
  • \n
\n

main.tf 文件:

\n
terraform {\n  required_providers {\n    google = {\n      source  = "hashicorp/google"\n      version = "4.8.0"\n    }\n  }\n}\n\nprovider "google" {\n  project = var.project_id\n  region  = var.region\n  zone    = var.zone\n}\n\nresource "google_compute_instance" "tf-instance-2" {\n  name = "tf-instance-2"\n  #   id = "4193295884192005746"\n  project      = var.project_id\n  zone         = var.zone\n  machine_type = "n1-standard-1"\n  labels = {\n    "goog-dm" = "qldm-10079641-937281f7192921b3"\n  }\n  boot_disk {\n    initialize_params {\n      image = "debian-10-buster-v20220118"\n    }\n  }\n  network_interface {\n    network = "default"\n    access_config {\n\n    }\n  }\n  allow_stopping_for_update = true\n  metadata_startup_script   = <<-EOT\n        #!/bin/bash\n    EOT\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Mat*_*ard 8

根据import文档google_compute_instance

可以使用以下任何可接受的格式导入实例:

$ terraform import google_compute_instance.default projects/{{project}}/zones/{{zone}}/instances/{{name}}
$ terraform import google_compute_instance.default {{project}}/{{zone}}/{{name}}
$ terraform import google_compute_instance.default {{name}}
Run Code Online (Sandbox Code Playgroud)

name这里可能是最简单的,因此我们可以修改命令import以相应地定位它:

terraform import google_compute_instance.tf-instance-2 tf-instance-2
Run Code Online (Sandbox Code Playgroud)