Home > OS >  Looking to make a Dimension block optional in terraform reusable modules
Looking to make a Dimension block optional in terraform reusable modules

Time:08-31

I succesfully created reusable module.But i am trying to make the dimension block optional.when it is called in root module

 resource "azurerm_monitor_metric_alert" "example" {
      name                = "example-metricalert"
      resource_group_name = var.resource_group_name
      scopes              = [data.azurerm_eventhub_namespace.testEventhub.id]
      description         = "Action will be triggered when Transactions count is greater than 50."
      frequency = "PT30M"
      window_size = "PT1H"
      criteria {
        metric_namespace = "Microsoft.EventHub/namespaces"
        metric_name      = "IncomingRequests"
        aggregation      = "Total"
        operator         = "GreaterThan"
        threshold        = 90
    
        dimension {
          name     = "EntityName"
          operator = "Include"
          values   = ["eventhubone"]
        }
      }
    
      action {
        action_group_id = data.azurerm_monitor_action_group.emailalert.id
      }
    }

resuable module i am looking to include dimension block in this reusable module as an optional

resource "azurerm_monitor_metric_alert" "main" {
  for_each = var.alert_rules
  
  name                = each.value.alertname
  resource_group_name = var.resource_group_name
  description         = each.value.description
  scopes              = var.alert_scope
  severity            = each.value.severity
  frequency           = each.value.frequency
  window_size         = each.value.windowsize
  criteria {
    metric_namespace  = each.value.metric_namespace
    threshold         = each.value.threshold
    metric_name       = each.value.metric_name
    aggregation       = each.value.aggregation
    operator          = each.value.operator
    
 
        action {
         action_group_id = var.actiongroupid
       }
  }

Variable.tf for reusable module ( trying to include dimension block argument's as an optional

variable "actiongroupid" {
    type = any
  description = "id of the action group"
}



variable "resource_group_name" {
    type = string
  description = "name of the resource group"
}





variable "alert_rules" {
    type = map(object({  
     alertname = string 
     metric_namespace = string      
     severity = number
     metric_name = string
     frequency = any
     windowsize = any
     # window size must be gretar than Frequency values be PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H and P1D. Defaults to PT5M
     aggregation = string
     description = string
     operator = string
     threshold = number        
    }))
}

'''

CodePudding user response:

As you have noted yourself, you need to use the dynamic block [1] along with for_each [2] to make this work. The easiest way to do this would be:

dynamic "dimension" {
  for_each = var.enable_dimension ? [1] : []
  
  content {
    name     = "EntityName"
    operator = "Include"
    values   = ["eventhubone"]
  }
}

As mentioned in the comments, the enable_dimension variable would be a bool and based on its value, the dimension block would be added (when equal to true) or not (when equal to false):

variable "enable_dimension" {
  type        = bool
  description = "Enable/disable dimension block."
}

[1] https://www.terraform.io/language/expressions/dynamic-blocks#dynamic-blocks

[2] https://www.terraform.io/language/meta-arguments/for_each

  • Related