简体   繁体   English

如果资源是使用 count 或 for_each 创建的,如何在 terraform 中使用 depends_on

[英]How to use depends_on in terraform if resources are created using count or for_each

Hello How to use depends_on in the cases of count or for_each.你好如何在count或for_each的情况下使用depends_on。

The basic syntax of depends_on assumes depends_on 的基本语法假定

resource "azurerm_storage_container" "cont1"{
.....
.....
depends_on = [
    azurerm_storage_account.storageAccount
 ]
}
 
resource "azurerm_storage_account" "storageAccount" {
  count                   = 3
  name                    = "storageAccountName-${count.index}"
  resource_group_name     = "rg-demo"
  location                = "centralus"
  account_tier            = "Standard"
  account_replication_type= "LRS"
  # all other relevant attributes
}

So here its azurerm_storage_account.storageAccount if only one such resource were to be created but if we have many azurerm_storage_account created as for_each or count was used in them then what to write in place of storageAccount?所以在这里它的 azurerm_storage_account.storageAccount 如果只创建一个这样的资源,但是如果我们有许多 azurerm_storage_account 创建为 for_each 或在其中使用了计数那么写什么来代替 storageAccount? Like suppose we have to have a dependency of storageAccountName-2 on the container then how will we write the depends on?假设我们必须在容器上有 storageAccountName-2 的依赖项,那么我们将如何编写依赖项?

As was mentioned in a comment above, you can add an index to the storageAccount resource reference:正如上面评论中提到的,您可以向 storageAccount 资源引用添加索引:

resource "azurerm_storage_account" "test_storage_account" {
  count                    = 3
  name                     = "teststorageaccount${count.index}"
  ...
}

resource "azurerm_storage_container" "container" {
  depends_on = [
    azurerm_storage_account.test_storage_account[0]
  ]
  name                  = "testcontainer"
  ...
}

You can also omit the index and have the container(s) depend on the entire storage account resource.您还可以省略索引并让容器依赖于整个存储帐户资源。 Both worked in my testing.两者都在我的测试中起作用。

Additionally, while implicit dependency would normally allow you to not have a "depends_on" block in the container resource (if you had a single storage account), the implicit dependency does not appear to work when you have multiple storage accounts created with a "for_each" that are later referenced in a storage container resource.此外,虽然隐式依赖通常允许您在容器资源中没有“depends_on”块(如果您有一个存储帐户),但当您有多个使用“for_each”创建的存储帐户时,隐式依赖似乎不起作用" 稍后在存储容器资源中引用。

Dependencies in Terraform are only between static blocks, not between individual instances declared by those blocks. Terraform 中的依赖关系仅存在于静态块之间,而不存在于这些块声明的各个实例之间。 This is because the count and for_each expressions themselves can have dependencies, and so Terraform must build the dependency graph before calculating which instances will exist for each resource or module.这是因为countfor_each表达式本身可以具有依赖关系,因此 Terraform 必须在计算每个资源或模块将存在哪些实例之前构建依赖关系图。

Therefore using count and for_each for a resource changes nothing about how you would create dependencies for the resource.因此,对资源使用countfor_each不会改变您为资源创建依赖项的方式。

As usual, the most common way to create a dependency is to just refer to the resource in at least one of the arguments of another resource.与往常一样,创建依赖项的最常见方法是仅在另一个资源的至少一个参数中引用该资源。 That creates a dependency automatically, so you don't need depends_on at all:这会自动创建依赖关系,因此您根本不需要depends_on

resource "azurerm_storage_account" "example" {
  count = 3

  # ...
}

resource "azurerm_storage_container" "example" {
  # the azurerm_storage_account.example part of the following
  # expression creates a dependency, regardless of what
  # else this expression does.
  count = length(azurerm_storage_account.example)

  # ...
}

The depends_on argument is needed only for situations where a remote API design causes there to be "hidden dependencies" that Terraform can't discover automatically by noticing your references. depends_on参数仅在远程 API 设计导致存在 Terraform 无法通过注意您的引用自动发现的“隐藏依赖项”的情况下才需要。

One way that could be true with the resource types you showed here is if you are writing a shared module that declares both of these resources and then exports the storage account IDs as an output value.对于您在此处显示的资源类型,一种可能适用的方法是,如果您正在编写一个声明这两种资源的共享模块,然后将存储帐户 ID 导出为输出值。 In that case you may wish to tell Terraform that the output value also depends on the storage container, which would represent that the storage account isn't actually ready to use until the storage container has been created:在这种情况下,您可能希望告诉 Terraform 输出值还取决于存储容器,这表示在创建存储容器之前存储帐户实际上尚未准备好使用:

output "storage_account_ids" {
  value = toset(azurerm_storage_account.example[*].id)

  # The storage accounts are not ready to use
  # until their containers are also ready.
  depends_on = [azurerm_storage_container.example]
}

Notice that depends_on refers to just azurerm_storage_container.example , because Terraform only tracks dependencies on whole resources.请注意depends_on仅指azurerm_storage_container.example ,因为 Terraform 仅跟踪对整个资源的依赖性。

Although it is syntactically valid to write an instance ID here -- for example, you could declare a dependency on azurerm_storage_container.example[0] and Terraform would accept it -- Terraform will just ignore the [0] part and create a dependency on the whole resource anyway.尽管在此处写入实例 ID 在语法上是有效的——例如,您可以声明对azurerm_storage_container.example[0]的依赖关系,Terraform 会接受它——Terraform 只会忽略[0]部分并创建对无论如何,整个资源。 The [0] suffix makes absolutely no difference to Terraform's behavior. [0]后缀对 Terraform 的行为完全没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM