-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasg.tf
96 lines (79 loc) · 2.6 KB
/
asg.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
86
87
88
89
90
91
92
93
94
95
96
# ASG(Auto Scaling Group) Part
resource "aws_autoscaling_group" "terra_asg" {
name = "terra_asg"
target_group_arns = [aws_lb_target_group.terra_alb_target_grp.arn]
health_check_type = "ELB"
health_check_grace_period = 30
vpc_zone_identifier = [aws_subnet.autoscaling_ws_sub_1.id, aws_subnet.autoscaling_ws_sub_2.id]
desired_capacity = 2
max_size = 4
min_size = 1
enabled_metrics = [
"GroupMinSize",
"GroupMaxSize",
"GroupDesiredCapacity",
"GroupInServiceInstances",
"GroupTotalInstances"
]
metrics_granularity = "1Minute"
launch_template {
id = local.launch_template_id
version = "$Latest"
}
tag {
key = "Name"
value = "Terra_asg"
propagate_at_launch = true
}
}
# AutoScaling Policies
# Scale Up Policy
resource "aws_autoscaling_policy" "terra_scaleup" {
name = "terra_scaleup"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 30
autoscaling_group_name = aws_autoscaling_group.terra_asg.name
}
# Cloudwatch Alarm
resource "aws_cloudwatch_metric_alarm" "terra_scaleup_alarm" {
alarm_name = "terra_scaleup_alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "RequestCountPerTarget"
namespace = "AWS/ApplicationELB"
period = "60"
statistic = "Sum"
threshold = "100"
dimensions = {
TargetGroup = aws_lb_target_group.terra_alb_target_grp.arn_suffix
LoadBalancer = aws_lb.terra_alb.arn_suffix
}
alarm_description = "Trigger when recieve more that 100 requests in 60 sec for alb"
alarm_actions = [aws_autoscaling_policy.terra_scaleup.arn]
}
# Scale Down Policy
resource "aws_autoscaling_policy" "terra_scaledown" {
name = "terra_scaledown"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 30
autoscaling_group_name = aws_autoscaling_group.terra_asg.name
}
# CloudWatch Alarm
resource "aws_cloudwatch_metric_alarm" "terra_scaledown_alarm" {
alarm_name = "terra_scaledown_alarm"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "RequestCountPerTarget"
namespace = "AWS/ApplicationELB"
period = "60"
statistic = "Sum"
threshold = "100"
dimensions = {
TargetGroup = aws_lb_target_group.terra_alb_target_grp.arn_suffix
LoadBalancer = aws_lb.terra_alb.arn_suffix
}
alarm_description = "Trigger when recieve less that 100 requests in 60 sec for alb"
alarm_actions = [aws_autoscaling_policy.terra_scaledown.arn]
}