1

我正在尝试为具有 Terraform 0.11.7 和 AWS 提供商 1.30 版本的 RDS 数据库添加 CloudWatch 警报。

我创建了一个主题。

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?

提前谢谢了!

4

1 回答 1

1

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

于 2018-08-08T16:02:59.033 回答