mattb
mattb

Reputation: 472

Using splat operator in terraform azurerm_monitor_metric_alert scope setting

I'm trying to setup an azurerm_monitor_metric_alert for my app services, I'd like to define one alert which covers all my app services which terraform is building.

I have two dimensions to my app services that are build, one based on regions (max of two) and the other on the number of app services deployed to each app service plan (unknown number, two in the below example).

I'd hoped I could do something like:

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
}

But I get an error like:

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.

I've tried a number of different options but all produce errors, the doc says

"A set of strings of resource IDs at which the metric criteria should be applied"

but I'm not sure what a "set of strings" means in this context.

-- EDIT After comments below I tried what I hoped was being suggested but I'm still getting errors:

concat(azurerm_app_service.location.*.id)

returns

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

returns

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

returns

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

Upvotes: 0

Views: 874

Answers (1)

Sascha
Sascha

Reputation: 11

this question is quite old, but still no answer, and I had a similar problem, so I give you the results of my research:

First, the splat expression syntax changed in terraform 0.12, so resource.*.attribute is now resource[*].attribute. This returns a list, that's why you get the "Inappropriate value" errors:

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

would be correct.

The other error: "scopes: attribute supports 1 item maximum, config has 2 declare" is because of other required attributes, if you use more than one value for scopes. Have a look at the provider documentation of this ressource at the attributes target_resource_type and target_resource_location. These two are required if using more than one scope: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_metric_alert#target_resource_type

I couldn't test this on azure, because we don't use it, but I hope it helps.

Upvotes: 1

Related Questions