Background
I have two Google cloud projects: [project1] and [project2]. [project1] has a virtual machine instance called my-vm
. I want to duplicate my-vm
into [project2].
Steps I done
So, I created this terraform file (main.tf
):
provider "google" {
credentials = "${file("service-account.json")}"
project = "[project2]"
region = "us-central1"
}
saved it into new directory. Now, run this commands:
$ terraform init
$ terraform import google_compute_instance.my-vm [project1]/us-central1-a/my-vm
Error: resource address "google_compute_instance.my-vm" does not exist in the configuration.
Before importing this resource, please create its configuration in the root module. For example:
resource "google_compute_instance" "my-vm" {
# (resource arguments)
}
At this point I figured out that I missed the resource "google_compute_instance" "my-vm"
statement. So, I added it to main.tf
. Now it's looks like this:
provider "google" {
credentials = "${file("service-account.json")}"
project = "[project2]"
region = "us-central1"
}
resource "google_compute_instance" "my-vm" {
}
Now, I was running the same terraform import
command agian and it's come with success. A terraform.tfstate
file was created. BUT, the main.tf
file wasn't changed. I was expecting to see the vm imported data in it, but the resource "google_compute_instance" "my-vm"
was empty. Strange...
Now, I was running the command plan
and got this:
$terraform plan
Error: Insufficient network_interface blocks
on line 0:
(source code not available)
At least 1 "network_interface" blocks are required.
Error: Insufficient boot_disk blocks
on line 0:
(source code not available)
At least 1 "boot_disk" blocks are required.
Error: Missing required argument
on main.tf line 7, in resource "google_compute_instance" "my-vm":
7: resource "google_compute_instance" "my-vm" {
The argument "name" is required, but no definition was found.
Error: Missing required argument
on main.tf line 7, in resource "google_compute_instance" "my-vm":
7: resource "google_compute_instance" "my-vm" {
The argument "machine_type" is required, but no definition was found.
Questions:
- Why after importing the resource I can't call the plan method?
- I saw some example to copy & deploy with terraform. All of those example was duplicating the machine, based on it's basic image. So, if the developer was added some changes to the virtual machine instance, it's won't appear in the duplicated resource ([project2]). Is this possible to duplicate the vm-disk, instead of vm-image?