使用depends_on时,terraform会等待整个依赖关系树还是仅等待显式依赖关系?

问题描述 投票:0回答:1

关于 Terraform 内部工作原理的一个简单问题,我找不到相关文档。

假设如下:

  • 我使用 Terraform 创建资源 B。
  • 然后我修改代码以声明资源 A 和 C。
    • 这些资源之间不存在固有的依赖关系
  • 我用
    depends_on
    来声明B依赖于A,C依赖于B
  • 我调用
    terraform apply

将会发生以下两件事之一:

  • 由于 B 已经存在,因此满足了 C 的依赖关系。 A 和 C 是并行创建的。
  • 由于 B 依赖于 A,而 C 依赖于 B,因此 Terraform 知道 C 传递地依赖于 A。它首先创建 A,并且仅在完成后才继续创建 C。

但这两者中的哪一个呢?两者似乎都很合理。

terraform
1个回答
0
投票

有趣的问题。我创建了一个小型 PoC 来看看会发生什么。

创建资源B

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.

添加资源 A 和 C

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。

© www.soinside.com 2019 - 2024. All rights reserved.