'How to fetch AmazonMQ nodes for RabbitMQ brokers using API, CLI or Terraform

I'm trying to create AWS Cloudwatch alarm for systemCpuUtilizaiton of each RabbitMQ broker nodes via Terraform. To create the AWS Cloudwatch alarm, I need to provide dimensions (node-name and broker) as mentioned in AWS docs.

Hence, I'm looking to fetch the rabbitMQ broker node-names from AWS (via CLI, or API or Terraform)

Please note: I'm able to see the matrices of each broker nodes in AWS Cloudwatch console, but not from API, SDK or CLI.

I went through the below links but didn't get anything handy https://awscli.amazonaws.com/v2/documentation/api/latest/reference/mq/index.html#cli-aws-mq https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/mq_broker

Please let me know in case I'm missing something.



Solution 1:[1]

Recently, AWS started publishing CPU/Mem/Disk metrics per Broker.

You should see these metrics under AmazonMQ/Broker metrics. You can now use the SystemCpuUtilization metric without a node name dimension and then take the Maximum statistic to get the most overloaded node. You can create a CloudWatch alarm based on this metric.

enter image description here

Solution 2:[2]

The AWS MQ node names used for the cloudwatch dimensions do not appear to be exposed through the AWS API, but the node name is predictable knowing the IP address. I believe this can be used to construct valid node names for alarms.

data "aws_region" "current" {}

resource "aws_mq_broker" "example" {
  ...
}

resource "aws_cloudwatch_metric_alarm" "bat" {
  for_each = toset([
    for instance in aws_mq_broker.example.instances : instance.ip_address
  ])

  alarm_name          = "terraform-test-foobar5"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "2"
  metric_name         = "SystemCpuUtilization"
  namespace           = "AWS/AmazonMQ"
  period              = "120"
  statistic           = "Average"
  threshold           = "80"

  dimensions = {
    Broker = aws_mq_broker.example.name
    Node   = "rabbitmq@ip-${replace(each.value, ".", "-")}.${data.aws_region.current.name}.compute.internal"
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stefan Moser
Solution 2