【问题标题】:Using splat operator in terraform azurerm_monitor_metric_alert scope setting在 terraform azurerm_monitor_metric_alert 范围设置中使用 splat 运算符
【发布时间】:2020-03-23 11:14:27
【问题描述】:

我正在尝试为我的应用服务设置一个 azurerm_monitor_metric_alert,我想定义一个警报,涵盖 terraform 正在构建的所有应用服务。

我构建的应用服务有两个维度,一个基于区域(最多两个),另一个基于部署到每个应用服务计划的应用服务数量(未知数字,下例中为两个)。

我希望我可以这样做:

resource "azurerm_monitor_metric_alert" "disk1" {
  name                = "AppService-diskSpace-Sev1"
  resource_group_name = azurerm_resource_group.location1[0].name
  scopes              = ["${azurerm_app_service.location1.*.id}","${azurerm_app_service.location2.*.id}"]
  description         = "Disk space over 90 percent"
  window_size         = "PT6H"
  frequency           = "PT1H"

  criteria {
    metric_namespace = "Microsoft.Web/sites"
    metric_name      = "FileSystemUsage"
    aggregation      = "Average"
    operator         = "GreaterThan"
    threshold        = 241591910400 # 90% of 250Gb in bytes
  }
  severity         = 1
}

但我收到如下错误:

Error: Incorrect attribute value type

  on ..\..\..\infra\terraform\global\web\main.tf line 343, in resource "azurerm_monitor_metric_alert" "disk1":
 343:   scopes              = ["${azurerm_app_service.location1.*.id}","${azurerm_app_service.location2.*.id}"]
    |----------------
    | azurerm_app_service.location is tuple with 2 elements
    | azurerm_app_service.location2 is tuple with 2 elements

Inappropriate value for attribute "scopes": element 0: string required.

我尝试了许多不同的选项,但都产生错误,文档说

“应应用度量标准的一组资源 ID 字符串”

但我不确定“一组字符串”在这种情况下是什么意思。

-- 编辑 在下面的 cmets 之后,我尝试了我希望得到的建议,但我仍然收到错误:

concat(azurerm_app_service.location.*.id)

返回

Error: scopes: attribute supports 1 item maximum, config has 2 declared. 
["${azurerm_app_service.location.*.id}"]

返回

Inappropriate value for attribute "scopes": element 0: string required.
"${azurerm_app_service.web.*.id}"

返回

Error: scopes: attribute supports 1 item maximum, config has 2 declare

【问题讨论】:

  • ${azurerm_app_service.location1.*.id} 是一个 ID 列表,因此 [${azurerm_app_service.location1.*.id}, ...] 创建一个列表列表。您不需要额外的括号,只需使用 concat 函数加入 2 个列表即可。
  • 恐怕我仍然无法让它工作:concat(azurerm_app_service.location.*.id) 返回Error: scopes: attribute supports 1 item maximum, config has 2 declared["${azurerm_app_service.location.*.id}"] 返回Inappropriate value for attribute "scopes": element 0: string required."${azurerm_app_service.web.*.id}" 返回Error: scopes: attribute supports 1 item maximum, config has 2 declare
  • 您应该编辑您的问题以包含您尝试过的所有内容。
  • 我已经尝试了许多不同的方法,但都不起作用,我认为用失败的例子乱扔我的问题没有太大的价值,我只需要一个应该起作用的例子,我可以去从那里开始。
  • 你只是试图用这种方式回复,但基本上是不可读的。您应该编辑问题,而不是使用代码块或错误消息进行评论。从评论中很难确定您是否真的尝试连接 2 个列表,或者您只是传递了一个列表,该列表不是该函数的工作方式(然后会被要求连接多个非列表对象)。但是因为你在评论中回复了,所以无法判断。

标签: terraform terraform-provider-azure azure-rm


【解决方案1】:

这个问题很老了,但还是没有答案,我也有类似的问题,所以我把我的研究结果给你:

首先,在 terraform 0.12 中更改了 splat 表达式语法,因此 resource.*.attribute 现在是 resource[*].attribute。这会返回一个列表,这就是您收到“不适当的值”错误的原因:

scopes = concat(azurerm_app_service.location1[*].id, azurerm_app_service.location2[*].id)

应该是正确的。

另一个错误:“范围:属性最多支持 1 个项目,配置有 2 个声明”是因为其他必需的属性,如果您对范围使用多个值。在属性 target_resource_type 和 target_resource_location 中查看此资源的提供者文档。如果使用多个范围,则需要这两个: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_metric_alert#target_resource_type

我无法在 azure 上对此进行测试,因为我们不使用它,但我希望它会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 2017-07-09
    相关资源
    最近更新 更多