-
Notifications
You must be signed in to change notification settings - Fork 4
/
ec2-auto-scaling.tf
85 lines (76 loc) · 2.73 KB
/
ec2-auto-scaling.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
resource "aws_autoscaling_group" "asg_nexus" {
name = "asg_nexus"
launch_configuration = "${aws_launch_configuration.launch-config_nexus.id}"
vpc_zone_identifier = ["${element(var.subnets, 0)}"]
min_size = 1
max_size = 5
desired_capacity = 1
health_check_type = "EC2"
health_check_grace_period = 400
target_group_arns = ["${aws_alb_target_group.alb-tg-nexus.id}"]
depends_on = [
"aws_efs_file_system.efs_nexus",
"aws_efs_mount_target.efs-mt_nexus-public-1",
"aws_efs_mount_target.efs-mt_nexus-public-2",
]
tags = [
{
key = "Name"
value = "nexus-scale-group"
propagate_at_launch = true
},
]
}
resource "aws_autoscaling_policy" "asp_nexus-scale-up" {
name = "asp_nexus-scale-up"
adjustment_type = "ChangeInCapacity"
autoscaling_group_name = "${aws_autoscaling_group.asg_nexus.name}"
estimated_instance_warmup = 60
metric_aggregation_type = "Average"
policy_type = "StepScaling"
step_adjustment = {
metric_interval_lower_bound = 0
scaling_adjustment = 2
}
}
resource "aws_cloudwatch_metric_alarm" "cw-alarm_nexus-scale-up" {
alarm_name = "cw-alarm_nexus-scale-up"
alarm_description = "O uso de CPU atingiu 70% no ultimo minuto"
metric_name = "CPUReservation"
namespace = "AWS/ECS"
comparison_operator = "GreaterThanOrEqualToThreshold"
statistic = "Maximum"
threshold = 70
period = 60
evaluation_periods = 1
treat_missing_data = "notBreaching"
alarm_actions = ["${aws_autoscaling_policy.asp_nexus-scale-up.arn}"]
dimensions = {
Name = "ClusterName"
Value = "${var.cluster-name}"
}
}
resource "aws_autoscaling_policy" "asp_nexus-scale-down" {
name = "asp_nexus-scale-down"
adjustment_type = "PercentChangeInCapacity"
autoscaling_group_name = "${aws_autoscaling_group.asg_nexus.name}"
cooldown = 120
scaling_adjustment = -50
}
resource "aws_cloudwatch_metric_alarm" "cw-alarm_nexus-scale-down" {
alarm_name = "cw-alarm_nexus-scale-down"
alarm_description = "O uso de CPU está abaixo de 50% nos últimos 10 minutos"
metric_name = "CPUReservation"
namespace = "AWS/ECS"
comparison_operator = "LessThanThreshold"
statistic = "Maximum"
threshold = 50
period = 600
evaluation_periods = 1
treat_missing_data = "notBreaching"
alarm_actions = ["${aws_autoscaling_policy.asp_nexus-scale-down.arn}"]
dimensions = {
Name = "ClusterName"
Value = "${var.cluster-name}"
}
}