背景
我有两个谷歌云项目:[project1] 和 [project2]。[project1] 有一个名为my-vm
. 我想复制my-vm
到 [project2]。
我做的步骤
所以,我创建了这个 terraform 文件 ( main.tf
):
provider "google" {
credentials = "${file("service-account.json")}"
project = "[project2]"
region = "us-central1"
}
将其保存到新目录中。现在,运行以下命令:
$ 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)
}
在这一点上,我发现我错过了resource "google_compute_instance" "my-vm"
声明。所以,我将它添加到main.tf
. 现在它看起来像这样:
provider "google" {
credentials = "${file("service-account.json")}"
project = "[project2]"
region = "us-central1"
}
resource "google_compute_instance" "my-vm" {
}
现在,我正在运行相同的terraform import
命令 agian 并且它成功了。创建了一个terraform.tfstate
文件。但是,文件main.tf
没有改变。我期待在其中看到 vm 导入的数据,但它resource "google_compute_instance" "my-vm"
是空的。奇怪的...
现在,我正在运行命令plan
并得到了这个:
$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.
问题:
- 为什么导入资源后无法调用plan方法?
- 我看到了一些使用 terraform 复制和部署的示例。所有这些示例都是基于机器的基本图像复制机器。因此,如果开发人员对虚拟机实例添加了一些更改,它就不会出现在重复的资源 ([project2]) 中。这是否可以复制 vm-disk 而不是 vm-image?