Terraform: Ease using AWS ALBs in CloudWatch metrics

Created on 13 Sep 2016  ·  7Comments  ·  Source: hashicorp/terraform

Fantastic to see ALBs available in Terraform so quickly. Thank you! 👏

Part of cutting over to ALBs for us involves creating equivalent CloudWatch alarms around our upstream health. The dimensions aren't just the LoadBalancerName set to aws_elb.*.name like ELBs however. For ALBs the dimension is LoadBalancer with a partial suffix from the ALB ARN:

# Alarm if this alb's targets have more than 50 5xx errors within two minutes
resource "aws_cloudwatch_metric_alarm" "my-alb-too-many-server-errors" {
  alarm_name  = "my-alb-too-many-server-errors"
  namespace   = "AWS/ApplicationELB"
  metric_name = "HTTPCode_Target_5XX_Count"

  dimensions = {
    # CloudWatch wants part of the arn. It's kinda silly.
    # arn:aws:elasticloadbalancing:us-east-1:123456:loadbalancer/app/my-alb/abc123 => app/my-alb/abc123
    LoadBalancer = "${replace("${aws_alb.my-alb.arn}", "/arn:.*?:loadbalancer\\/(.*)/", "$1")}"
  }

  statistic           = "Sum"
  period              = 60
  comparison_operator = "GreaterThanThreshold"
  threshold           = "50"
  evaluation_periods  = 2
}

This definitely works, but it's pretty clumsy. Perhaps this ARN suffix could be exposed as a derived attribute of aws_alb resources?

enhancement provideaws

Most helpful comment

For anyone coming here looking for a solution, the below works

  • aws_alb.my-alb.arn_suffix
  • aws_alb_target_group.my-tg.arn_suffix

All 7 comments

Hi @sj26! This looks like a great candidate for an additional computed field. I'll look into adding this.

Thanks so much! 💖

Any chance we could do the same thing for target group resources?

http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/elb-metricscollected.html#load-balancer-metric-dimensions-alb

It would be good to have this exposed on the target group as well, as it is needed for metrics on that such as the unhealthy host count.

For anyone coming here looking for a solution, the below works

  • aws_alb.my-alb.arn_suffix
  • aws_alb_target_group.my-tg.arn_suffix

I was searching for this for hours. Why it's not documented here https://www.terraform.io/docs/providers/aws/d/lb.html ?

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings