Reputation: 6893
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].
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.
Upvotes: 0
Views: 1869
Reputation: 6920
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:
GoogleCloudPlatform/terraformer
CLI tool to generate terraform files from existing infrastructure (reverse Terraform).
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:
Upvotes: 1