|
| 1 | +# ASG(Auto Scaling Group) Part |
| 2 | +resource "aws_autoscaling_group" "terra_asg" { |
| 3 | + name = "terra_asg" |
| 4 | + target_group_arns = [aws_lb_target_group.terra_alb_target_grp.arn] |
| 5 | + |
| 6 | + health_check_type = "ELB" |
| 7 | + health_check_grace_period = 30 |
| 8 | + |
| 9 | + vpc_zone_identifier = [aws_subnet.autoscaling_ws_sub_1.id, aws_subnet.autoscaling_ws_sub_2.id] |
| 10 | + |
| 11 | + |
| 12 | + desired_capacity = 2 |
| 13 | + max_size = 4 |
| 14 | + min_size = 1 |
| 15 | + |
| 16 | + enabled_metrics = [ |
| 17 | + "GroupMinSize", |
| 18 | + "GroupMaxSize", |
| 19 | + "GroupDesiredCapacity", |
| 20 | + "GroupInServiceInstances", |
| 21 | + "GroupTotalInstances" |
| 22 | + ] |
| 23 | + metrics_granularity = "1Minute" |
| 24 | + |
| 25 | + launch_template { |
| 26 | + id = local.launch_template_id |
| 27 | + version = "$Latest" |
| 28 | + } |
| 29 | + |
| 30 | + tag { |
| 31 | + key = "Name" |
| 32 | + value = "Terra_asg" |
| 33 | + propagate_at_launch = true |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +# AutoScaling Policies |
| 38 | +# Scale Up Policy |
| 39 | +resource "aws_autoscaling_policy" "terra_scaleup" { |
| 40 | + name = "terra_scaleup" |
| 41 | + scaling_adjustment = 1 |
| 42 | + adjustment_type = "ChangeInCapacity" |
| 43 | + cooldown = 30 |
| 44 | + autoscaling_group_name = aws_autoscaling_group.terra_asg.name |
| 45 | +} |
| 46 | + |
| 47 | +# Cloudwatch Alarm |
| 48 | +resource "aws_cloudwatch_metric_alarm" "terra_scaleup_alarm" { |
| 49 | + alarm_name = "terra_scaleup_alarm" |
| 50 | + comparison_operator = "GreaterThanOrEqualToThreshold" |
| 51 | + evaluation_periods = "1" |
| 52 | + metric_name = "RequestCountPerTarget" |
| 53 | + |
| 54 | + namespace = "AWS/ApplicationELB" |
| 55 | + period = "60" |
| 56 | + statistic = "Sum" |
| 57 | + threshold = "100" |
| 58 | + |
| 59 | + dimensions = { |
| 60 | + TargetGroup = aws_lb_target_group.terra_alb_target_grp.arn_suffix |
| 61 | + LoadBalancer = aws_lb.terra_alb.arn_suffix |
| 62 | + } |
| 63 | + |
| 64 | + alarm_description = "Trigger when recieve more that 100 requests in 60 sec for alb" |
| 65 | + alarm_actions = [aws_autoscaling_policy.terra_scaleup.arn] |
| 66 | +} |
| 67 | + |
| 68 | +# Scale Down Policy |
| 69 | +resource "aws_autoscaling_policy" "terra_scaledown" { |
| 70 | + name = "terra_scaledown" |
| 71 | + scaling_adjustment = -1 |
| 72 | + adjustment_type = "ChangeInCapacity" |
| 73 | + cooldown = 30 |
| 74 | + autoscaling_group_name = aws_autoscaling_group.terra_asg.name |
| 75 | +} |
| 76 | + |
| 77 | +# CloudWatch Alarm |
| 78 | +resource "aws_cloudwatch_metric_alarm" "terra_scaledown_alarm" { |
| 79 | + alarm_name = "terra_scaledown_alarm" |
| 80 | + comparison_operator = "LessThanOrEqualToThreshold" |
| 81 | + evaluation_periods = "1" |
| 82 | + metric_name = "RequestCountPerTarget" |
| 83 | + |
| 84 | + namespace = "AWS/ApplicationELB" |
| 85 | + period = "60" |
| 86 | + statistic = "Sum" |
| 87 | + threshold = "100" |
| 88 | + |
| 89 | + dimensions = { |
| 90 | + TargetGroup = aws_lb_target_group.terra_alb_target_grp.arn_suffix |
| 91 | + LoadBalancer = aws_lb.terra_alb.arn_suffix |
| 92 | + } |
| 93 | + |
| 94 | + alarm_description = "Trigger when recieve less that 100 requests in 60 sec for alb" |
| 95 | + alarm_actions = [aws_autoscaling_policy.terra_scaledown.arn] |
| 96 | +} |
0 commit comments