Terraform 在variables.tf中使用局部值
如何在variables.tf中使用局部值 我需要为netapp volume metric alert的两个动态分配阈值,我得到一个错误:Terraform 在variables.tf中使用局部值,terraform,terraform-provider-azure,hcl,Terraform,Terraform Provider Azure,Hcl,如何在variables.tf中使用局部值 我需要为netapp volume metric alert的两个动态分配阈值,我得到一个错误:error:Variables not allowed。每个NetApp卷都有不同的存储配额(GB),这就是为什么它需要是动态的 NetApp卷代码: main.tf locals { iops_80 = format("%.0f", (var.storage_quota_in_gb * 1.6)) } resource "
error:Variables not allowed
。每个NetApp卷都有不同的存储配额(GB),这就是为什么它需要是动态的
NetApp卷代码:
main.tf
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
}
}
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
},
}
}
变量。tf
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
}
}
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中,但是否有其他方法执行此操作?似乎无法使用变量文件中的本地值。您可以做的是在本地使用变量,并在资源块中使用本地值和变量。您还可以在另一个本地文件中使用本地值
所以我认为你需要使用变量来设置输入,事情就会改变。并在局部中引用变量,或在另一个局部中引用局部变量。例如,您可以使用本地设置
标准,而不是使用变量。似乎无法使用变量文件中的本地值。您可以做的是在本地使用变量,并在资源块中使用本地值和变量。您还可以在另一个本地文件中使用本地值
所以我认为你需要使用变量来设置输入,事情就会改变。并在局部中引用变量,或在另一个局部中引用局部变量。例如,您可以使用local来设置标准,而不是使用变量。我为临时解决方案添加了一个条件语句threshold=(element(key(var.criteria)、count.index)=“ReadIOPS80”| | element(key(var.criteria)、count.index)=“WriteIOPS80”)?(查找(变量标准,元素(键(变量标准),计数指数),null)[“阈值”]+local.iops_80):查找(变量标准,元素(键(变量标准),计数指数),null)[“阈值”]
您是否曾使用terraform.tfvars
或TF_var_标准设置?如果不是,您也可以将其设置为本地。您的意思是要在变量中使用本地。tf吗?@ydaetskcoR它是一个模块,我在那里不使用terraform.tfvars。@charlessu是的,没错。我为临时解决方案添加了一个条件语句threshold=(element(key(var.criteria),count.index)=“ReadIOPS80”| |元素(键(变量标准)、计数索引)=“WriteIOPS80”)?(查找(变量标准,元素(键(变量标准),计数指数),null)[“阈值”]+local.iops_80):查找(变量标准,元素(键(变量标准),计数指数),null)[“阈值”]
您是否曾使用terraform.tfvars
或TF_var_标准设置?如果不是,您也可以将其设置为局部变量。您的意思是要在variables.tf中使用局部变量吗?@ydaetskcoR它是一个模块,我在那里不使用terraform.tfvars。@charlessu是的,没错。