Ashish Saxena
Ashish Saxena

Reputation: 21

AWS Cloudwatch Alarm Terraform configuration

I have configured following AWS Cloudwatch alarm using terraform. However when I look at the alarm data, I don't see any values. Alarm does not trigger.

resource "aws_cloudwatch_metric_alarm" "Test_Alarm" {
  alarm_name          = "Test_Alarm"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "1"
  metric_name         = "RequestCount"
  namespace           = "AWS/ApplicationELB"
  period              = "60"
  statistic           = "Sum"
  threshold           = "2"
  alarm_description   = "Test_Alarm_ALB"
  treat_missing_data  = "notBreaching"
  alarm_actions       = ["${aws_sns_topic.sns_topic.arn}"]
  ok_actions          = ["${aws_sns_topic.sns_topic.arn}"]
  dimensions = {
    LoadBalancer = aws_alb.application_load_balancer.name
  }
}

# SNS Topic for Errors
resource "aws_sns_topic" "sns_topic" {
  name = "${var.app_name}-sns_topic"
}

resource "aws_sns_topic_policy" "notify_policy" {
  arn    = aws_sns_topic.sns_topic.arn
  policy = data.aws_iam_policy_document.notify_policy.json
}

data "aws_iam_policy_document" "notify_policy" {
  statement {
    actions = [
      "SNS:Publish",
    ]

    resources = [
      "${aws_sns_topic.sns_topic.arn}",
    ]

    principals {
      type        = "Service"
      identifiers = ["cloudwatch.amazonaws.com"]
    }
  }
}

Cloudwatch alarm metric - Test_Alarm

Upvotes: 1

Views: 1310

Answers (1)

Ashish Saxena
Ashish Saxena

Reputation: 21

Solved this query. Had issue in "dimensions".

instead of Load balancer value of .name

dimensions = {
    LoadBalancer = aws_alb.application_load_balancer.name
  }

should have used .arn_suffix

dimensions = {
    LoadBalancer = aws_alb.application_load_balancer.arn_suffix
  }

Upvotes: 1

Related Questions