【问题标题】:if condition in terraform in count如果条件在 terraform 中
【发布时间】:2021-11-05 20:21:16
【问题描述】:

我在 Azure cosmosdb 数据库中添加自动缩放设置,我的问题不是我们的所有数据库都需要自动缩放,只有选择的数据库需要自动缩放其余都是手动的。我将无法在同一资源中指定 autoscalse 块,因为这两者之间存在冲突。所以我想到了使用计数,但我将无法仅为其中一个数据库运行资源块。下面的例子

变量

variable "databases" {
  description = "The list of Cosmos DB SQL Databases."
  type = list(object({
    name       = string
    throughput = number
    autoscale          = bool
    max_throughput     = number
  }))
  default = [
      {
    name       = "testcoll1"
    throughput = 400
    autoscale          = false
    max_throughput     = 0
      },
       {
    name       = "testcoll2"
    throughput = 400
    autoscale          = true
    max_throughput     = 1000
      }
  ]
}

第一个我不需要自动缩放,下一个我需要。我的 main.tf 代码

resource "azurerm_cosmosdb_mongo_database" "database_manual" {
  count = length(var.databases)

  name                = var.databases[count.index].name
  resource_group_name = azurerm_cosmosdb_account.cosmosdb.resource_group_name
  account_name        = local.account_name
  throughput          = var.databases[count.index].throughput

}

resource "azurerm_cosmosdb_mongo_database" "database_autoscale" {
  count = length(var.databases)

  name                = var.databases[count.index].name
  resource_group_name = azurerm_cosmosdb_account.cosmosdb.resource_group_name
  account_name        = local.account_name

  autoscale_settings {
      max_throughput = var.databases[count.index].max_throughput
  }
}

首先我想运行两个块,一个有刻度,一个没有,但我无法继续,因为它需要计数

count = var.autoscale_required == true ?长度(数据库):0

在开始时,但在我的情况下,我只会在迭代时知道。我尝试在块内使用动态但出错了。

*更新 我已切换到 foreach 并能够运行该条件,但仍需要 2 个块 资源“azurerm_cosmosdb_mongo_database”“database_autoscale” 资源 "azurerm_cosmosdb_mongo_database" "database_manual"

resource "azurerm_cosmosdb_mongo_database" "database_autoscale" {
  for_each = { 
      for key, value in var.databases : key => value
      if value.autoscale_required == true }

  name                = each.value.name
  resource_group_name = azurerm_cosmosdb_account.cosmosdb.resource_group_name
  account_name        = local.account_name

  autoscale_settings {
      max_throughput = each.value.max_throughput
  }
}

【问题讨论】:

    标签: terraform azure-cosmosdb terraform-provider-azure azure-cosmosdb-mongoapi


    【解决方案1】:

    如果我理解正确,我认为您可以使用以下内容做您想做的事:

    resource "azurerm_cosmosdb_mongo_database" "database_autoscale" {
      count               = length(var.databases)
    
      name                = var.databases[count.index].name
      resource_group_name = azurerm_cosmosdb_account.cosmosdb.resource_group_name
      account_name        = local.account_name
      
      throughput          = var.databases[count.index].autoscale == false ? var.databases[count.index].throughput : null
    
      dynamic "autoscale_settings" {
          for_each = var.databases[count.index].autoscale == false ? [] : [1]
          content {
              max_throughput = var.databases[count.index].max_throughput
          }
      }
    }
    

    【讨论】:

    • 我运行它,它抛出错误,for_each = var.databases[count.index].autoscale_required == false ? {} : {1}│需要一个等号(“=”)来标记属性值的开始。我正在使用 terraformversion:0.15.4
    • @threeleggedrabbit 更新了答案。应该是固定的。
    猜你喜欢
    • 2019-04-11
    • 2019-09-22
    • 2020-08-20
    • 2020-04-26
    • 2022-01-11
    • 2021-12-29
    • 2021-11-16
    • 2018-12-31
    • 2019-05-19
    相关资源
    最近更新 更多