如何将SNS通知附加到aws_appautoscaling_policy以便在terraform中进行ECS目标跟踪?

时间:2018-05-29 13:26:13

标签: terraform amazon-cloudwatch aws-ecs

如果我有以下terraform,并且SNS主题ARN可用,是否有办法将Cloudwatch通知事件附加到AWS在封面下创建的警报?

resource "aws_ecs_service" "service_definition" {
  name                               = "${var.servicename}"
  cluster                            = "${var.name}"
  task_definition                    = "${var.task_definition_arn}"
  desired_count                      = "${var.desired_count}"
  deployment_minimum_healthy_percent = "${var.deployment_minimum_healthy_percent}"

  ordered_placement_strategy {
    field = "attribute:ecs.availability-zone"
    type  = "spread"
  }
}

resource "aws_appautoscaling_target" "ecs_target" {
  max_capacity       = "${var.container_max_count}"
  min_capacity       = "${var.desired_count}"
  resource_id        = "service/${var.clustername}/${var.servicename}"
  role_arn           = "${var.ecs_iam_role}"
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"

  depends_on = ["aws_ecs_service.service_definition"]
}

resource "aws_appautoscaling_policy" "ecs_scale-memory" {
  name               = "${var.servicename}_scale-memory"
  policy_type        = "TargetTrackingScaling"
  resource_id        = "service/${var.clustername}/${var.servicename}"
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"

  target_tracking_scaling_policy_configuration {
    target_value       = "${var.target_container_memory_percent}"
    scale_in_cooldown  = "${var.target_scalein_cooldown_seconds}"
    scale_out_cooldown = "${var.target_scaleout_cooldown_seconds}"

    predefined_metric_specification {
      predefined_metric_type = "ECSServiceAverageMemoryUtilization"
    }
  }

  depends_on = ["aws_appautoscaling_target.ecs_target"]
}

AWS为跟踪事件创建了两个警报:

  • TargetTracking-service / clustername / servicename-AlarmHigh-GUID value
  • TargetTracking-service / clustername / servicename-AlarmLow-GUID value

这些操作可以通过附加的ecs_target扩展到所需的计数。我只想附上(通过Terraform)这些的SNS通知(就像EC2扩展事件一样)。

1 个答案:

答案 0 :(得分:0)

由于操作被定义为aws_cloudwatch_metric_alarm资源的一部分,因此无法独立管理。即使是aws cli equivalent,也需要将操作作为设置警报的一部分。由于此部分在您的情况下是抽象的,因此您无法添加SNS主题警报。您最好的选择可能是为通知SNS主题的每个人创建一个额外的警报。您可以使用describe-alarms了解内置警报的配置方式,然后在Terraform中复制它们。