簡體   English   中英

Terraform 依賴於模塊

[英]Terraform depends_on with modules

我是 terraform 的新手,我創建了一個關於模塊結構的自定義 azure 策略。 每個策略代表一個自定義模塊。 我創建的模塊之一是為創建的任何新 azure 資源啟用診斷日志。 但是,我需要一個存儲帳戶。 (在啟用診斷設置之前,我如何實現“depends_on”?或任何其他方法?我想先創建存儲帳戶,然后創建診斷設置模塊。在main.tf (調用所有其他模塊)或內部資源(模塊)?

謝謝您的幫助:! :)

下面的代碼代表 main.tf 文件:

//calling the create storage account name

module "createstorageaccount" {

source = "./modules/module_create_storage_account"
    depends_on = [
    "module_enable_diagnostics_logs"
  ]

}

這個代表創建存儲帳戶模塊

resource "azurerm_resource_group" "management" {


  name     = "management-rg"
  location = "West Europe"
}

resource "azurerm_storage_account" "test" {
  name                     = "diagnostics${azurerm_resource_group.management.name}"
  resource_group_name      = "${azurerm_resource_group.management.name}"
  location                 = "${azurerm_resource_group.management.location}"
  account_tier             = "Standard"
  account_replication_type = "LRS"

  tags = {
    environment = "diagnostics"
  }
}

    depends_on = [
    "module_enable_diagnostics_logs"
  ]

在大多數情況下,必要的依賴關系只是由於您的引用而自動發生。 如果一個資源的配置直接或間接引用另一個資源,Terraform 會自動推斷它們之間的依賴關系,而不需要顯式的depends_on

這是有效的,因為模塊變量和輸出也是依賴圖中的節點:如果子模塊資源引用var.foo那么它間接依賴於該變量的值所依賴的任何東西。

對於自動依賴檢測不足的罕見情況,您仍然可以利用模塊變量和輸出是依賴關系圖中的節點這一事實來創建間接顯式依賴關系,如下所示:

variable "storage_account_depends_on" {
  # the value doesn't matter; we're just using this variable
  # to propagate dependencies.
  type    = any
  default = []
}

resource "azurerm_storage_account" "test" {
  name                     = "diagnostics${azurerm_resource_group.management.name}"
  resource_group_name      = "${azurerm_resource_group.management.name}"
  location                 = "${azurerm_resource_group.management.location}"
  account_tier             = "Standard"
  account_replication_type = "LRS"

  tags = {
    environment = "diagnostics"
  }

  # This resource depends on whatever the variable
  # depends on, indirectly. This is the same
  # as using var.storage_account_depends_on in
  # an expression above, but for situations where
  # we don't actually need the value.
  depends_on = [var.storage_account_depends_on]
}

調用此模塊時,可以將storage_account_depends_on設置為任何表達式,其中包含要確保在存儲帳戶之前創建的對象:

module "diagnostic_logs" {
  source = "./modules/diagnostic_logs"
}

module "storage_account" {
  source = "./modules/storage_account"

  storage_account_depends_on = [module.diagnostic_logs.logging]
}

然后在您的diagnostic_logs模塊中,您可以為logging output 配置間接依賴關系,以完成模塊之間的依賴關系鏈接:

output "logging" {
  # Again, the value is not important because we're just
  # using this for its dependencies.
  value = {}

  # Anything that refers to this output must wait until
  # the actions for azurerm_monitor_diagnostic_setting.example
  # to have completed first.
  depends_on = [azurerm_monitor_diagnostic_setting.example]
}

如果您的關系可以通過傳遞實際來表達,例如通過使用包含 id 的 output,我建議您更喜歡這種方法,因為它會導致更容易遵循的配置。 但是在資源之間存在無法建模為數據流的關系的極少數情況下,您也可以使用輸出和變量來傳播模塊之間的顯式依賴關系。

Terraform 13 現在支持模塊依賴項,目前處於發布候選階段。

resource "aws_iam_policy_attachment" "example" {
  name       = "example"
  roles      = [aws_iam_role.example.name]
  policy_arn = aws_iam_policy.example.arn
}

module "uses-role" {
  # ...

  depends_on = [aws_iam_policy_attachment.example]
}

在資源級別使用depends_on與在模塊間級別使用depends_on不同,我發現在模塊級別使用非常簡單的方法

module "eks" {
source = "../modules/eks"
vpc_id = module.vpc.vpc_id
vpc_cidr = [module.vpc.vpc_cidr_block]
public_subnets = flatten([module.vpc.public_subnets])
private_subnets_id = flatten([module.vpc.private_subnets])
depends_on = [module.vpc] 
}

我直接使用簡單的模塊創建依賴關系,因為最簡單,不需要復雜的關系

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM