获取AWS CloudWatch警报的Terraform参考

时间:2019-11-14 23:44:37

标签: terraform amazon-cloudwatch-metrics cloudwatch-alarms

以下terraform资源会创建AWS cloudwatch警报,但仍处于“数据不足”状态。我相信这是由于我使用的某些尺寸名称(DevicePath,fstype)可能不正确。我知道MountPath和InstanceID的名称正确,但是无法验证其他两个名称(DevicePath,fstype)。 AWS分别将这些维度称为路径,设备,fstype和主机,但是找不到地形将其称为参考。

resource "aws_cloudwatch_metric_alarm" "Low_Disk_Space_For_root_drive" {
  alarm_name                = "Low_Disk_Space_For_root_drive"
  comparison_operator       = "GreaterThanOrEqualToThreshold"
  evaluation_periods        = "2"
  metric_name               = "disk_used_percent"
  namespace                 = "CWAgent"

  dimensions {
    MountPath = "/"
    DevicePath = "/dev/xvda2"
    fstype = "xfs"
    InstanceId = "i-xxxxxxxxxxxxxxxxx"

  }

  period                    = "60"
  statistics                = "Maximum"
  threshold                 = "90" 
  alarm_description         = "Disk usage for / is high"
  insufficient_data_actions = []
  actions_enabled           = true
  alarm_actions             = ["arn:aws:sns:xxxxxx"]
  ok_actions                = ["arn:aws:sns:xxxxxx"]
}

3 个答案:

答案 0 :(得分:0)

将TreatMissingData添加到资源主体

resource "aws_cloudwatch_metric_alarm" "Low_Disk_Space_For_root_drive"{
  alarm_name                = "Low_Disk_Space_For_root_drive"
 comparison_operator       = "GreaterThanOrEqualToThreshold"
 evaluation_periods        = "2"
 metric_name               = "disk_used_percent"
 namespace                 = "CWAgent"

 dimensions {
   MountPath = "/"
   DevicePath = "/dev/xvda2"
   fstype = "xfs"
   InstanceId = "i-xxxxxxxxxxxxxxxxx"

  }

  period                    = "60"
  statistics                = "Maximum"
  threshold                 = "90" 
  alarm_description         = "Disk usage for / is high"
  insufficient_data_actions = []
  **TreatMissingData = "notBreaching"**
  actions_enabled           = true
  alarm_actions             = ["arn:aws:sns:xxxxxx"]
  ok_actions                = ["arn:aws:sns:xxxxxx"]
}

答案 1 :(得分:0)

要完成这项工作,您需要

  • MountPath更改为path
  • 删除DevicePath,然后添加device
  • 添加ImageIDInstanceType

在下面查看更新后的dimensions字段

注意-用您自己的值替换这些值。您应该能够从AWS控制台的Cloudwatch指标CWAgent部分中看到它们

resource "aws_cloudwatch_metric_alarm" "Low_Disk_Space_For_root_drive" {
  alarm_name                = "Low_Disk_Space_For_root_drive"
  comparison_operator       = "GreaterThanOrEqualToThreshold"
  evaluation_periods        = "2"
  metric_name               = "disk_used_percent"
  namespace                 = "CWAgent"

  dimensions {
      InstanceId    = "i-xxxxxxxxxxxxxxxxx"
      ImageId       = "your-image-id"
      InstanceType  = "your-instance-type"
      path          = "/"
      device        = "your-device"
      fstype        = "xfs"

  }

  period                    = "60"
  statistics                = "Maximum"
  threshold                 = "90" 
  alarm_description         = "Disk usage for / is high"
  insufficient_data_actions = []
  actions_enabled           = true
  alarm_actions             = ["arn:aws:sns:xxxxxx"]
  ok_actions                = ["arn:aws:sns:xxxxxx"]
}

答案 2 :(得分:0)

主要原因是因为你把它作为设备

将“/dev/xvda2”更改为 AWS 的确切设备,例如(“nvme1n1”)。您可以在 CloudWatch Metrics 中查看。