Terraform 嵌套或 For 循环来配置 Azure 资源

问题描述 投票:0回答:1

所以我有一个 Terraform

locals.tf
文件,其中包含以下内容来表示我打算部署到的一组环境:

locals {
    environments = [
    "sandbox", 
    "test", 
    "prod"]
}

然后,我还有一个

monitor.tf
文件,该文件引用我希望配置的 Azure Monitor 警报资源的 Terraform 模块,其给出如下:

# Create Alert
module "azurerm_monitor_alert" {
  source   = "git::https://.../.../_git/module-tf-azurerm-monitor-alert"
  for_each = local.monitor_alerts[var.environment]
  alert               = each.value
  resource_group_name = each.value.resource_group_name
  tags = {
    dept = "infra"
  }
  depends_on = [ 
    azurerm_resource_group.alerts_resource_group
  ]
}

然后我有一个

local_alerts.tf
文件,它使用以下 terraform 配置来实现为每个目标环境(在本例中为非产品)提供所需资源的模块。

locals {

      monitor_alert_webhook_service_name = "customer-orders"

            monitor_alerts = {

                **SANDBOX** = {
                  orders_failure = {
                    alert_type                        = "metric"
                    name                              = "${["azure_monitor_log_search_alert"]}-rx-failures"
                    resource_group_name               = local.alerts_resource_group
                    location                          = var.location
                    description                       = "Sandbox - One or more calls have failed"
                    scopes                            = [data.azurerm_application_insights.service_app_insights.id] 
                    enabled                           = true
                    evaluation_frequency              = "PT1M"
                    severity                          = 1
                    target_resource_type              = "Microsoft.Insights/components"
                    criteria = [
                      {
                        metric_namespace = "Azure.ApplicationInsights"
                        metric_name = "Submission Failures"
                        aggregation = "Count"
                        operator = "GreaterThan"
                        threshold = 0
                        skip_metric_validation = false
                      }
                    ]
                    
                    action = [
                      {
                        action_group_id = try(module.azurerm_monitor_action_group["rx_engineers"].action_group.id, "")
                        webhook_properties = {
                          service = local.monitor_alert_webhook_service_name
                        }
                      }
                    ]

                }

                **TEST** = {
                  orders_failure = {
                    alert_type                        = "metric"
                    name                              = "${["azure_monitor_log_search_alert"]}-rx-failures"
                    resource_group_name               = local.alerts_resource_group
                    location                          = var.location
                    description                       = "Test - One or more calls have failed"
                    scopes                            = [data.azurerm_application_insights.service_app_insights.id] 
                    enabled                           = true
                    evaluation_frequency              = "PT1M"
                    severity                          = 1
                    target_resource_type              = "Microsoft.Insights/components"
                    criteria = [
                      {
                        metric_namespace = "Azure.ApplicationInsights"
                        metric_name = "Submission Failures"
                        aggregation = "Count"
                        operator = "GreaterThan"
                        threshold = 0
                        skip_metric_validation = false
                      }
                    ]
                    
                    action = [
                      {
                        action_group_id = try(module.azurerm_monitor_action_group["rx_engineers"].action_group.id, "")
                        webhook_properties = {
                          service = local.monitor_alert_webhook_service_name
                        }
                      }
                    ]
                }

                **PROD** = {
                  orders_failure = {
                    alert_type                        = "metric"
                    name                              = "${["azure_monitor_log_search_alert"]}-rx-failures"
                    resource_group_name               = local.alerts_resource_group
                    location                          = var.location
                    description                       = "Prod - One or more calls have failed"
                    scopes                            = [data.azurerm_application_insights.service_app_insights.id] 
                    enabled                           = true
                    evaluation_frequency              = "PT1M"
                    severity                          = 1
                    target_resource_type              = "Microsoft.Insights/components"
                    criteria = [
                      {
                        metric_namespace = "Azure.ApplicationInsights"
                        metric_name = "Submission Failures"
                        aggregation = "Count"
                        operator = "GreaterThan"
                        threshold = 0
                        skip_metric_validation = false
                      }
                    ]
                    
                    action = [
                      {
                        action_group_id = try(module.azurerm_monitor_action_group["rx_engineers"].action_group.id, "")
                        webhook_properties = {
                          service = local.monitor_alert_webhook_service_name
                        }
                      }
                    ]
                }
    
}

最后,在我的

local_alerts.tf
文件中,我不想使用同一代码块的多个实例来表示每个环境(顺便说一下,数量远多于我列出的 3 个),我希望有一个单独的实例我可以使用嵌套的 for_each 和/或 for 循环迭代引用该块,然后为每个目标环境创建监视器警报资源(在初始 locals.tf 文件中引用)。有什么建议或例子来展示我如何实现这一目标?

azure for-loop foreach terraform nested-loops
1个回答
0
投票

Terraform 嵌套或 For 循环来配置 Azure 资源

您可以通过有效地利用

for_each
for
循环为多个环境创建监控警报。

使用

local.environments
定义您的环境,每个值都根据您的要求包含
description
location
等属性。

本地.tf

locals {
  environments = {
    sandbox = {
      description = "Sandbox - One or more calls have failed"
      location    = "eastus"
    },
    test = {
      description = "Test - One or more calls have failed"
      location    = "eastus2"
    },
    prod = {
      description = "Prod - One or more calls have failed"
      location    = "westus"
    }
  }

  monitor_alert_webhook_service_name = "customer-orders"
}

主.tf

provider "azurerm" {
  features {}
}

data "azurerm_application_insights" "demo" {
  name                = "sampleinsights"
  resource_group_name = "venkatrg"
}

data "azurerm_resource_group" "example"{
    name     = "venkatrg"
}
resource "azurerm_monitor_action_group" "rx_engineers" {
  name                = "CriticalAlertsAction"
  resource_group_name = data.azurerm_resource_group.example.name
  short_name          = "p0action"

  email_receiver {
    name          = "sendtoadmin"
    email_address = "[email protected]"
  }
}

resource "azurerm_monitor_metric_alert" "alerts" {
  for_each = local.environments

  name                = "${each.key}-orders-failure"
  resource_group_name = data.azurerm_resource_group.example.name
  description         = each.value.description
  scopes              = [data.azurerm_application_insights.demo.id]
  enabled             = true
  severity            = 1
  target_resource_type= "Microsoft.Insights/components"

  criteria {
    metric_namespace        = "Azure.ApplicationInsights"
    metric_name             = "Submission Failures"
    aggregation             = "Count"
    operator                = "GreaterThan"
    threshold               = 0
    skip_metric_validation  = true
  }

  action {
    action_group_id = azurerm_monitor_action_group.rx_engineers.id
    webhook_properties = {
      service = local.monitor_alert_webhook_service_name
    }
  }
}

Terraform 应用

enter image description here

执行上述

Terraform
代码后,3警报规则已创建。

enter image description here enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.