如果运行"地形应用"两次会发生什么?(地形)



如果运行"terraform apply"两次?它是否两次创建所有资源?

我假设当你说"地形部署"时;这里你的意思是运行terraform apply命令。

第一次针对一个全新的配置运行terraform apply时,Terraform将建议创建与您在配置中声明的每个资源实例相对应的新对象。如果您接受该计划,并允许Terraform真正应用它,Terraform将创建每个对象,并在Terraform状态中记录有关它们的信息。

如果您再次运行terraform apply, Terraform将比较您的配置与状态,看看是否有任何差异。这一次,如果配置与状态中记录的现有对象不匹配,Terraform将只建议更改。如果你接受这个计划,那么Terraform将采取它提出的每一个行动,这可以是不同行动类型的混合:更新,创建,破坏。

这意味着为了成功地使用Terraform,您需要确保在Terraform运行之间保持状态快照的安全。在没有任何特殊配置的情况下,Terraform默认将状态保存在一个名为terraform.tfstate的本地文件中,但是当您在生产中使用Terraform时,您通常会使用远程状态,这是一种告诉Terraform将状态快照存储在与运行Terraform的计算机分开的远程数据存储中。通过将状态存储在所有同事都可以访问的位置,您可以一起协作。

如果您使用HashiCorp提供的互补托管服务Terraform Cloud,您可以配置Terraform以将状态快照存储在Terraform Cloud中。Terraform Cloud还具有各种其他功能,例如在远程执行环境中运行Terraform,以便使用该环境的每个人都可以确保使用远程存储的一致环境变量集运行Terraform。

如果您第一次运行terraform apply命令,它将创建terraform plan中所需的资源。

如果您第二次运行terraform apply命令,它将尝试检查该资源是否已经存在。如果找到,则不会创建任何重复资源。

在第二次运行terraform apply之前,如果您运行terraform plan,您将得到change/create/delete list的列表。

2022年4月更新:

  • 第一次运行"terraform apply">创建(添加)资源。
  • 第二次或以后运行的"地形应用">创建(添加)、更新(更改)或删除(销毁)已存在的资源(如果对它们有更改)。另外,基本上当修改时,现有资源的可变值,它的现有资源是更新的,而不是删除,然后创建,基本上当改变时,现有资源的不可变值,则删除现有资源,然后创建资源,而不是更新资源。

*一个可变值创建资源后可更改的值。

*一个不可变的值创建资源后不能更改的值。

例如,我创建(添加)云存储桶& kai_bucket">使用下面的terrform代码:

resource "google_storage_bucket" "bucket" {
name                        = "kai_bucket"
location                    = "ASIA-NORTHEAST1"
force_destroy               = true
uniform_bucket_level_access = true
}

那么,执行下面命令的第一次运行:

terraform apply -auto-approve

然后,一个资源' kai_bucket ';如下所示创建(添加):

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# google_storage_bucket.bucket will be created
+ resource "google_storage_bucket" "bucket" {
+ force_destroy               = true
+ id                          = (known after apply)
+ location                    = "ASIA-NORTHEAST1"
+ name                        = "kai_bucket"
+ project                     = (known after apply)
+ self_link                   = (known after apply)
+ storage_class               = "STANDARD"
+ uniform_bucket_level_access = true
+ url                         = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
google_storage_bucket.bucket: Creating...
google_storage_bucket.bucket: Creation complete after 1s [id=kai_bucket]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

现在,我改变可变值"uniform_bucket_level_access">"true">"false">:

resource "google_storage_bucket" "bucket" {
name                        = "kai_bucket"
location                    = "ASIA-NORTHEAST1"
force_destroy               = true
uniform_bucket_level_access = false # Here
}

然后,执行下面命令的第二次运行:

terraform apply -auto-approve

,"uniform_bucket_level_access""true">更新(更改)到"false">如下所示:

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# google_storage_bucket.bucket will be updated in-place
~ resource "google_storage_bucket" "bucket" {
id                          = "kai_bucket"
name                        = "kai_bucket"
~ uniform_bucket_level_access = true -> false
# (9 unchanged attributes hidden)
}
Plan: 0 to add, 1 to change, 0 to destroy.
google_storage_bucket.bucket: Modifying... [id=kai_bucket]
google_storage_bucket.bucket: Modifications complete after 1s [id=kai_bucket]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

现在,我改变不可变值"location">"ASIA-NORTHEAST1">"US-EAST1">:

resource "google_storage_bucket" "bucket" {
name                        = "kai_bucket"
location                    = "US-EAST1" # Here
force_destroy               = true
uniform_bucket_level_access = false
}

然后,执行下面命令的第三次运行:

terraform apply -auto-approve

然后,一个资源' kai_bucket ';"ASIA-NORTHEAST1">被删除(销毁),那么一个资源kai_bucket"US-EAST1">如下所示创建(添加):

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
# google_storage_bucket.bucket must be replaced
-/+ resource "google_storage_bucket" "bucket" {
- default_event_based_hold    = false -> null
~ id                          = "kai_bucket" -> (known after apply)
- labels                      = {} -> null
~ location                    = "ASIA-NORTHEAST1" -> "US-EAST1" # forces replacement
name                        = "kai_bucket"
~ project                     = "myproject-272234" -> (known after apply)
- requester_pays              = false -> null
~ self_link                   = "https://www.googleapis.com/storage/v1/b/kai_bucket" -> (known after apply)
~ url                         = "gs://kai_bucket" -> (known after apply)
# (3 unchanged attributes hidden)
}
Plan: 1 to add, 0 to change, 1 to destroy.
google_storage_bucket.bucket: Destroying... [id=kai_bucket]
google_storage_bucket.bucket: Destruction complete after 1s
google_storage_bucket.bucket: Creating...
google_storage_bucket.bucket: Creation complete after 1s [id=kai_bucket]
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

相关内容

  • 没有找到相关文章

最新更新