Home > OS >  How do I target a specific RDS instance within a cloudwatch alarm using Terraform?
How do I target a specific RDS instance within a cloudwatch alarm using Terraform?

Time:07-07

I'm trying to create a CloudWatch alarm for CPUUtilization on an RDS Instance using terraform. I am able to get an alarm working but am unsure which RDS instance in particular this is monitoring. Hence I would like to be able to choose a specific RDS instance to monitor.

The code below works in as far as it builds a resource for an alarm, which when triggered sends an email notification via SNS topic.

resource "aws_cloudwatch_metric_alarm" "CPUUtilization" {
  alarm_name                = "test-cpu-alarm"
  comparison_operator       = "GreaterThanOrEqualToThreshold"
  evaluation_periods        = "5"
  metric_name               = "CPUUtilization"
  namespace                 = "AWS/RDS"
  period                    = "30"
  statistic                 = "Maximum"
  threshold                 = "50"
  alarm_description         = "This metric monitors RDS CPU utilization"
  alarm_actions             = [aws_sns_topic.test_cloudwatch_updates.arn]
  insufficient_data_actions = []
}

resource "aws_sns_topic" "test_cloudwatch_updates" {
  name = "test-cloudwatch-notifications"
}

resource "aws_sns_topic_subscription" "cloudwatch_email_sub" {
  topic_arn = aws_sns_topic.test_cloudwatch_updates.arn
  protocol  = "email"
  endpoint  = "*****"
}

Would this alarm just monitor all of the instances within this AWS account? How am I able to target a speicific instance based on an instance ID for example?

CodePudding user response:

You're missing the dimensions attribute on your aws_cloudwatch_metric_alarm resource. You would most likely get an alarm that is perpetually in "Insufficient Data" status from that.

You need to add the DBInstanceIdentifier dimension to the alarm, like:

resource "aws_cloudwatch_metric_alarm" "CPUUtilization" {
   # your other alarm attributes

   dimensions = {
      DBInstanceIdentifier = "your_db_instance_id"
   }
}
  • Related