Search code examples
amazon-web-servicesamazon-rdsterraformamazon-cloudwatchamazon-cloudwatch-metrics

Why doesn't an aws_cloudwatch_metrics_alarm for RDS DBInstance show up in console?


I am trying to add a CloudWatch Alarm for an RDS database with Terraform 0.11.7 and AWS provider 1.30 versions.

I have created a topic.

resource "aws_sns_topic" "alarm" {
  name                    = "${var.cluster_name}-alarms-topic"
  delivery_policy         = <<-EOF
                            {
                              "http": {
                                "defaultHealthyRetryPolicy": {
                                  "minDelayTarget": 20,
                                  "maxDelayTarget": 20,
                                  "numRetries": 3,
                                  "numMaxDelayRetries": 0,
                                  "numNoDelayRetries": 0,
                                  "numMinDelayRetries": 0,
                                  "backoffFunction": "linear"
                                },
                                "disableSubscriptionOverrides": false,
                                "defaultThrottlePolicy": {
                                  "maxReceivesPerSecond": 1
                                }
                              }
                            }
                            EOF

  provisioner "local-exec" {
    command = "aws sns subscribe --topic-arn ${self.arn} --protocol email --notification-endpoint ${var.alerts_email}"
  }
}

I have created an RDS database.

resource "aws_db_instance" "database" {
  identifier                = "${var.cluster_name}"
  engine                    = "${var.engine}"
  engine_version            = "${var.engine_version}"
  allocated_storage         = "${var.db_size_gb}"
  instance_class            = "${var.instance_type}"
  name                      = "postgres"
  username                  = "postgres"
  password                  = "postgres"
  publicly_accessible       = false
  skip_final_snapshot       = true
  tags                      = "${var.tags}"
  vpc_security_group_ids    = ["${aws_security_group.database.id}"]
}

I have created an alarm metric:

resource "aws_cloudwatch_metric_alarm" "database-storage-low-alarm" {
  alarm_name                = "database-storage-low-alarm"
  alarm_description         = "This metric monitors database storage dipping below threshold"
  alarm_actions             = ["${var.alerts_arn}"]
  comparison_operator       = "LessThanThreshold"
  threshold                 = "20"
  evaluation_periods        = "2"
  metric_name               = "FreeStorageSpace"
  namespace                 = "RDS"
  period                    = "120"
  statistic                 = "Average"

  dimensions {
    DBInstanceIdentifier    = "${aws_db_instance.database.id}"
  }
}

I can apply the configuration, and querying the state for that resource shows its existence:

$ terraform state show module.staging.module.usw2.module.db.aws_cloudwatch_metric_alarm.database-storage-low-alarm
id                                    = database-storage-low-alarm
actions_enabled                       = true
alarm_actions.#                       = 1
alarm_actions.3493004098              = arn:aws:sns:us-west-2:114416042199:recs-api-staging-alarms-topic
alarm_description                     = This metric monitors database storage dipping below threshold
alarm_name                            = database-storage-low-alarm
comparison_operator                   = LessThanThreshold
datapoints_to_alarm                   = 0
dimensions.%                          = 1
dimensions.DBInstanceIdentifier       = recs-api-staging
evaluate_low_sample_count_percentiles =
evaluation_periods                    = 2
extended_statistic                    =
insufficient_data_actions.#           = 0
metric_name                           = FreeStorageSpace
namespace                             = RDS
ok_actions.#                          = 0
period                                = 120
statistic                             = Average
threshold                             = 20
treat_missing_data                    = missing
unit                                  =

When I open the Management Console for the RDS database created, the CloudWatch alarm does not appear to exist.

Does anyone have any idea as to why the created resource doesn't show up in the Console? One thing that jumped out at me was the dimensions.DBInstanceIdentifier = recs-api-staging in the state returned. Is that correct, or should it be an ARN instead?

Many thanks in advance!


Solution

  • Solution was to fix the namespace. Ensure use of namespace 'AWS/RDS' instead of RDS.