gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:02:46 26 4
gpt4 key购买 nike

我正在尝试使用 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 个 block ,terraform、google provider 和 google_compute_instance 资源。我尝试过的事情:

  • 更改 terraform 和 google provider 的版本
  • 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_compute_instanceimport 文档:

Instances can be imported using any of these accepted formats:

$ 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

关于google-cloud-platform - 即使资源存在,将 gcp 资源导入 terraform 也会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70879211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com