关于 Terraform 内部工作原理的一个简单问题,我找不到相关文档。
假设如下:
depends_on
来声明B依赖于A,C依赖于Bterraform apply
将会发生以下两件事之一:
但这两者中的哪一个呢?两者似乎都很合理。
有趣的问题。我创建了一个小型 PoC 来看看会发生什么。
resource "null_resource" "b" {
triggers = {
description = "Resource B"
}
}
terraform apply
的输出:
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:
# null_resource.b will be created
+ resource "null_resource" "b" {
+ id = (known after apply)
+ triggers = {
+ "description" = "Resource B"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
null_resource.b: Creating...
null_resource.b: Creation complete after 0s [id=1123007074489954098]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
resource "null_resource" "b" {
triggers = {
description = "Resource B"
}
depends_on = [ null_resource.a ]
}
resource "null_resource" "a" {
triggers = {
description = "Resource A"
}
provisioner "local-exec" {
command = "Start-Sleep 60"
interpreter = ["PowerShell", "-Command"]
}
}
resource "null_resource" "c" {
triggers = {
description = "Resource C"
}
provisioner "local-exec" {
command = "Start-Sleep 60"
interpreter = ["PowerShell", "-Command"]
}
depends_on = [ null_resource.b ]
}
terraform apply
的输出:
null_resource.b: Refreshing state... [id=5916431791595061011]
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:
# null_resource.a will be created
+ resource "null_resource" "a" {
+ id = (known after apply)
+ triggers = {
+ "description" = "Resource A"
}
}
# null_resource.c will be created
+ resource "null_resource" "c" {
+ id = (known after apply)
+ triggers = {
+ "description" = "Resource C"
}
}
Plan: 2 to add, 0 to change, 0 to destroy.
null_resource.a: Creating...
null_resource.a: Provisioning with 'local-exec'...
null_resource.a (local-exec): Executing: ["PowerShell" "-Command" "Start-Sleep 60"]
null_resource.a: Still creating... [10s elapsed]
null_resource.a: Still creating... [20s elapsed]
null_resource.a: Still creating... [30s elapsed]
null_resource.a: Still creating... [40s elapsed]
null_resource.a: Still creating... [50s elapsed]
null_resource.a: Still creating... [1m0s elapsed]
null_resource.a: Creation complete after 1m1s [id=6341824471776991203]
null_resource.c: Creating...
null_resource.c: Provisioning with 'local-exec'...
null_resource.c (local-exec): Executing: ["PowerShell" "-Command" "Start-Sleep 60"]
null_resource.c: Still creating... [10s elapsed]
null_resource.c: Still creating... [20s elapsed]
null_resource.c: Still creating... [30s elapsed]
null_resource.c: Still creating... [40s elapsed]
null_resource.c: Still creating... [50s elapsed]
null_resource.c: Still creating... [1m0s elapsed]
null_resource.c: Creation complete after 1m0s [id=8942321372455105138]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
从日志中我们可以看到,资源
A
和 C
不是并行创建的。
所以正确答案似乎是:
由于 B 依赖于 A,而 C 依赖于 B,因此 Terraform 知道 C 传递地依赖于 A。它首先且仅在这之后创建 A 完成后继续创建 C。