在计划阶段,“创建”和“破坏”的顺序是什么?

时间:2019-07-16 16:19:08

标签: terraform terraform-provider-gcp

只需运行terraform plan,它就会吐出需要针对GCP API运行的配置。根据计划,它必须销毁某些实体并重新创建它们。这些模块互不依赖。下面的示例输出。

只是想知道创建(在这种情况下更像是“覆盖”)是在销毁之前发生还是反之亦然,或者它是完全随机的吗?


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
  - destroy

Terraform will perform the following actions:

  + module.project_01.google_project_service.project_service[0]
      id:                 <computed>
      disable_on_destroy: "true"
      project:            "proj-**"
      service:            "compute.googleapis.com"

  + module.project_01.google_project_service.project_service[1]
      id:                 <computed>
      disable_on_destroy: "true"
      project:            "proj-**"
      service:            "cloudresourcemanager.googleapis.com"

  + module.project_01.google_project_service.project_service[2]
      id:                 <computed>
      disable_on_destroy: "true"
      project:            "proj-**"
      service:            "cloudbilling.googleapis.com"

  + module.project_01.google_project_service.project_service[3]
      id:                 <computed>
      disable_on_destroy: "true"
      project:            "proj-**"
      service:            "servicenetworking.googleapis.com"

  - module.project_01.google_project_service.project_service_billing

  - module.project_01.google_project_service.project_service_compute

  - module.project_01.google_project_service.project_service_crm


Plan: 4 to add, 0 to change, 3 to destroy.

------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:1)

在无法更新资源的change+-情况下,Terraform将在创建新资源之前自动销毁资源。引用文档:

  

默认情况下,当Terraform必须更改由于远程API限制而无法就地更新的资源参数时,Terraform会销毁现有对象,然后使用新配置的参数创建新的替换对象。 / p>

从本质上讲,Terraform资源将CRUD,并为四个操作中的每个操作编写代码。在这种change情况下,将尝试进行更新,但是由于API的限制,有时无法进行更新。在这种情况下,destroy之后是create

但是,当没有更新代码和/或API端点可用时,可以在资源中的create_before_destroy块中lifecycle进行操作:

lifecycle {
  create_before_destroy = true
}

您可以在lifecycle block documentation中了解有关此内容的更多信息。