Vicky
Vicky

Reputation: 1225

how to create composite CloudWatch alarm for Sum of two metrics in terraform

I am writing alarms for the MSK cluster and for the CPU utilization I need to create a composite metric which is sum of two metrices CpuUser and CpuSystem. The alarm will get triggered when the composite metric reaches an average of CPU utilization of 60%. https://repost.aws/knowledge-center/msk-broker-high-cpu-usage

Can anyone help here? AWS official documentation suggests using the Math function for this https://docs.aws.amazon.com//AmazonCloudWatch/latest/monitoring/using-metric-math.html.

Upvotes: 0

Views: 601

Answers (1)

Vicky
Vicky

Reputation: 1225

I achieved this by using 'metric_query'. created two metric queries for both metrics 'CpuUser' and 'CpuUser' then in the third one provided expression of addition.. so finally the code looks like this.

resource "aws_cloudwatch_metric_alarm" "cpu_utilization" {
  count  = var.no_of_broker_nodes
  alarm_name = "alarm_name-broker-${count.index + 1}" 
  comparision_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods   = 2
  threshold = 60
  alarm_description = "des"
  alarm_actions = "Sns_topic_name"
  metric_query {
     id = "cpu_utilization"
     expression = "m1 + m2"
     label  = "CpuUtilization"
     return_data = true
  }
  metric_query {
     id = "m1"
     metric {
       metric_name = "CpuUser"
       namespace   = "AWS/Kafka"
       period      = 300
       stat        = "Average"
       dimensions  = {
           "Cluster Name" = "Cluster_Name"
           "Broker ID"    = "${count.index + 1}"
       }
     }
  }
  metric_query {
     id = "m2"
     metric {
       metric_name = "CpuSystem"
       namespace   = "AWS/Kafka"
       period      = 300
       stat        = "Average"
       dimensions  = {
           "Cluster Name" = "Cluster_Name"
           "Broker ID"    = "${count.index + 1}"
       }
     }
  }

}

Upvotes: 1

Related Questions