Skip to content

Instantly share code, notes, and snippets.

@gomex
Last active August 23, 2019 22:07
Show Gist options
  • Save gomex/0421a339c2f16707f41a4eb89aa45a01 to your computer and use it in GitHub Desktop.
Save gomex/0421a339c2f16707f41a4eb89aa45a01 to your computer and use it in GitHub Desktop.
resource "aws_appautoscaling_target" "app_scale_target" {
service_namespace = "ecs"
resource_id = "service/${data.aws_ecs_cluster.main.id}/${aws_ecs_service.main.name}"
scalable_dimension = "ecs:service:DesiredCount"
max_capacity = "${var.asg_max_tasks}"
min_capacity = "${var.asg_min_tasks}"
}
resource "aws_appautoscaling_policy" "app_up" {
name = "${data.aws_ecs_cluster.main.id}-app-scale-up"
service_namespace = "${aws_appautoscaling_target.app_scale_target.service_namespace}"
resource_id = "${aws_appautoscaling_target.app_scale_target.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.app_scale_target.scalable_dimension}"
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = "${var.asg_cooldown_to_scale_up_again}"
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = 1
}
}
}
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_high" {
alarm_name = "${data.aws_ecs_cluster.main.id}-CPU-High"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "${var.asg_evaluation_periods}"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "${var.asg_period}"
statistic = "Average"
threshold = "${var.asg_threshold_cpu_to_scale_up}"
dimensions {
ClusterName = "${data.aws_ecs_cluster.main.id}"
ServiceName = "${aws_ecs_service.main.name}"
}
alarm_actions = ["${aws_appautoscaling_policy.app_up.arn}"]
}
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_low" {
alarm_name = "${data.aws_ecs_cluster.main.id}-CPU-Utilization-Low"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "${var.asg_threshold_cpu_to_scale_down}"
dimensions {
ClusterName = "${data.aws_ecs_cluster.main.id}"
ServiceName = "${aws_ecs_service.main.name}"
}
alarm_actions = ["${aws_appautoscaling_policy.app_down.arn}"]
}
resource "aws_appautoscaling_policy" "app_down" {
name = "${data.aws_ecs_cluster.main.id}-scale-down"
service_namespace = "${aws_appautoscaling_target.app_scale_target.service_namespace}"
resource_id = "${aws_appautoscaling_target.app_scale_target.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.app_scale_target.scalable_dimension}"
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = "${var.asg_cooldown_to_scale_down_again}"
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment