使用Terraform为Azure中相同订阅下的所有VM创建警报

时间:2020-10-02 06:01:32

标签: terraform-provider-azure azure-monitoring

**I am trying to deploy below terraform alert for all VMs under my subscription, and subscription id I've kept it in variables file. But its throwing below error. 

我又如何在下面的模板中为此类型定义类型为MultipleResourceMultipleMetricCriteria,就像在ARM模板中一样,我们可以将此类型定义为odata.type,但是当我在terraform中尝试使用odata_type时,它不接受它。 我也可以通过任何方式在运行时使用查询来获取订阅ID,而不是在变量文件中传递订阅ID。 terraform是否有任何快速入门模板站点供参考。

Error: **Can not parse "scopes.0" as a resource id: Cannot parse Azure ID: parse "{subscription_id is getting printed here}":** invalid URI for request**

  on metric.tf line 1, in resource "azurerm_monitor_metric_alert" "example":
   1: resource "azurerm_monitor_metric_alert" "example" {

terraform.tf文件

resource "azurerm_monitor_metric_alert" "example" {
  name                = "example-metricalert"
  resource_group_name = "MyTemp"
  scopes              = ["${var.subscription_id}"]
  description         = "Action will be triggered when Transactions count is greater than 50."
  target_resource_type = "Microsoft.Compute/virtualMachines"
  
  criteria { 
    metric_namespace = "Microsoft.Compute/virtualMachines"
    metric_name      = "Percentage CPU"
    aggregation      = "Total"
    operator         = "GreaterThan"
    threshold        = 50
  }

  action {
    action_group_id = "/subscriptions/xxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.Insights/actionGroups/xxxxx"
  }
}

1 个答案:

答案 0 :(得分:0)

您可以使用Data Source: azurerm_subscription访问有关现有订阅的信息。

terraform.tf文件将如下所示:

data "azurerm_subscription" "current" {

  subscription_id  = var.subscription_id
}

resource "azurerm_monitor_metric_alert" "example" {
  name                = "example-metricalert"
  resource_group_name = "MyTemp"
  scopes              = [data.azurerm_subscription.current.id]
  description         = "Action will be triggered when Transactions count is greater than 50."
  target_resource_type = "Microsoft.Compute/virtualMachines"
  
  criteria { 
    metric_namespace = "Microsoft.Compute/virtualMachines"
    metric_name      = "Percentage CPU"
    aggregation      = "Total"
    operator         = "GreaterThan"
    threshold        = 50
  }

  action {
    action_group_id = "/subscriptions/xxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.Insights/actionGroups/xxxxx"
  }
}
相关问题