0

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!

3
  • We need to see the actual error message since you claim it stems from the provider resource schema, and not Terraform core. Commented Apr 12, 2022 at 18:52
  • @MattSchuchard - have added the errors, thanks Commented Apr 12, 2022 at 19:07
  • Those error messages look like your declared module has declared variables with no default values, and you did not provide input arguments for them in your module declaration. I would recommend replacing the azurerm_monitor_metric_alert.alert in the question with the module variable declaration snippets. Commented Apr 12, 2022 at 19:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.