为什么RDS DBInstance的aws_cloudwatch_metrics_alarm没有显示在控制台中?

时间:2018-08-07 17:27:18

标签: amazon-web-services amazon-rds terraform amazon-cloudwatch amazon-cloudwatch-metrics

我正在尝试为Terraform 0.11.7和AWS提供程序1.30版本的RDS数据库添加CloudWatch Alarm。

我创建了一个主题。

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}"
  }
}

我已经创建了一个RDS数据库。

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}"]
}

我创建了一个警报指标:

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}"
  }
}

我可以应用配置,并查询该资源的状态以显示其存在:

$ 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                                  =

当我为创建的RDS数据库打开管理控制台时,CloudWatch警报似乎不存在。

有人对为什么创建的资源未显示在控制台中有任何想法吗?让我大吃一惊的是返回状态的dimensions.DBInstanceIdentifier = recs-api-staging。是正确的,还是应该是ARN?

非常感谢!

1 个答案:

答案 0 :(得分:0)

解决方案是修复名称空间。确保使用名称空间“ AWS / RDS”而不是RDS。