如果 运行 "terraform apply" 两次会怎样? (地形)
What happens if running "terraform apply" twice? (Terraform)
如果运行“terraform apply”两次会怎样?
它会创建所有资源两次吗?
我假设当你在这里说“terraform deploy”时你的意思是运行宁 terraform apply
命令。
第一次 运行 terraform apply
针对全新配置时,Terraform 将建议创建与您在配置中声明的每个资源实例对应的新对象。如果您接受该计划并因此允许 Terraform 真正应用它,Terraform 将创建每个对象并在 Terraform state.
中记录有关它们的信息
如果您再次 运行 terraform apply
,Terraform 会将您的配置与状态进行比较,以查看是否存在任何差异。这一次,如果配置与状态中记录的现有对象不匹配,Terraform 将仅 提出更改建议。如果您接受 那个 计划,那么 Terraform 将执行它建议的每个操作,这些操作可以是不同操作类型的混合:更新、创建、销毁。
这意味着为了成功使用 Terraform,您需要确保在 Terraform 运行s 之间保持状态快照的安全。在没有任何特殊配置的情况下,Terraform 默认将状态保存在名为 terraform.tfstate
的本地文件中,但是当您在生产中使用 Terraform 时,您通常会使用 remote state,这是一种告诉 Terraform 的方式将状态快照存储在与您 运行ning Terraform 所在的计算机分开的远程数据存储中。通过将状态存储在所有同事都可以访问的位置,您可以一起协作。
如果您使用 Terraform Cloud,一种由 HashiCorp 提供的补充托管服务,您可以配置 Terraform 以将状态快照存储在 Terraform Cloud 本身中。 Terraform Cloud 还具有其他各种功能,例如在远程执行环境中 运行ning Terraform,这样使用该环境的每个人都可以确保 运行 Terraform 具有一组远程存储的一致环境变量。
如果您 运行 第一次使用 terraform apply
命令,它将创建 terraform plan
中的必要资源。
如果您 运行 第二次执行 terraform apply
命令,它将尝试检查该资源是否已经存在。如果找到则不会创建任何重复资源。
在运行第二次调用terraform apply
之前,如果你运行terraform plan
你会得到change/create/delete列表的列表。
2022 年 4 月更新:
- “terraform apply”的第一个运行创建(添加)资源。
- “terraform apply”的第二个或更高版本运行 创建(添加)、更新(更改)或删除(销毁)现有资源,如果有更改他们。另外,基本上当更改现有资源的可变值时,它的现有资源会被更新而不是删除然后创建,并且基本上当更改现有资源的不可变值时,其现有资源被删除然后创建而不是更新。
*可变值 是创建资源后可以更改的值。
*不可变值是创建资源后不能改变的值。
例如,我使用以下 Terraform 代码创建(添加)Cloud Storage 存储桶“kai_bucket”:
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”更改为”假:
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.
如果运行“terraform apply”两次会怎样? 它会创建所有资源两次吗?
我假设当你在这里说“terraform deploy”时你的意思是运行宁 terraform apply
命令。
第一次 运行 terraform apply
针对全新配置时,Terraform 将建议创建与您在配置中声明的每个资源实例对应的新对象。如果您接受该计划并因此允许 Terraform 真正应用它,Terraform 将创建每个对象并在 Terraform state.
如果您再次 运行 terraform apply
,Terraform 会将您的配置与状态进行比较,以查看是否存在任何差异。这一次,如果配置与状态中记录的现有对象不匹配,Terraform 将仅 提出更改建议。如果您接受 那个 计划,那么 Terraform 将执行它建议的每个操作,这些操作可以是不同操作类型的混合:更新、创建、销毁。
这意味着为了成功使用 Terraform,您需要确保在 Terraform 运行s 之间保持状态快照的安全。在没有任何特殊配置的情况下,Terraform 默认将状态保存在名为 terraform.tfstate
的本地文件中,但是当您在生产中使用 Terraform 时,您通常会使用 remote state,这是一种告诉 Terraform 的方式将状态快照存储在与您 运行ning Terraform 所在的计算机分开的远程数据存储中。通过将状态存储在所有同事都可以访问的位置,您可以一起协作。
如果您使用 Terraform Cloud,一种由 HashiCorp 提供的补充托管服务,您可以配置 Terraform 以将状态快照存储在 Terraform Cloud 本身中。 Terraform Cloud 还具有其他各种功能,例如在远程执行环境中 运行ning Terraform,这样使用该环境的每个人都可以确保 运行 Terraform 具有一组远程存储的一致环境变量。
如果您 运行 第一次使用 terraform apply
命令,它将创建 terraform plan
中的必要资源。
如果您 运行 第二次执行 terraform apply
命令,它将尝试检查该资源是否已经存在。如果找到则不会创建任何重复资源。
在运行第二次调用terraform apply
之前,如果你运行terraform plan
你会得到change/create/delete列表的列表。
2022 年 4 月更新:
- “terraform apply”的第一个运行创建(添加)资源。
- “terraform apply”的第二个或更高版本运行 创建(添加)、更新(更改)或删除(销毁)现有资源,如果有更改他们。另外,基本上当更改现有资源的可变值时,它的现有资源会被更新而不是删除然后创建,并且基本上当更改现有资源的不可变值时,其现有资源被删除然后创建而不是更新。
*可变值 是创建资源后可以更改的值。
*不可变值是创建资源后不能改变的值。
例如,我使用以下 Terraform 代码创建(添加)Cloud Storage 存储桶“kai_bucket”:
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”更改为”假:
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.