【问题标题】:condition on terraform moduleterraform 模块的条件
【发布时间】:2021-08-17 05:12:40
【问题描述】:

尝试有条件地运行模块。

期望:仅当 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 terraform-provider-azure


【解决方案1】:

您需要使用 Terraform v0.13 或更高版本才能在 module 块内使用 countfor_each

如果您无法从 Terraform v0.12 升级,那么在支持模块重复之前,旧方法是向您的模块添加一个变量以指定对象计数:

variable "instance_count" {
  type = number
}

...然后在您的模块中count 添加到每个资源中:

resource "example" "example" {
  count = var.instance_count
}

但是,如果您现在能够升级到 Terraform v0.13,那么我强烈建议您这样做,而不是使用上述解决方法,因为稍后升级到使用模块级 count,并且已经创建了对象,非常一个繁琐的过程,涉及为该模块中的每个资源运行 terraform state mv

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 1970-01-01
    • 2021-09-06
    • 2023-01-30
    • 2022-07-30
    • 1970-01-01
    • 2021-05-15
    • 2019-07-02
    • 1970-01-01
    相关资源
    最近更新 更多