Search code examples
amazon-web-servicesterraformaws-application-load-balancer

CloudWatch alarams using terraform for load balancer


I am trying to create cloudwatch alarams for LB using terraform using below code. I am getting an error An argument named "alb_arn_suffix" is not expected here.

here is the sample code which i am using.

   resource "aws_cloudwatch_metric_alarm" "this" { 
      alarm_name          = "alb-alarams"
      alarm_description   = "unhealthy"
      comparison_operator = "GreaterThanOrEqualToThreshold"
      evaluation_periods  = 1
      threshold           = 1
      period              = 60
      unit                = "Count"
      namespace   = "ALB"
      metric_name = "UnHealthyHostCount"
      statistic   = "Sum"
      alb_arn_suffix = ["arn:aws:elasticloadbalancing:eu-west-2:124531745575:loadbalancer/app/alb- 
                        123/1cd382a00a565a8b"]
      alarm_actions = ["arn:aws:sns:eu-west-2:124531745575:alb-alerts"]
      dimensions = {
     Name="ALB"
     Value="test"
     }

Please advise.


Solution

  • There are several issues with your aws_cloudwatch_metric_alarm.

    1. alb_arn_suffix is invalid, thus error.
    2. dimensions are also incorrect
    3. namespace is sadly also wrong.

    UnHealthyHostCount metric is part of AWS/ApplicationELB namespace, which has only two set of dimensions:

    • TargetGroup, LoadBalancer
    • TargetGroup, AvailabilityZone, LoadBalancer

    Assuming that you would use first set, aws_cloudwatch_metric_alarm would be something like the following:

     resource "aws_cloudwatch_metric_alarm" "this" { 
        alarm_name          = "alb-alarams"
        alarm_description   = "unhealthy"
        comparison_operator = "GreaterThanOrEqualToThreshold"
        evaluation_periods  = 1
        threshold           = 1
        period              = 60
        unit                = "Count"
        namespace   = "AWS/ApplicationELB"
        metric_name = "UnHealthyHostCount"
        statistic   = "Sum"
        alarm_actions = ["arn:aws:sns:eu-west-2:124531745575:alb-alerts"]
        
        dimensions = {
           TargetGroup = aws_lb_target_group.lb-tg.arn_suffix
           LoadBalancer = aws_lb.lb.arn_suffix
       }   
    }
    

    You would have to substitute aws_lb_target_group and aws_lb for your values.