0
votes

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:

  1. Why after importing the resource I can't call the plan method?
  2. 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?
1

1 Answers

1
votes

Terraform currently can't generate configuration for you, import only saves data to the state file.

The current implementation of Terraform import can only import resources into the state. It does not generate configuration. A future version of Terraform will also generate configuration.

Because of this, prior to running terraform import it is necessary to write manually a resource configuration block for the resource, to which the imported object will be mapped.

While this may seem tedious, it still gives Terraform users an avenue for importing existing resources. A future version of Terraform will fully generate configuration, significantly simplifying this process.

There are third-party tools that can generate Terrafrom configurations for existing resource:

Is this possible to duplicate the vm-disk, instead of vm-image?

You can create instance template from your VM and use it to create new VMs: