-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.tf
90 lines (76 loc) · 2.6 KB
/
metrics.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
resource "aws_sns_topic" "topic" {
name = join("-", [var.name, "notification-topic"])
tags = {
Environment = terraform.workspace
}
}
resource "aws_sns_topic_subscription" "email_sub" {
count = var.use_email_notification ? 1 : 0
topic_arn = aws_sns_topic.topic.arn
protocol = "email"
endpoint = var.sns_email
}
resource "aws_cloudwatch_metric_alarm" "instance_number" {
alarm_name = join(": ", ["Active instances for", aws_autoscaling_group.asg.name])
comparison_operator = "LessThanThreshold"
evaluation_periods = 1
metric_name = "GroupTotalInstances"
namespace = "AWS/AutoScaling"
period = "180"
statistic = "Average"
threshold = aws_autoscaling_group.asg.min_size
actions_enabled = true
alarm_actions = [aws_sns_topic.topic.arn]
ok_actions = [aws_sns_topic.topic.arn]
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.asg.name
}
alarm_description = "This metric monitors running instances"
}
resource "aws_cloudwatch_metric_alarm" "cpu_utilization" {
alarm_name = join(": ", ["CPU utilization", aws_autoscaling_group.asg.name])
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 1
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "180"
statistic = "Average"
threshold = "80"
actions_enabled = true
alarm_actions = [aws_sns_topic.topic.arn]
ok_actions = [aws_sns_topic.topic.arn]
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.asg.name
}
alarm_description = "This metric monitors cpu utilization running instances"
}
resource "aws_cloudwatch_metric_alarm" "mem_utilization" {
alarm_name = join(": ", ["Memory utilization", aws_autoscaling_group.asg.name])
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 1
metric_name = "mem_available_percent"
namespace = "CWAgent"
period = "180"
statistic = "Average"
threshold = "80"
actions_enabled = true
alarm_actions = [aws_sns_topic.topic.arn]
ok_actions = [aws_sns_topic.topic.arn]
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.asg.name
}
alarm_description = "This metric monitors memory utilization running instances"
}
output "sns_topic" {
description = "SNS topic display name"
value = aws_sns_topic.topic.display_name
}
output "sns_topic_arn" {
description = "SNS topic ARN"
value = aws_sns_topic.topic.arn
}
output "sns_subscription" {
description = "SNS topic email subscripted"
sensitive = true
value = aws_sns_topic_subscription.email_sub
}