Terraform接受来自命令行的输入,但会破坏上次添加的资源

时间:2019-01-23 07:09:19

标签: google-cloud-platform terraform

我正在尝试运行一个Terraform脚本,该脚本接受VM大小,网络详细信息和映像详细信息的参数,以从用户启动新VM。如果我第二次运行脚本,它将破坏第一次创建的资源,这是我不想要的。保留第一次创建的资源的可能解决方案是什么?

只看一眼我正在经历的事情:

resource "google_compute_network" "custom" {
  name = "test-ntwrk"
  auto_create_subnetworks = false,
}

resource "google_compute_subnetwork" "custom-subnet" {
  count = "${var.count}"
  name = "${var.name[count.index]}",
  ip_cidr_range = "${var.cidrs[count.index]}",
  region = "asia-south1",
  network = "${google_compute_network.custom.self_link}"
}

这将使子网具有给定的范围,可以说[“ 10.0.1.0/24”,“ 10.0.2.0/24”]。

当我重新运行同一脚本以将另外两个具有CIDR范围[“ 10.0.3.0/24,10.0.4.0/24]的子网添加到已创建的脚本中时,terraform计划向我显示了如下内容:

Google_compute_subnetwork.custom-subnet[0] (new resource required)
      id:                        "asia-south1/subnet-tf-0" => <computed> (forces new resource)
      creation_timestamp:        "2019-02-06T11:34:34.371-08:00" => <computed>
      gateway_address:           "10.0.1.1" => <computed>
      ip_cidr_range:             "10.0.1.0/24" => "10.3.0.0/24" (forces new resource)
      name:                      "subnet-tf-0" => "subnet-tf-0"
      network:                   "https://www.googleapis.com/compute/v1/projects/cio-demopoc-project-228209/global/networks/test-ntwrk" => "https://www.googleapis.com/compute/v1/projects/cio-demopoc-project-228209/global/networks/test-ntwrk"
      project:                   "xxxxxxxxxx" => <computed>
      region:                    "asia-south1" => "asia-south1"
      secondary_ip_range.#:      "0" => <computed>
      self_link:                 "https://www.googleapis.com/compute/v1/projects/cio-demopoc-project-228209/regions/asia-south1/subnetworks/subnet-tf-0" => <computed>

-/+ google_compute_subnetwork.custom-subnet[1] (new resource required)
      id:                        "asia-south1/subnet-tf-1" => <computed> (forces new resource)
      creation_timestamp:        "2019-02-06T22:31:39.600-08:00" => <computed>
      fingerprint:               "rt9soQpV_Nw=" => <computed>
      gateway_address:           "10.0.2.1" => <computed>
      ip_cidr_range:             "10.0.2.0/24" => "10.0.4.0/24" (forces new resource)
      name:                      "subnet-tf-1" => "subnet-tf-1"
      network:                   "https://www.googleapis.com/compute/v1/projects/cio-demopoc-project-228209/global/networks/test-ntwrk" => "https://www.googleapis.com/compute/v1/projects/cio-demopoc-project-228209/global/networks/test-ntwrk"
      project:                   "xxxxxxxxxx" => <computed>
      region:                    "asia-south1" => "asia-south1"
      secondary_ip_range.#:      "0" => <computed>
      self_link:                 "https://www.googleapis.com/compute/v1/project

第二次运行此脚本,强制创建资源并替换我不希望的现有资源。

预期的输出:应在VPC内创建所有4个子网。

1 个答案:

答案 0 :(得分:0)

我需要查看Terraform Plan的输出,以确切地了解为什么 资源被重新创建。有问题的更改将标记为“(强制使用新资源)”

这可能是Terraform无法预先查看其输出将是计算/生成的值/参数的结果。例如,将随机值分配给变量,或在资源中使用“ depends_on” 变量。后者可以触发Terraform来计算值 apply-time 而不是 plan-time ,在这种情况下,它将始终将依赖资源标记为必须重新创建。

我还注意到模板文件可能会导致这种行为,因为行尾以及源文件中没有的东西,Terraform一直在例如之间进行更改。先前应用的用户数据和磁盘上的用户数据源文件。

请运行地形计划,并检查哪个变量会强制使用新资源。一一遍历并评估正在发生的事情,将使您确定Terraform重新创建资源是否合乎逻辑。

如果您需要进一步的帮助,请在此处发布输出,最好是在Terraform代码中发布。

更新: 查看Terraform Plan输出,看来这两个新的CIDR块已分配给您的现有资源。您确定将两个CIDR块添加到之前的块中,从而导致一个数组中总共包含4个CIDR块吗?

Terraform与其说是 state ,不如说是 script (正如您所指)。如果您已经应用了之前的两个子网,则更改Terraform代码中的值将导致修改这两个子网,而不是创建两个其他子网。重新运行相同的Terraform代码应导致完全零的更改,一次又一次地添加相同的资源。