【问题标题】:importing gcp resource into terraform fails even if the resource exists即使资源存在,将 gcp 资源导入 terraform 也会失败
【发布时间】:2022-01-27 22:09:54
【问题描述】:

我正在尝试使用 terraform import 命令将现有 gcp 计算实例导入。 但是我遇到了这个错误,它说在使用导入命令时资源不存在:

 terraform import google_compute_instance.tf-instance-2 my_project_id

google_compute_instance.tf-instance-2: Import prepared!
  Prepared google_compute_instance for import
google_compute_instance.tf-instance-2: Refreshing state... [id=projects/qwiklabs-gcp-02-67a8ccc33dba/zones/us-central1-a/instances/qwiklabs-gcp-02-67a8ccc33dba]
╷
│ Error: Cannot import non-existent remote object
│
│ 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
│ associated with the provider's configured region or endpoint, or use "terraform apply" to create a new remote object for this resource.
╵


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


NAME: tf-instance-1
ZONE: us-central1-a
MACHINE_TYPE: n1-standard-1
PREEMPTIBLE:
INTERNAL_IP: 10.128.0.3
EXTERNAL_IP: 34.121.38.65
STATUS: RUNNING

NAME: tf-instance-2
ZONE: us-central1-a
MACHINE_TYPE: n1-standard-1
PREEMPTIBLE:
INTERNAL_IP: 10.128.0.2
EXTERNAL_IP: 35.184.192.60
STATUS: RUNNING

我尝试导入的实例是由 GCP 的代码实验室自动创建的。 我的 main.tf 仅包含 3 个块,terraform, google provider and google_compute_instance 资源。 我尝试过的事情:

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

main.tf 文件:

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "4.8.0"
    }
  }
}

provider "google" {
  project = var.project_id
  region  = var.region
  zone    = var.zone
}

resource "google_compute_instance" "tf-instance-2" {
  name = "tf-instance-2"
  #   id = "4193295884192005746"
  project      = var.project_id
  zone         = var.zone
  machine_type = "n1-standard-1"
  labels = {
    "goog-dm" = "qldm-10079641-937281f7192921b3"
  }
  boot_disk {
    initialize_params {
      image = "debian-10-buster-v20220118"
    }
  }
  network_interface {
    network = "default"
    access_config {

    }
  }
  allow_stopping_for_update = true
  metadata_startup_script   = <<-EOT
        #!/bin/bash
    EOT
}

【问题讨论】:

    标签: google-cloud-platform terraform terraform-provider-gcp


    【解决方案1】:

    根据importgoogle_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}}
    

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

    terraform import google_compute_instance.tf-instance-2 tf-instance-2
    

    【讨论】:

      猜你喜欢
      • 2022-08-04
      • 2020-09-23
      • 2015-10-26
      • 2021-12-27
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多