-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud_watch.tf
179 lines (155 loc) · 6.08 KB
/
cloud_watch.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#creating a simple notification sistem to send out emails with the cloudWatch alarms
locals {
emails = ["[email protected]"]
}
resource "aws_sns_topic" "stack_notifications" {
name = "StackNotifications_AQUA"
}
resource "aws_sns_topic_subscription" "email_subscription" {
count = length(local.emails)
topic_arn = aws_sns_topic.stack_notifications.arn
protocol = "email"
#endpoint = "[email protected]" #change to your email
endpoint = local.emails[count.index]
}
#creating a cloudWatch alarm to monitor when something is stored in the S3 bucket
resource "aws_cloudwatch_metric_alarm" "example_alarm" {
alarm_name = "bucket_upload_alarm"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "NumberOfObjectsUploaded"
namespace = "AWS/S3"
period = "60"
statistic = "SampleCount"
threshold = "1"
alarm_description = "Alarm triggered when objects are uploaded to the S3 bucket."
alarm_actions = [aws_sns_topic.stack_notifications.arn]
dimensions = {
BucketName = aws_s3_bucket.bucket.id
}
}
#for every stack we create a cloudWatch alarm to monitor the number of resources in the stack
resource "aws_cloudwatch_metric_alarm" "resource_count_alarm_0" {
alarm_name = "ResourceCountAlarm_0"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "ResourceCount"
namespace = "AWS/CloudFormation"
period = 60
statistic = "Average"
threshold = 12 #change to the number of resources you want to monitor, the yaml script used as a template for the stacks has 31 resources
alarm_description = "Monitors the count of resources in the CloudFormation stack"
alarm_actions = [aws_sns_topic.stack_notifications.arn]
dimensions = {
StackName = aws_cloudformation_stack.mission_profile.id
}
}
resource "aws_cloudwatch_metric_alarm" "resource_count_alarm_1" {
alarm_name = "ResourceCountAlarm_1"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "ResourceCount"
namespace = "AWS/CloudFormation"
period = 60
statistic = "Average"
threshold = 5 #change to the number of resources you want to monitor
alarm_description = "Monitors the count of resources in the CloudFormation stack"
alarm_actions = [aws_sns_topic.stack_notifications.arn]
dimensions = {
StackName = aws_cloudformation_stack.data_processing.id
}
}
#CPU utilization alarms
#too low
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_alarm_too_low_0" {
alarm_name = "CPU_utilizationAlarm_LOW_0"
comparison_operator = "LessThanThreshold"
evaluation_periods = 1
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 60
statistic = "Average"
threshold = 20
alarm_description = "Triggered when CPU utilization is below 20%"
alarm_actions = [aws_sns_topic.stack_notifications.arn]
dimensions = {
InstanceId = aws_cloudformation_stack.mission_profile.id
}
}
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_alarm_too_low_1" {
alarm_name = "CPU_utilizationAlarm_LOW_1"
comparison_operator = "LessThanThreshold"
evaluation_periods = 1
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 60
statistic = "Average"
threshold = 20
alarm_description = "Triggered when CPU utilization is below 20%"
alarm_actions = [aws_sns_topic.stack_notifications.arn]
dimensions = {
InstanceId = aws_cloudformation_stack.data_processing.id
}
}
#too high
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_alarm_too_high_0" {
alarm_name = "CPU_UtilizationAlarm_HIGH_0"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 60
statistic = "Average"
threshold = 90
alarm_description = "Triggered when CPU utilization is above 90%"
alarm_actions = [aws_sns_topic.stack_notifications.arn]
dimensions = {
InstanceId = aws_cloudformation_stack.mission_profile.id
}
}
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_alarm_too_high_1" {
alarm_name = "CPU_UtilizationAlarm_HIGH_1"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 60
statistic = "Average"
threshold = 90
alarm_description = "Triggered when CPU utilization is above 90%"
alarm_actions = [aws_sns_topic.stack_notifications.arn]
dimensions = {
InstanceId = aws_cloudformation_stack.data_processing.id
}
}
#Memory utilization alarms
resource "aws_cloudwatch_metric_alarm" "memory_utilization_alarm_0" {
alarm_name = "MemoryUtilizationAlarm_0"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "MemoryUtilization"
namespace = "System/Linux"
period = 300
statistic = "Average"
threshold = 90
alarm_description = "Triggered when memory utilization is above 90%"
alarm_actions = [aws_sns_topic.stack_notifications.arn]
dimensions = {
InstanceId = aws_cloudformation_stack.mission_profile.id
}
}
resource "aws_cloudwatch_metric_alarm" "memory_utilization_alarm_1" {
alarm_name = "MemoryUtilizationAlarm_1"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "MemoryUtilization"
namespace = "System/Linux"
period = 300
statistic = "Average"
threshold = 90
alarm_description = "Triggered when memory utilization is above 90%"
alarm_actions = [aws_sns_topic.stack_notifications.arn]
dimensions = {
InstanceId = aws_cloudformation_stack.data_processing.id
}
}