服务器列表的 Cloudwatch 警报 [英] Cloudwatch alarm for list of servers

查看:23
本文介绍了服务器列表的 Cloudwatch 警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在服务器列表中设置一些警报,我的服务器在本地定义如下:

I am trying to set a few alerts across a list of servers, I have my servers defined in locals as below:

  locals {
      my_list = [
        "server1",
        "server2"
      ]
    }

然后我将我的 cloudwatch 警报定义为:(这是一个这样的警报)

I then defined my cloudwatch alerts as so: (This is one such alert)

resource "aws_cloudwatch_metric_alarm" "ec2-high-cpu-warning" {
  for_each            = toset(local.my_list)
  alarm_name          = "ec2-high-cpu-warning-for-${each.key}"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "CPUUtilization"
  namespace           = "AWS/EC2"
  dimensions = {
    instanceid   = values(data.aws_instances.my_instances)[*].ids
    instancename = local.my_list
  }

  period                    = "60"
  statistic                 = "Average"
  threshold                 = "11"
  alarm_description         = "This warning is for high cpu utilization for ${each.key}"
  actions_enabled           = true
  alarm_actions             = [data.aws_sns_topic.my_sns.arn]
  insufficient_data_actions = []
  treat_missing_data        = "notBreaching"
}

我也这样定义数据源:

data "aws_instances" "my_instances" {

  for_each = toset(local.my_list)

  instance_tags = {
    Name = each.key
  }
}

现在当我运行 terraform plan 时出现错误:

Now when i run terraform plan i get an error:

| data.aws_instances.my_instances is object with 2 attributes

属性dimensions"的值不合适:元素instanceid":字符串必填.

Inappropriate value for attribute "dimensions": element "instanceid": string required.

推荐答案

在你的 for_each 你应该使用 data.aws_instance.my_instances:

In your for_each you should use data.aws_instance.my_instances:

resource "aws_cloudwatch_metric_alarm" "ec2-high-cpu-warning" {

  for_each            = data.aws_instance.my_instances
  
  alarm_name          = "ec2-high-cpu-warning-for-${each.key}"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "CPUUtilization"
  namespace           = "AWS/EC2"
  
  dimensions = {
    instanceid   = each.value.id
    instancename = each.key
  }

  period                    = "60"
  statistic                 = "Average"
  threshold                 = "11"
  alarm_description         = "This warning is for high cpu utilization for ${each.key}"
  actions_enabled           = true
  alarm_actions             = [data.aws_sns_topic.my_sns.arn]
  insufficient_data_actions = []
  treat_missing_data        = "notBreaching"
}

上面将为您的两个实例创建两个警报(每个实例一个警报),其中 instancename 将是 server1 或 ``server2`.

The above will create two alarms for your two instances (one alarm per instance) where instancename will be server1 or ``server2`.

这篇关于服务器列表的 Cloudwatch 警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆