'Terraform Activity Log Alerts Status Field

I'm in the process of recreating our company's alerts in Terraform after they were originally created manually. I have been looking into the JSON of each alert to obtain the property values, however I'm stumped on what appears to be a 'status' field in the Activity Log Alerts.

The JSON is below:

                {
                    "anyOf": [
                        {
                            "field": "properties.currentHealthStatus",
                            "equals": "Degraded",
                            "containsAny": null,
                            "odata.type": null
                        },
                        {
                            "field": "properties.currentHealthStatus",
                            "equals": "Unavailable",
                            "containsAny": null,
                            "odata.type": null
                        },
                        {
                            "field": "properties.currentHealthStatus",
                            "equals": "Unknown",
                            "containsAny": null,
                            "odata.type": null
                        }
                    ],
                    "odata.type": null
                },
                {
                    "anyOf": [
                        {
                            "field": "status",
                            "equals": "In Progress",
                            "containsAny": null,
                            "odata.type": null
                        },
                        {
                            "field": "status",
                            "equals": "Resolved",
                            "containsAny": null,
                            "odata.type": null
                        },
                        {
                            "field": "status",
                            "equals": "Updated",
                            "containsAny": null,
                            "odata.type": null
                        }
                    ],
                    "odata.type": null
                }
            ],
            "odata.type": null

Currently my module for creating the is this:

resource "azurerm_monitor_activity_log_alert" "alert_rh" {
  
  for_each = var.activity_log_rh_alerts

  name                      = each.value.name
  resource_group_name       = var.resource_group_name
  scopes                    = each.value.scopes
  description               = each.value.description

  dynamic "criteria" { 
    for_each = each.value.criteria

    content {
      category = criteria.value.category
      resource_group = criteria.value.resource_group
      resource_id = criteria.value.resource_id
      status = try(criteria.value.status, [])

      resource_health {
        current = try(criteria.value.current, [])
        previous = try(criteria.value.previous, [])
      }
    }

  }

  action {
    action_group_id = each.value.action_group_id
  }

}

The documentation specifies there is a status field, however the argument that is expected is a string instead of a list, and none of the possible values match what is in the JSON.

Any help is appreciated.



Solution 1:[1]

You need category,resource_group,resource_id,status,current,previous fields in the json file as its not there now.

content {
          category = criteria.value.category
          resource_group = criteria.value.resource_group
          resource_id = criteria.value.resource_id
          status = try(criteria.value.status, [])
    
          resource_health {
            current = try(criteria.value.current, [])
            previous = try(criteria.value.previous, [])
          }
        }

Instead of fetching the json values from jsonfile you can directly include in main.tf file likewise i have did in this thread. Get an idea from this thread implement it accordingly. for_each` expects a map or a set. Instead of using jsonencode, use Terraform's map directly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RahulKumarShaw-MT