Is it expected that terraform does not wait for the module to be fully applied when that module is a dependency of the resource?

huangapple go评论54阅读模式
英文:

Is it expected that terraform does not wait for the module to be fully applied when that module is a dependency of the resource?

问题

根据您提供的信息,模块 aks_cluster 和模块 keyvault 的资源似乎在同时创建,而不是按照您的期望的依赖关系。这可能是因为 Terraform 模块内的资源定义本身并不明确地表示模块之间的依赖关系。资源之间的依赖通常是根据它们之间的引用关系来确定的。

在 Terraform 中,如果一个资源引用了另一个资源的属性,Terraform 会自动建立依赖关系,确保被引用的资源在引用它的资源之前创建。这种依赖关系是自动推断的,而不需要显式声明。

所以,尽管您在代码中引用了 module.keyvault.keyvault_id,但这并不一定意味着 aks_cluster 模块会等待 keyvault 模块的资源创建完成。依赖关系通常基于资源引用来自动生成,而不是模块之间的名称。

如果您需要确保 aks_cluster 等待 keyvault 资源创建完成后才创建自己的资源,您可以使用 depends_on 参数来显式定义依赖关系。例如:

module "aks_cluster" {
  source         = "git::ssh://git@ssh.dev.azure.com/..."
  key_vault_id   = module.keyvault.keyvault_id
  depends_on     = [module.keyvault]
  # ...
}

通过将 aks_cluster 模块的 depends_on 参数设置为 [module.keyvault],您可以确保 aks_cluster 等待 keyvault 模块的资源创建完成后再开始创建自己的资源。

英文:

Consider the following terraform snippet:

module "aks_cluster" {
  source                     = "git::ssh://git@ssh.dev.azure.com/..."
  key_vault_id               = module.keyvault.keyvault_id
  ...
}

From this I conclude that the module aks_cluster depends on the module keyvault.

However, when running apply I can see the following:

module.aks_cluster.module.aks_cluster.tls_private_key.aks_ssh_key["aks1"]: Creating...
module.aks_cluster.module.aks_cluster.tls_private_key.aks_ssh_key["aks1"]: Creation complete after 0s [id=fe71f0acb5c598e1d1cc1dbef7e732ef35c9648c]
module.aks_cluster.module.aks_cluster.azurerm_user_assigned_identity.private_aks["aks1"]: Creating...
module.aks_cluster.module.app_insights.azurerm_application_insights.app_insights["ai01"]: Creating...
module.aks_cluster.azurerm_resource_group.onboarding: Creating...
module.aks_cluster.module.keyvault.azurerm_key_vault.keyvault: Creating...
module.aks_cluster.module.lga.azurerm_log_analytics_workspace.lga: Creating...
module.aks_cluster.azurerm_resource_group.onboarding: Creation complete after 1s [id=...]
module.aks_cluster.module.aks_cluster.azurerm_user_assigned_identity.private_aks["aks1"]: Creation complete after 3s [id=...]
module.aks_cluster.module.aks_cluster.azurerm_role_assignment.private_aks_dns_contributor["aks1"]: Creating...
module.aks_cluster.module.aks_cluster.azurerm_role_assignment.acr_pull["aks1"]: Creating...
module.aks_cluster.module.aks_cluster.azurerm_role_assignment.private_aks_network_contributor["aks1"]: Creating...
module.aks_cluster.module.app_insights.azurerm_application_insights.app_insights["ai01"]: Still creating... [10s elapsed]
module.aks_cluster.module.keyvault.azurerm_key_vault.keyvault: Still creating... [10s elapsed]
...

It looks like the aks_cluster module resources are being created at the same time as those of the keyvault.

I am pretty sure when resource X references a property on a resource Y it means X depends on Y, but it does not seem to work this way for modules.

What am I missing?

答案1

得分: 0

aks_cluster模块中,只有引用keyvault_id的资源将依赖于用于创建模块输出的keyvault中的特定资源。

如果您想强制模块之间的依赖关系,您可以使用depends_on

module "aks_cluster" {
  source                     = "git::ssh://git@ssh.dev.azure.com/..."
  key_vault_id               = module.keyvault.keyvault_id
  ...

  depends_on = [
    module.keyvault
  ]
}
英文:

Within the aks_cluster module, only the resource(s) that references the keyvault_id will have a dependency on the spesific resource(s) in the keyvault that is used to create the module output.

If you want to force a dependency on the module you can use depends_on:

module "aks_cluster" {
  source                     = "git::ssh://git@ssh.dev.azure.com/..."
  key_vault_id               = module.keyvault.keyvault_id
  ...

  depends_on = [
    module.keyvault
  ]
}

huangapple
  • 本文由 发表于 2023年2月24日 10:04:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定