gpt4 book ai didi

azure - 在terraform中如何根据变量将数值转换为字符串

转载 作者:行者123 更新时间:2023-12-03 06:49:28 30 4
gpt4 key购买 nike

您好,我正在尝试根据警报中的严重性创建警报名称。在输入中,我已经给出了严重性的数值,但我正在尝试将该严重性的相等字符串(单词)附加到警报名称

严重 = 0 错误 = 1 警告 = 2 信息 = 3 详细 = 4

I am getting alert name like keyvault - 0

I would like to get keyvault - critical

resource "azurerm_monitor_metric_alert" "keyvault_alert" {

for_each = var.keyvault_alert_rules

name = "${var.kv_name} - ${each.value.severity}"
resource_group_name = var.resource_group_name
description = each.value.description
scopes = var.alert_scope
severity = each.value.severity
frequency = each.value.frequency
`window_size = each.value.windowsize`

# criteria block
criteria {

metric_namespace = "Microsoft.KeyVault/vaults"
threshold = each.value.threshold
metric_name = each.value.metric_name
aggregation = each.value.aggregation
operator = each.value.operator

# dimension block

dynamic "dimension" {
for_each = each.value.dimension != null ? each.value.dimension : []
content {
name = dimension.value.dimensionname
operator = dimension.value.dimensionoperator
values = dimension.value.dimensionvalues
}
}

}

action {
action_group_id = var.action_group_id
}

variable.tf

variable "action_group_id" {
type = any
description = "ID of the action group"
}


variable "resource_group_name" {
type = string
description = "name of the resource group"
}

/* in the variables i am passing warning as n input.is there any way i can append warning to alert name in the main.tf based on the severity value which is given down below*/
variable "kv_alert_rules" {
type = map(object({
display_name = string
# display_name = "(severity numeric equalent ex:warning)-(metric name)"


#------details for the alert criteria
metric_name = string
operator = string
threshold = number
aggregation = string

#------ dimension vaules----------
dimension = list(object({
dimensionname = string
dimensionoperator = string
dimensionvalues = list(string)
}))
#-----------------------------------
severity = number
frequency = string
windowsize = string
# window size must be gretar than Frequency values be PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H and P1D. Defaults to PT5M

description = string

}))

description = "This variable for alert criteria for key vault"


default = {
"Alert_1" = {
# display_name = "(severity numeric equalent ex:warning)-(generic word for metric name)"
display_name = "warning-used capacity"
severity = 2
dimension = null
metric_name = "SaturationShoebox"
aggregation = "Average"
frequency = "PT30M"
description = "Alert fires When Used vault capacity is GreaterThan 85"
windowsize = "PT1H"
operator = "GreaterThan"
threshold = 85
}
}
}

variable "kv_name" {
description = "key vault name "
type = string
}

module calling

module "keyvault" {
source = "../testing/key-vault-alert"
alert_scope = [data.azurerm_key_vault.examplekeyvault.id]
action_group_id = module.action-group.AGidout
resource_group_name = var.resource_group_name
kv_name = data.azurerm_key_vault.examplekeyvault.name
}

如果有人知道如何做到这一点,请指导我谢谢

最佳答案

只需在 locals 部分创建一个新 map :

locals {
severity_alerts = {
0 = "Critical",
1 = "Error",
2 = "Warning"
}
}

然后将资源 azurerm_monitor_metric_alert 名称属性更新为:

format("%s - %s", var.kv_name, lookup(local.severity_alerts, each.value.severity))

通过使用lookup函数,您可以通过提供键从映射中检索值。

关于azure - 在terraform中如何根据变量将数值转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73830485/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com