【发布时间】: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 init和terraform 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