terraform资源应基于变量创建多个CloudWatch警报,该警报已在计划中确认,但仅部署一个警报

时间:2020-10-07 15:13:06

标签: amazon-web-services terraform

地形代码:

resource "aws_cloudwatch_metric_alarm" "disk_percentage_low" {
  for_each                  = toset(var.instance)

  alarm_name                = "disk_percentage_low"  
  comparison_operator       = "LessThanOrEqualToThreshold"  
  evaluation_periods        = "1"                  
  metric_name"LogicalDisk   = % Free Space"   
  namespace                 = "CWAgent"  
  period                    = "60"   
  statistic                 = "Average"            
  threshold                 = "20"
  alarm_description         = "This metric monitors ec2 disk utilization"   
  actions_enabled           = "true"    
  alarm_actions             = [aws_sns_topic.disk_alarm.arn]   
  insufficient_data_actions = []
  
  dimensions = {
    InstanceId   = var.InstanceId
    InstanceType = var.InstanceType
    ImageId      = var.ImageId
    instance     = each.value
    objectname   = var.objectname   
  } 
}

variable "instance" {   
  type = list   
  default = ["E:" , "D:"]
}

这是Terraform计划的输出。如您所见,它应该为实例E:D:创建两个CloudWatch警报。但是,当应用成功运行后,当我进入控制台时,它只会为实例E:创建警报。

# aws_cloudwatch_metric_alarm.disk_percentage_low["D:"] will be created   
+ resource "aws_cloudwatch_metric_alarm"
"disk_percentage_low" {
  + actions_enabled      = true
  + alarm_actions        = (known after apply)
  + alarm_description    = "This metric monitors ec2 disk utilization"
  + alarm_name           = "disk_percentage_low"
  + arn                  = (known after apply)
  + comparison_operator  = "LessThanOrEqualToThreshold"
  + dimensions                              = {
      + "ImageId"      = "ami-0aac9d7fa83beb6d2"
      + "InstanceId"   = "i-021e580bccd130ac6"
      + "InstanceType" = "t2.micro"
      + "instance"     = "D:"
      + "objectname"   = "LogicalDisk"
    }
  + evaluate_low_sample_count_percentiles = (known after apply)
  + evaluation_periods                    = 1
  + id                                    = (known after apply)
  + metric_name                           = "LogicalDisk % Free Space"
  + namespace                             = "CWAgent"
  + period                                = 60
  + statistic                             = "Average"
  + threshold                             = 20
  + treat_missing_data                    = "missing"
}

# aws_cloudwatch_metric_alarm.disk_percentage_low["E:"] will be created   
+ resource "aws_cloudwatch_metric_alarm"
"disk_percentage_low" {
  + actions_enabled     = true
  + alarm_actions       = (known after apply)
  + alarm_description   = "This metric monitors ec2 disk utilization"
  + alarm_name          = "disk_percentage_low"
  + arn                 = (known after apply)
  + comparison_operator = "LessThanOrEqualToThreshold"
  + dimensions                            = {
      + "ImageId"      = "ami-0aac9d7fa83beb6d2"
      + "InstanceId"   = "i-021e580bccd130ac6"
      + "InstanceType" = "t2.micro"
      + "instance"     = "E:"
      + "objectname"   = "LogicalDisk"
    }
  + evaluate_low_sample_count_percentiles = (known after apply)
  + evaluation_periods                    = 1
  + id                                    = (known after apply)
  + metric_name                           = "LogicalDisk % Free Space"
  + namespace                             = "CWAgent"
  + period                                = 60
  + statistic                             = "Average"
  + threshold                             = 20
  + treat_missing_data                    = "missing"
}

2 个答案:

答案 0 :(得分:1)

我认为这是因为您修复了alarm_name

 alarm_name                = "disk_percentage_low"  

因此,第二个警报将覆盖第一个警报,因为两者名称相同。快速修复方法可能是:

alarm_name                = "disk_percentage_low_${each.value}"

但是在上面不能确定是否允许:的名称,因为您有“ E:”和“ D:”。您可以检查一下。

答案 1 :(得分:0)

您需要设置警报的计数,然后使用索引来配置名称(您的实例资源将需要计数,并且警报计数将被设置为实例计数的长度。监视):

resource "aws_cloudwatch_metric_alarm" "WorkerEC2Disk90" {
  count = length(aws_instance.worker)
  alarm_name                = "worker-${count.index + 1}-${var.app_env}-EC2Disk90%"
  comparison_operator       = "GreaterThanOrEqualToThreshold"
  evaluation_periods        = "2"
  metric_name               = "disk_used_percent"
  namespace                 = "CWAgent"
  period                    = "300"
  statistic                 = "Average"
  threshold                 = "90"
  alarm_description         = "This metric monitors ec2 disk utilization"
  datapoints_to_alarm       = 2
  alarm_actions             = [var.org_account_cloudwatch_to_slack]
  ok_actions                = [var.org_account_cloudwatch_to_slack]
  insufficient_data_actions = []

  dimensions = {
    InstanceId = aws_instance.worker[count.index].id,
    ImageId    = aws_instance.worker[count.index].ami,
    InstanceType = aws_instance.worker[count.index].instance_type,
    path        = "/",
    device      = "nvme0n1p1",
    fstype      = "ext4"
  }
}

resource "aws_instance" "worker" {
  count                       = var.worker_instance_count
  ami                          = var.ubuntu18_ami
  ...
}

您的dimensions哈希也将需要使用计数索引,否则Cloudwatch不会知道何时显式监视实例。