-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.tf
More file actions
374 lines (350 loc) · 10.7 KB
/
Copy pathmain.tf
File metadata and controls
374 lines (350 loc) · 10.7 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Monitoring Module - CloudWatch Dashboards and Alarms
#############
# CloudWatch Dashboard
#############
resource "aws_cloudwatch_dashboard" "main" {
dashboard_name = "${var.project_name}-${var.environment}-dashboard"
dashboard_body = jsonencode({
widgets = [
# EC2 CPU Utilization
{
type = "metric"
properties = {
metrics = [
["AWS/EC2", "CPUUtilization", "AutoScalingGroupName", var.eb_autoscaling_group_name, { stat = "Average" }]
]
period = 300
stat = "Average"
region = var.aws_region
title = "EC2 CPU Utilization (%)"
yAxis = {
left = {
min = 0
max = 100
}
}
}
},
# EC2 Memory Utilization (Custom Metric)
{
type = "metric"
properties = {
metrics = [
[{ expression = "SELECT AVG(MemoryUtilization) FROM \"CWAgent\"", id = "m1" }]
]
period = 300
region = var.aws_region
title = "EC2 Memory Utilization (%)"
yAxis = {
left = {
min = 0
max = 100
}
}
}
},
# EC2 Disk Utilization (Custom Metric) - Root filesystem
{
type = "metric"
properties = {
metrics = [
[{ expression = "SELECT AVG(DiskUtilization) FROM \"CWAgent\" WHERE path = '/'", id = "m1" }]
]
period = 300
region = var.aws_region
title = "EC2 Disk Utilization (%) - Root"
yAxis = {
left = {
min = 0
max = 100
}
}
}
},
{
type = "metric"
properties = {
metrics = [
["AWS/ApplicationELB", "RequestCount", "LoadBalancer", var.alb_arn_suffix, { stat = "Sum" }]
]
period = 300
stat = "Sum"
region = var.aws_region
title = "Request Count"
}
},
# HTTP 5xx Errors (Target Responses)
{
type = "metric"
properties = {
metrics = [
["AWS/ApplicationELB", "HTTPCode_Target_5XX_Count", "LoadBalancer", var.alb_arn_suffix, { stat = "Sum" }]
]
period = 300
stat = "Sum"
region = var.aws_region
title = "HTTP 5xx Errors"
}
},
# RDS CPU Utilization
{
type = "metric"
properties = {
metrics = [
["AWS/RDS", "CPUUtilization", "DBInstanceIdentifier", var.rds_instance_id, { stat = "Average" }]
]
period = 300
stat = "Average"
region = var.aws_region
title = "RDS CPU Utilization (%)"
yAxis = {
left = {
min = 0
max = 100
}
}
}
},
# RDS Read/Write IOPS
{
type = "metric"
properties = {
metrics = [
["AWS/RDS", "ReadIOPS", "DBInstanceIdentifier", var.rds_instance_id, { stat = "Average", label = "Read IOPS" }],
[".", "WriteIOPS", ".", ".", { stat = "Average", label = "Write IOPS" }]
]
period = 300
stat = "Average"
region = var.aws_region
title = "RDS Read/Write IOPS"
}
},
# RDS Network Throughput
{
type = "metric"
properties = {
metrics = [
["AWS/RDS", "NetworkReceiveThroughput", "DBInstanceIdentifier", var.rds_instance_id, { stat = "Average", label = "Network In" }],
[".", "NetworkTransmitThroughput", ".", ".", { stat = "Average", label = "Network Out" }]
]
period = 300
stat = "Average"
region = var.aws_region
title = "RDS Network Throughput (Bytes/sec)"
}
},
# RDS Database Connections
{
type = "metric"
properties = {
metrics = [
["AWS/RDS", "DatabaseConnections", "DBInstanceIdentifier", var.rds_instance_id, { stat = "Average" }]
]
period = 300
stat = "Average"
region = var.aws_region
title = "RDS Database Connections"
}
},
# RDS Freeable Memory
{
type = "metric"
properties = {
metrics = [
["AWS/RDS", "FreeableMemory", "DBInstanceIdentifier", var.rds_instance_id, { stat = "Average" }]
]
period = 300
stat = "Average"
region = var.aws_region
title = "RDS Freeable Memory (Bytes)"
}
},
# RDS Read/Write Latency
{
type = "metric"
properties = {
metrics = [
["AWS/RDS", "ReadLatency", "DBInstanceIdentifier", var.rds_instance_id, { stat = "Average", label = "Read Latency" }],
[".", "WriteLatency", ".", ".", { stat = "Average", label = "Write Latency" }]
]
period = 300
stat = "Average"
region = var.aws_region
title = "RDS Read/Write Latency (ms)"
}
},
# RDS Queue Depth
{
type = "metric"
properties = {
metrics = [
["AWS/RDS", "DiskQueueDepth", "DBInstanceIdentifier", var.rds_instance_id, { stat = "Average" }]
]
period = 300
stat = "Average"
region = var.aws_region
title = "RDS Disk Queue Depth"
}
},
# RDS Throughput (MB/s)
{
type = "metric"
properties = {
metrics = [
["AWS/RDS", "ReadThroughput", "DBInstanceIdentifier", var.rds_instance_id, { stat = "Average", label = "Read Throughput" }],
[".", "WriteThroughput", ".", ".", { stat = "Average", label = "Write Throughput" }]
]
period = 300
stat = "Average"
region = var.aws_region
title = "RDS Disk Throughput (Bytes/sec)"
}
}
]
})
}
#############
# EB CloudWatch Alarms
#############
# High CPU Alarm
resource "aws_cloudwatch_metric_alarm" "eb_cpu_high" {
alarm_name = "${var.project_name}-${var.environment}-eb-cpu-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 300
statistic = "Average"
threshold = 80
alarm_description = "This metric monitors EC2 CPU utilization"
alarm_actions = [var.sns_topic_arn]
dimensions = {
AutoScalingGroupName = var.eb_autoscaling_group_name
}
tags = {
Environment = var.environment
Project = var.project_name
}
}
# HTTP 5xx Error Rate Alarm
# This monitors server errors which indicate application health issues
resource "aws_cloudwatch_metric_alarm" "alb_http_5xx_errors" {
alarm_name = "${var.project_name}-${var.environment}-alb-http-5xx-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "HTTPCode_Target_5XX_Count"
namespace = "AWS/ApplicationELB"
period = 300
statistic = "Sum"
threshold = 10 # Alert if more than 10 5xx errors in 5 minutes
alarm_description = "High rate of HTTP 5xx errors indicates application issues"
alarm_actions = [var.sns_topic_arn]
dimensions = {
LoadBalancer = var.alb_arn_suffix
}
tags = {
Environment = var.environment
Project = var.project_name
}
}
#############
# RDS CloudWatch Alarms
#############
# High RDS CPU Alarm
resource "aws_cloudwatch_metric_alarm" "rds_cpu_high" {
alarm_name = "${var.project_name}-${var.environment}-rds-cpu-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/RDS"
period = 300
statistic = "Average"
threshold = 75
alarm_description = "RDS CPU utilization is high - may need optimization or larger instance"
alarm_actions = [var.sns_topic_arn]
dimensions = {
DBInstanceIdentifier = var.rds_instance_id
}
tags = {
Environment = var.environment
Project = var.project_name
}
}
# High RDS Read Latency Alarm
resource "aws_cloudwatch_metric_alarm" "rds_read_latency_high" {
alarm_name = "${var.project_name}-${var.environment}-rds-read-latency-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "ReadLatency"
namespace = "AWS/RDS"
period = 300
statistic = "Average"
threshold = 0.01 # 10ms in seconds
alarm_description = "RDS read latency is high - may indicate I/O bottleneck or need for indexing"
alarm_actions = [var.sns_topic_arn]
dimensions = {
DBInstanceIdentifier = var.rds_instance_id
}
tags = {
Environment = var.environment
Project = var.project_name
}
}
# Low RDS Freeable Memory Alarm
resource "aws_cloudwatch_metric_alarm" "rds_memory_low" {
alarm_name = "${var.project_name}-${var.environment}-rds-memory-low"
comparison_operator = "LessThanThreshold"
evaluation_periods = 2
metric_name = "FreeableMemory"
namespace = "AWS/RDS"
period = 300
statistic = "Average"
threshold = 524288000 # 500MB in bytes
alarm_description = "RDS freeable memory is low - may need larger instance or query optimization"
alarm_actions = [var.sns_topic_arn]
dimensions = {
DBInstanceIdentifier = var.rds_instance_id
}
tags = {
Environment = var.environment
Project = var.project_name
}
}
# High Memory Alarm
resource "aws_cloudwatch_metric_alarm" "eb_memory_high" {
alarm_name = "${var.project_name}-${var.environment}-eb-memory-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
threshold = 75
alarm_description = "This metric monitors EC2 memory utilization"
alarm_actions = [var.sns_topic_arn]
metric_query {
id = "m1"
return_data = true
metric {
namespace = "CWAgent"
metric_name = "MemoryUtilization"
period = 300
stat = "Average"
dimensions = {
AutoScalingGroupName = var.eb_autoscaling_group_name
}
}
}
tags = {
Environment = var.environment
Project = var.project_name
}
}
#############
# Log Groups
#############
resource "aws_cloudwatch_log_group" "eb_logs" {
name = "/aws/elasticbeanstalk/${var.project_name}-${var.environment}"
retention_in_days = var.log_retention_days
tags = {
Name = "${var.project_name}-${var.environment}-eb-logs"
Environment = var.environment
Project = var.project_name
}
}