Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # auto_scaling.tf
- resource "aws_appautoscaling_target" "target" {
- service_namespace = "ecs"
- resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"
- scalable_dimension = "ecs:service:DesiredCount"
- min_capacity = 1
- max_capacity = 5
- }
- # Automatically scale capacity up by one
- resource "aws_appautoscaling_policy" "up" {
- name = "${var.project}_scale_up"
- service_namespace = "ecs"
- resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"
- scalable_dimension = "ecs:service:DesiredCount"
- step_scaling_policy_configuration {
- adjustment_type = "ChangeInCapacity"
- cooldown = 60
- metric_aggregation_type = "Maximum"
- step_adjustment {
- metric_interval_lower_bound = 0
- scaling_adjustment = 1
- }
- }
- depends_on = [aws_appautoscaling_target.target]
- }
- # Automatically scale capacity down by one
- resource "aws_appautoscaling_policy" "down" {
- name = "${var.project}_scale_down"
- service_namespace = "ecs"
- resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"
- scalable_dimension = "ecs:service:DesiredCount"
- step_scaling_policy_configuration {
- adjustment_type = "ChangeInCapacity"
- cooldown = 60
- metric_aggregation_type = "Maximum"
- step_adjustment {
- metric_interval_lower_bound = 0
- scaling_adjustment = -1
- }
- }
- depends_on = [aws_appautoscaling_target.target]
- }
- # CloudWatch alarm that triggers the autoscaling up policy
- resource "aws_cloudwatch_metric_alarm" "service_cpu_high" {
- alarm_name = "${var.project}_cpu_utilization_high"
- comparison_operator = "GreaterThanOrEqualToThreshold"
- evaluation_periods = "1"
- metric_name = "CPUUtilization"
- namespace = "AWS/ECS"
- period = "60"
- statistic = "Average"
- threshold = "50"
- dimensions = {
- ClusterName = aws_ecs_cluster.main.name
- ServiceName = aws_ecs_service.main.name
- }
- alarm_actions = [aws_appautoscaling_policy.up.arn]
- }
- # CloudWatch alarm that triggers the autoscaling down policy
- resource "aws_cloudwatch_metric_alarm" "service_cpu_low" {
- alarm_name = "${var.project}_cpu_utilization_low"
- comparison_operator = "LessThanOrEqualToThreshold"
- evaluation_periods = "2"
- metric_name = "CPUUtilization"
- namespace = "AWS/ECS"
- period = "60"
- statistic = "Average"
- threshold = "10"
- dimensions = {
- ClusterName = aws_ecs_cluster.main.name
- ServiceName = aws_ecs_service.main.name
- }
- alarm_actions = [aws_appautoscaling_policy.down.arn]
- }
Advertisement
Add Comment
Please, Sign In to add comment