I am trying to import existing gcp compute instance into with terraform import
command.
But I am encountering this error which says the resource does not exist while using the import command as :
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.
╵
But when I list the available gcloud compute instances tf-instance-2
(the instance I am trying to import) is there.
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
The instances that I am trying to import are automatically created by GCP's codelabs.
My main.tf consists of only 3 blocs, terraform, google provider and google_compute_instance
resource.
Things I have tried:
terraform init
and terraform init -refconfigure
before running the import commands.main.tf file:
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
}
According to the import
documentation for google_compute_instance
:
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
would probably be easiest here, and so we can modify the import
command to target it accordingly:
terraform import google_compute_instance.tf-instance-2 tf-instance-2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With