Guest User

Untitled

a guest
Mar 5th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 2.77 KB | None | 0 0
  1. # auto_scaling.tf
  2.  
  3. resource "aws_appautoscaling_target" "target" {
  4.   service_namespace  = "ecs"
  5.   resource_id        = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"
  6.   scalable_dimension = "ecs:service:DesiredCount"
  7.   min_capacity       = 1
  8.   max_capacity       = 5
  9. }
  10.  
  11. # Automatically scale capacity up by one
  12. resource "aws_appautoscaling_policy" "up" {
  13.   name               = "${var.project}_scale_up"
  14.   service_namespace  = "ecs"
  15.   resource_id        = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"
  16.   scalable_dimension = "ecs:service:DesiredCount"
  17.  
  18.   step_scaling_policy_configuration {
  19.     adjustment_type         = "ChangeInCapacity"
  20.     cooldown                = 60
  21.     metric_aggregation_type = "Maximum"
  22.  
  23.     step_adjustment {
  24.       metric_interval_lower_bound = 0
  25.       scaling_adjustment          = 1
  26.     }
  27.   }
  28.  
  29.   depends_on = [aws_appautoscaling_target.target]
  30. }
  31.  
  32. # Automatically scale capacity down by one
  33. resource "aws_appautoscaling_policy" "down" {
  34.   name               = "${var.project}_scale_down"
  35.   service_namespace  = "ecs"
  36.   resource_id        = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"
  37.   scalable_dimension = "ecs:service:DesiredCount"
  38.  
  39.   step_scaling_policy_configuration {
  40.     adjustment_type         = "ChangeInCapacity"
  41.     cooldown                = 60
  42.     metric_aggregation_type = "Maximum"
  43.  
  44.     step_adjustment {
  45.       metric_interval_lower_bound = 0
  46.       scaling_adjustment          = -1
  47.     }
  48.   }
  49.  
  50.   depends_on = [aws_appautoscaling_target.target]
  51. }
  52.  
  53. # CloudWatch alarm that triggers the autoscaling up policy
  54. resource "aws_cloudwatch_metric_alarm" "service_cpu_high" {
  55.   alarm_name          = "${var.project}_cpu_utilization_high"
  56.   comparison_operator = "GreaterThanOrEqualToThreshold"
  57.   evaluation_periods  = "1"
  58.   metric_name         = "CPUUtilization"
  59.   namespace           = "AWS/ECS"
  60.   period              = "60"
  61.   statistic           = "Average"
  62.   threshold           = "50"
  63.  
  64.   dimensions = {
  65.     ClusterName = aws_ecs_cluster.main.name
  66.     ServiceName = aws_ecs_service.main.name
  67.   }
  68.  
  69.   alarm_actions = [aws_appautoscaling_policy.up.arn]
  70. }
  71.  
  72. # CloudWatch alarm that triggers the autoscaling down policy
  73. resource "aws_cloudwatch_metric_alarm" "service_cpu_low" {
  74.   alarm_name          = "${var.project}_cpu_utilization_low"
  75.   comparison_operator = "LessThanOrEqualToThreshold"
  76.   evaluation_periods  = "2"
  77.   metric_name         = "CPUUtilization"
  78.   namespace           = "AWS/ECS"
  79.   period              = "60"
  80.   statistic           = "Average"
  81.   threshold           = "10"
  82.  
  83.   dimensions = {
  84.     ClusterName = aws_ecs_cluster.main.name
  85.     ServiceName = aws_ecs_service.main.name
  86.   }
  87.  
  88.   alarm_actions = [aws_appautoscaling_policy.down.arn]
  89. }
Advertisement
Add Comment
Please, Sign In to add comment