Reputation: 425
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
}
Upvotes: 4
Views: 13453
Reputation: 91
landed here and previous Matt's reply gave us the hint.
to add some value, if you're using google-shell, beware its being launched on a specific project, so if you want to import cross-project resources it's required to add the projects/{{project}}/
or {{project}}/
prefix.
pretty obvious but got us some headaches, hope it helps someone
Upvotes: 1
Reputation: 11
Use
$ terraform import google_compute_instance.tf-instance-2 {{my_project_id}}/{{zone}}/tf-instance-2
like is a documentation
$ terraform import google_compute_instance.default projects/{{project}}/zones/{{zone}}/instances/{{name}}
Upvotes: 1
Reputation: 1995
I encountered the same issue, with an initial Terraform API call resulting in status code 409, Error: Error creating Service: googleapi: Error 409: Requested entity already exists
. Thinking that if it already exists, I should import it, I was then presented with OP's error Error: Cannot import non-existent remote object
.
In the case of Cloud Monitoring services, the name is far from simple. To find it, I had to navigate to Cloud Monitoring services, select the three dots at the end of the relevant service's row, choose Edit Display Name, and then copy the name out of the displayed JSON. The name wound up having the form, projects/<project_number>/services/wl:<project_name>-zone-<zone>-<cluster_name>-<namespace>-Deployment-<deployment_name>
(this is for a GKE Workload).
Upvotes: 0
Reputation: 28739
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
Upvotes: 8