Terraform iterate over nested map from module

517 Views Asked by At

I'm fairly new to Terraform, and I'm struggling trying to design a module to create Monitor Alerts in Azure.

I've essentially created a module that will generate alerts based on a nested map passed in from my storage-account-alerts.tf file. However I'm receiving errors saying all of the required arguments are missing. I've tried a few different things, but from my understanding this should work? I've declared the variables in the variables.tf file as well.

module "storage-account-alerts" {
    source = "../modules/az-monitor-alerts"

    alerts = local.alerts

}

locals {
    alerts = {
      SA_average_availability = {
          name = "Test Name"
          scope = var.sa_avg_availability_tp2_scope
          description = "Test description"
          action_group_id = azurerm_monitor_action_group.ag["Test-ag-1"].id
          severity = 1
          frequency = "PT5M"
          window_size = "PT15M"
          criteria = [{
              metric_namespace = "Microsoft.Storage/storageAccounts"
              metric_name      = "Availability"
              aggregation      = "Average"
              operator         = "LessThan"
              threshold        = 100
          }]
          target_resource_type = "Microsoft.Storage/storageAccounts"
          target_resource_location = "canadacentral"
      }
  } 
}

And the module itself looks like this:

resource "azurerm_monitor_metric_alert" "alert" {

  for_each = var.alerts

  name                      = each.value.name
  resource_group_name       = var.resource_group_name
  scopes                    = each.value.scope
  description               = each.value.description
  severity                  = each.value.severity
  frequency                 = each.value.frequency
  window_size               = each.value.window_size
  target_resource_type      = try(each.value.target_resource_type, "")
  target_resource_location  = try(each.value.target_resource_location, "")

  
  dynamic "criteria" { 
    for_each = each.value.criteria
    
    content {
        metric_namespace = criteria.value.metric_namespace
        metric_name      = criteria.value.metric_name
        aggregation      = criteria.value.aggregation
        operator         = criteria.value.operator
        threshold        = criteria.value.threshold
        skip_metric_validation = true

        dynamic "dimension" {
            for_each = try(criteria.value.dimensions, [])

            content {
                name        = dimension.value.name
                operator    = dimension.value.operator
                values      = dimension.value.values
            }
        }
    }
  }

  action {
    action_group_id = each.value.action_group_id
  }
}

Edit: Added errors

│ Error: Missing required argument
│
│   on storage-account-alerts.tf line 1, in module "storage-account-alerts":
│    1: module "storage-account-alerts" {
│
│ The argument "action_group_id" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│
│   on storage-account-alerts.tf line 1, in module "storage-account-alerts":
│    1: module "storage-account-alerts" {
│
│ The argument "name" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│
│   on storage-account-alerts.tf line 1, in module "storage-account-alerts":
│    1: module "storage-account-alerts" {
│
│ The argument "target_resource_location" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│
│   on storage-account-alerts.tf line 1, in module "storage-account-alerts":
│    1: module "storage-account-alerts" {
│
│ The argument "frequency" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│
│   on storage-account-alerts.tf line 1, in module "storage-account-alerts":
│    1: module "storage-account-alerts" {
│
│ The argument "window_size" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│
│   on storage-account-alerts.tf line 1, in module "storage-account-alerts":
│    1: module "storage-account-alerts" {
│
│ The argument "target_resource_type" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│
│   on storage-account-alerts.tf line 1, in module "storage-account-alerts":
│    1: module "storage-account-alerts" {
│
│ The argument "resource_group_name" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│
│   on storage-account-alerts.tf line 1, in module "storage-account-alerts":
│    1: module "storage-account-alerts" {
│
│ The argument "scopes" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│
│   on storage-account-alerts.tf line 1, in module "storage-account-alerts":
│    1: module "storage-account-alerts" {
│
│ The argument "description" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│
│   on storage-account-alerts.tf line 1, in module "storage-account-alerts":
│    1: module "storage-account-alerts" {
│
│ The argument "severity" is required, but no definition was found.

Any help is appreciated!

0

There are 0 best solutions below