我是terraform的新手,我在模块结构上创建了一个定制的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"
]发布于 2019-10-07 21:04:17
在大多数情况下,所需的依赖关系只是由于引用的结果而自动发生。如果一个资源的配置直接或间接地引用另一个资源,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 "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的输出,我建议您更喜欢这种方法,因为它会导致更易于跟踪的配置。但是,在很少的情况下,资源之间的关系不能建模为数据流,您也可以使用输出和变量在模块之间传播显式依赖关系。
发布于 2020-08-01 13:36:46
现在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]
}发布于 2022-09-07 21:16:55
在资源级别使用depends_on与在模块间级别使用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]
}我用简单的模块直接创建了依赖关系作为最简单的,没有重新配置复杂的关系。
https://stackoverflow.com/questions/58275233
复制相似问题