使用 GCP 的 Terraform:将 vm 实例复制到不同的 gcp 项目
Terraform with GCP: copy vm instance to different gcp project
背景
我有两个 Google 云项目:[project1] 和 [project2]。 [project1] 有一个名为 my-vm
的虚拟机实例。我想将 my-vm
复制到 [project2].
我完成的步骤
因此,我创建了这个地形文件 (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
命令,并且成功了。已创建 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?
Terraform 目前无法为您生成配置,import
只能将数据保存到状态文件。
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.
有 third-party 种工具可以为现有资源生成 Terrafrom 配置:
GoogleCloudPlatform/terraformer
用于从现有基础设施生成 Terraform 文件的 CLI 工具(反向 Terraform)。
Is this possible to duplicate the vm-disk, instead of vm-image?
您可以从您的 VM 创建 instance template 并使用它来创建新的 VM:
背景
我有两个 Google 云项目:[project1] 和 [project2]。 [project1] 有一个名为 my-vm
的虚拟机实例。我想将 my-vm
复制到 [project2].
我完成的步骤
因此,我创建了这个地形文件 (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
命令,并且成功了。已创建 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?
Terraform 目前无法为您生成配置,import
只能将数据保存到状态文件。
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.
有 third-party 种工具可以为现有资源生成 Terrafrom 配置:
GoogleCloudPlatform/terraformer
用于从现有基础设施生成 Terraform 文件的 CLI 工具(反向 Terraform)。
Is this possible to duplicate the vm-disk, instead of vm-image?
您可以从您的 VM 创建 instance template 并使用它来创建新的 VM: