1

如何在 variables.tf 中使用局部值?

我需要为两个netapp 卷指标警报动态分配阈值,但出现错误:Error: Variables not allowed. 每个 NetApp 卷都有不同的存储配额(以 GB 为单位),这就是它需要动态的原因。

NetApp 卷代码:

主文件

locals {
  iops_80 = format("%.0f", (var.storage_quota_in_gb * 1.6))
}

resource "azurerm_netapp_volume" "netapp_volume" {
  name                = var.netapp_vol_name
  resource_group_name = var.resource_group_name
  location            = var.location
  account_name        = var.account_name
  pool_name           = var.pool_name
  volume_path         = var.volume_path
  service_level       = var.service_level
  subnet_id           = var.subnet_id
  storage_quota_in_gb = var.storage_quota_in_gb
  protocols           = var.protocols

  dynamic "export_policy_rule" {
    for_each = var.export_policy_rules
    content {
      rule_index        = export_policy_rule.value.rule_index
      allowed_clients   = export_policy_rule.value.allowed_clients
      protocols_enabled = export_policy_rule.value.protocols_enabled
      unix_read_only    = export_policy_rule.value.unix_read_only
      unix_read_write   = export_policy_rule.value.unix_read_write
    }
  }

  tags = var.tags
}

resource "azurerm_monitor_metric_alert" "alert" {
  depends_on = [azurerm_netapp_volume.netapp_volume]

  count               = length(var.criteria)
  name                = "HPG-ALRT-${var.netapp_vol_name}-001-${element(keys(var.criteria), count.index)}"
  resource_group_name = var.resource_group_name
  scopes              = [azurerm_netapp_volume.netapp_volume.id]
  enabled             = var.enabled
  auto_mitigate       = var.auto_mitigate
  description         = lookup(var.criteria, element(keys(var.criteria), count.index), null)["description"]
  frequency           = var.frequency
  severity            = lookup(var.criteria, element(keys(var.criteria), count.index), null)["severity"]
  window_size         = var.window_size

  criteria {
    metric_namespace = lookup(var.criteria, element(keys(var.criteria), count.index), null)["metric_namespace"]
    metric_name      = lookup(var.criteria, element(keys(var.criteria), count.index), null)["metric_name"]
    aggregation      = lookup(var.criteria, element(keys(var.criteria), count.index), null)["aggregation"]
    operator         = lookup(var.criteria, element(keys(var.criteria), count.index), null)["operator"]
    threshold        = lookup(var.criteria, element(keys(var.criteria), count.index), null)["threshold"]
  }

  action {
    action_group_id = var.action_group_id
  }
}

变量.tf

    variable "criteria" {
  type = map
  default = {
    "ReadLATENCY5" = {
      metric_namespace = "Microsoft.NetApp/netAppAccounts/capacityPools/volumes"
      metric_name      = "AverageReadLatency"
      aggregation      = "Average"
      operator         = "GreaterThan"
      threshold        = 5
      description      = "NetApp: Volume Read Latency over 5ms"
      severity         = 2
    },
    "ReadIOPS80" = {
      metric_namespace = "Microsoft.NetApp/netAppAccounts/capacityPools/volumes"
      metric_name      = "ReadIops"
      aggregation      = "Average"
      operator         = "GreaterThan"
      threshold        = local.iops_80
      description      = "NetApp: Volume Read IOPS over TBD"
      severity         = 2
    },
    "WriteIops80" = {
      metric_namespace = "Microsoft.NetApp/netAppAccounts/capacityPools/volumes"
      metric_name      = "WriteIops"
      aggregation      = "Average"
      operator         = "GreaterThan"
      threshold        = local.iops_80
      description      = "NetApp: Volume Write IOPS over TBD"
      severity         = 2
    },
  }
}

一种方法是执行另一个标准映射以仅定义具有 iops_80 值的警报并在 main.tf 中分配它,但还有其他方法吗?

4

1 回答 1

0

看来您不能在变量文件中使用本地值。您可以做的是在本地使用变量,并在资源块中使用本地值和变量。您还可以在另一个本地使用本地值。

所以我认为你需要使用变量来设置输入并且事情会改变。并在本地引用变量或在另一个本地引用本地。例如,也许您可​​以使用本地来设置criteria而不是使用变量。

于 2020-12-21T07:29:33.670 回答