尝试创建由CW统一代理为Terraform中的多个实例发布的自定义磁盘指标

时间:2018-11-29 21:56:43

标签: amazon-web-services terraform

场景-如果要成功循环通过的每个设备都将发布多个ebs卷,则metric_name =“ disk_used_percent”将被发布,因此可以为每个设备创建警报,并在其中包含具有正确实例ID的名称。 Github-https://github.com/hirenshah005/cloudwatch

data "aws_instance" "x" {
count       = "${length(data.aws_instances.all_ec2.ids)}"
instance_id = "${element(data.aws_instances.all_ec2.*.id, count.index)}"
}

#------------Disk Alarms------------------
resource "aws_cloudwatch_metric_alarm" "disk_used_percent" {
count                     = "${length(data.aws_instance.x.*.ebs_block_device)}"
alarm_name                = "Disk_used_percent_below_10%_on-${data.aws_instance.x.id}-${element(data.aws_instance.x.ebs_block_device.device_name, count.index)}"
comparison_operator       = "LessThanOrEqualToThreshold"
evaluation_periods        = "2"
datapoints_to_alarm       = "2"
metric_name               = "disk_used_percent"
namespace                 = "Custim/CWAgent"
period                    = "900"
threshold                 = "10"
insufficient_data_actions = []

dimensions {
InstanceId = "${element(data.aws_instances.all_ec2.ids, count.index)}"
}

alarm_description = "This alarm will trigger when usable EBS storage is below 10% on --> ${data.aws_instances.all_ec2.ids[count.index]}"
alarm_actions     = ["${var.sns_notification_arn}"]
}

错误----------

data.aws_instances.all_ec2: Refreshing state...
data.aws_instance.x[0]: Refreshing state...
data.aws_instance.x[2]: Refreshing state...
data.aws_instance.x[4]: Refreshing state...
data.aws_instance.x[1]: Refreshing state...
data.aws_instance.x[3]: Refreshing state...

Error: Error refreshing state: 1 error(s) occurred:

* module.cloudwatch-metrics.data.aws_instance.x: 5 error(s) occurred:

* module.cloudwatch-metrics.data.aws_instance.x[1]: data.aws_instance.x.1: InvalidInstanceID.Malformed: Invalid id: "terraform-20181129025105352600000001"
        status code: 400, request id: 11c44ab9-06c0-466c-9d08-2e5cafcedb9a
* module.cloudwatch-metrics.data.aws_instance.x[0]: data.aws_instance.x.0: InvalidInstanceID.Malformed: Invalid id: "terraform-20181129025105352600000001"
        status code: 400, request id: 072d11e8-9067-41e2-a79b-6a4f09b5af07
* module.cloudwatch-metrics.data.aws_instance.x[3]: data.aws_instance.x.3: InvalidInstanceID.Malformed: Invalid id: "terraform-20181129025105352600000001"
        status code: 400, request id: 7a59d224-36bf-44be-ac17-ee8b65e6a6c5
* module.cloudwatch-metrics.data.aws_instance.x[4]: data.aws_instance.x.4: InvalidInstanceID.Malformed: Invalid id: "terraform-20181129025105352600000001"
        status code: 400, request id: 6ca1bfed-d38b-4868-86e5-bc042085ff90
* module.cloudwatch-metrics.data.aws_instance.x[2]: data.aws_instance.x.2: InvalidInstanceID.Malformed: Invalid id: "terraform-20181129025105352600000001"
        status code: 400, request id: f1dd970e-5faa-4c20-ae0f-00869e3bcdae

2 个答案:

答案 0 :(得分:0)

不幸的是,假设我正确理解了您的情况,那么我不认为Terraform v0.11.x当前可以做到这一点。

这是为什么:您有可变数量的实例,并附加了可变数量的EBS卷。例如实例1具有2个EBS卷,实例2具有3个,实例3具有1个,依此类推。

count上的aws_cloudwatch_metric_alarm必须是#个EBS卷的 sum 。在v0.11.x中,除了count之外没有更复杂的循环。

现在,如果附加到每个实例的卷的数量是固定的,则可以执行乘法计算以确定计数。 ->如果是这样,请更新您的问题,我将更新此答案。

答案 1 :(得分:0)

您可以在Terraform 0.12及更高版本using the multiple alarms module

中执行此操作

基本上,您可以指定具有相同警报条件的多个尺寸,它将为您创建必要的警报