2
votes

When trying to setup some CloudWatch alarms using Terraform for some reason it doesn't find the metrics and the alarm remains stuck in insufficient data. Terraform doesn't output any errors and I can find the metrics if I search manually in AWS. What am I missing here?

Example a simple healthy host alarm point to a target group:

#healthy host alarm
resource "aws_cloudwatch_metric_alarm" "health" {
  alarm_name          = "${var.tag_app}_healthy_host"
  comparison_operator = "LessThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "HealthyHostCount"
  namespace           = "AWS/ApplicationELB"
  period              = "60"
  statistic           = "Maximum"
  threshold           = "1"
  alarm_description   = "Healthy host count for EC2 machine"
  alarm_actions       = ["${data.aws_sns_topic.blabla.arn}"]
  ok_actions          = ["${data.aws_sns_topic.blabla.arn}"]

  dimensions = {
    TargetGroup  = "${aws_lb_target_group.alb_target.arn_suffix}"
  }
}

When I select another resource (EC2, RDS) and another metric I get a CloudWatch alarm pointing to the right metric and it doesn't remain stuck at insufficient data.

1

1 Answers

3
votes

The HealthyHostCount metric is only available under either the TargetGroup, LoadBalancer dimensions or the TargetGroup, AvailabilityZone, LoadBalancer so you need to at least add the LoadBalancer dimension as well to access this metric.

So your Terraform code should instead be:

#healthy host alarm
resource "aws_cloudwatch_metric_alarm" "health" {
  alarm_name          = "${var.tag_app}_healthy_host"
  comparison_operator = "LessThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "HealthyHostCount"
  namespace           = "AWS/ApplicationELB"
  period              = "60"
  statistic           = "Maximum"
  threshold           = "1"
  alarm_description   = "Healthy host count for EC2 machine"
  alarm_actions       = ["${data.aws_sns_topic.blabla.arn}"]
  ok_actions          = ["${data.aws_sns_topic.blabla.arn}"]

  dimensions = {
    LoadBalancer = "${aws_lb.example.arn_suffix}"
    TargetGroup  = "${aws_lb_target_group.alb_target.arn_suffix}"
  }
}