Terraform 模块的条件
condition on terraform module
有条件地尝试 运行 模块。
预期:运行 模块仅当 env 不等于 prd
module "database_diagnostic_eventhub_setting" {
count = var.env != "prd" ? 1 : 0 // run block if condition is satisfied
source = "git::https://git_url//modules/...."
target_ids = [
"${data.terraform_remote_state.database.outputs.server_id}"
]
environment = "${var.environment}-database-eventhub"
destination = data.azurerm_eventhub_namespace_authorization_rule.event_hub.id
eventhub_name = var.eventhub_name
logs = [
"PostgreSQLLogs",
"QueryStoreWaitStatistics"
]
}
错误:
The name "count" is reserved for use in a future version of Terraform.
您需要使用 Terraform v0.13 或更高版本才能在 module
块中使用 count
or for_each
。
如果您无法从 Terraform v0.12 升级,那么在支持模块重复之前,旧方法是向您的模块添加一个变量以指定对象计数:
variable "instance_count" {
type = number
}
...然后 在您的模块中 添加 count
到每个资源:
resource "example" "example" {
count = var.instance_count
}
但是,如果您现在能够升级到 Terraform v0.13,那么我强烈建议您这样做而不是使用上述解决方法,因为稍后升级到使用模块级别 count
,对象已经存在创建,是一个非常繁琐的过程,涉及该模块中每个资源的 运行 terraform state mv
。
有条件地尝试 运行 模块。
预期:运行 模块仅当 env 不等于 prd
module "database_diagnostic_eventhub_setting" {
count = var.env != "prd" ? 1 : 0 // run block if condition is satisfied
source = "git::https://git_url//modules/...."
target_ids = [
"${data.terraform_remote_state.database.outputs.server_id}"
]
environment = "${var.environment}-database-eventhub"
destination = data.azurerm_eventhub_namespace_authorization_rule.event_hub.id
eventhub_name = var.eventhub_name
logs = [
"PostgreSQLLogs",
"QueryStoreWaitStatistics"
]
}
错误:
The name "count" is reserved for use in a future version of Terraform.
您需要使用 Terraform v0.13 或更高版本才能在 module
块中使用 count
or for_each
。
如果您无法从 Terraform v0.12 升级,那么在支持模块重复之前,旧方法是向您的模块添加一个变量以指定对象计数:
variable "instance_count" {
type = number
}
...然后 在您的模块中 添加 count
到每个资源:
resource "example" "example" {
count = var.instance_count
}
但是,如果您现在能够升级到 Terraform v0.13,那么我强烈建议您这样做而不是使用上述解决方法,因为稍后升级到使用模块级别 count
,对象已经存在创建,是一个非常繁琐的过程,涉及该模块中每个资源的 运行 terraform state mv
。