gpt4 book ai didi

azure - 在 terraform 中,使字符串变量在操作 block 中可选

转载 作者:行者123 更新时间:2023-12-03 06:48:34 24 4
gpt4 key购买 nike

您好,我正在创建警报规则。对于某些警报,我不需要使用操作组发送任何通知。为此,我需要将操作组变量(字符串)设置为可选。

我尝试使用像 var.action_group_id != null action_group_id : ""这样的条件。但我收到以下错误。

Error: Can not parse "action.0.action_group_id" as a resource id: Cannot parse Azure ID: parse "null": invalid URI for request

我尝试将操作 block 制作为动态 block ,但它不可迭代。 for_each 不支持具有一个值的单个变量的动态 block 。

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 = string
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
}

我想将 action_group_id 设置为可选变量。在某些情况下我不想引用操作组 ID

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

最佳答案

action 是一个动态 block ,因此要使其可选,您可以执行以下操作:

  dynamic "action" {
for_each = var.action_group_id != null ? [1] : []
content {
action_group_id = var.action_group_id
}
}

关于azure - 在 terraform 中,使字符串变量在操作 block 中可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73902326/

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