使用 Terraform 在 Azure 中为同一订阅下的所有 VM 创建警报 [英] Alert Creation for All VMs under same subscription in Azure using Terraform

查看:22
本文介绍了使用 Terraform 在 Azure 中为同一订阅下的所有 VM 创建警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**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 的快速入门模板站点供参考.

Also how can I define type as MultipleResourceMultipleMetricCriteria for this below template as in ARM templates we can define this type as odata.type but when I try odata_type in terraform its not accepting it. also is there any way I can grab subscription id at runtime using query instead of passing subscription id in variables file. Is there any quickstart template site for the terraform for reference.

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 文件

terraform.tf file

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"
  }
}

推荐答案

您可以使用 数据来源:azurerm_subscription 以访问有关现有订阅的信息.

You can use Data Source: azurerm_subscription to access information about an existing Subscription.

terraform.tf 文件将如下所示:

terraform.tf file will look like this:

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"
  }
}

这篇关于使用 Terraform 在 Azure 中为同一订阅下的所有 VM 创建警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆