Terraform with GCP:将vm实例复制到不同的GCP项目 出身背景

Terraform with GCP:将vm实例复制到不同的GCP项目 出身背景,terraform,terraform-provider-gcp,Terraform,Terraform Provider Gcp,我有两个谷歌云项目:[project1]和[project2]。[project1]有一个名为my vm的虚拟机实例。我想将我的vm复制到[project2] 我做的步骤 因此,我创建了这个地形文件main.tf: 将其保存到新目录中。现在,运行以下命令: $ terraform init $ terraform import google_compute_instance.my-vm [project1]/us-central1-a/my-vm Error: resource address

我有两个谷歌云项目:[project1]和[project2]。[project1]有一个名为my vm的虚拟机实例。我想将我的vm复制到[project2]

我做的步骤 因此,我创建了这个地形文件main.tf:

将其保存到新目录中。现在,运行以下命令:

$ 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)
}
此时,我发现我错过了资源google\u compute\u实例my vm语句。所以,我把它添加到main.tf。现在看起来是这样的:

provider "google" {
  credentials = "${file("service-account.json")}"
  project     = "[project2]"
  region      = "us-central1"
}
resource "google_compute_instance" "my-vm" {

}
现在,我在agian上运行相同的terraform导入命令,并且获得了成功。已创建terraform.tfstate文件。但是,main.tf文件没有更改。我本来希望看到虚拟机导入的数据,但是我的虚拟机的资源google\u compute\u实例是空的。奇怪

现在,我正在运行指挥计划,得到了:

$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]中。是否可以复制虚拟机磁盘而不是虚拟机映像?
Terraform当前无法为您生成配置,导入仅将数据保存到状态文件

文档: Terraform导入的当前实现只能将资源导入状态。它不生成配置。Terraform的未来版本也将生成配置

因此,在运行terraform导入之前,需要手动写入资源的资源配置块,导入的对象将映射到该块

虽然这看起来很乏味,但它仍然为Terraform用户提供了导入现有资源的途径。Terraform的未来版本将完全生成配置,大大简化此过程

有一些第三方工具可以根据现有资源的配置生成:

CLI工具,用于从现有基础结构反向地形生成地形文件

是否可以复制虚拟机磁盘而不是虚拟机映像

您可以从VM创建并使用它创建新VM:


嘿,谢谢你的回答。你能分享一个剧本来实现这些目标吗?
$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.