-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.tf
242 lines (219 loc) · 9.31 KB
/
main.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
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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
// CPU Utilization
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_too_high" {
count = var.create_high_cpu_alarm ? 1 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-highCPUUtilization"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.evaluation_period
metric_name = "CPUUtilization"
namespace = "AWS/RDS"
period = var.statistic_period
statistic = "Average"
threshold = var.cpu_utilization_too_high_threshold
alarm_description = "Average database CPU utilization is too high."
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
dimensions = {
DBInstanceIdentifier = var.db_instance_id
}
tags = var.tags
}
resource "aws_cloudwatch_metric_alarm" "cpu_credit_balance_too_low" {
count = var.create_low_cpu_credit_alarm ? length(regexall("(t2|t3|t4g)", var.db_instance_class)) > 0 ? 1 : 0 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-lowCPUCreditBalance"
comparison_operator = "LessThanThreshold"
evaluation_periods = var.evaluation_period
metric_name = "CPUCreditBalance"
namespace = "AWS/RDS"
period = var.statistic_period
statistic = "Average"
threshold = var.cpu_credit_balance_too_low_threshold
alarm_description = "Average database CPU credit balance is too low, a negative performance impact is imminent."
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
dimensions = {
DBInstanceIdentifier = var.db_instance_id
}
tags = var.tags
}
// Disk Utilization
resource "aws_cloudwatch_metric_alarm" "disk_queue_depth_too_high" {
count = var.create_high_queue_depth_alarm ? 1 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-highDiskQueueDepth"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.evaluation_period
metric_name = "DiskQueueDepth"
namespace = "AWS/RDS"
period = var.statistic_period
statistic = "Average"
threshold = var.disk_queue_depth_too_high_threshold
alarm_description = "Average database disk queue depth is too high, performance may be negatively impacted."
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
dimensions = {
DBInstanceIdentifier = var.db_instance_id
}
tags = var.tags
}
resource "aws_cloudwatch_metric_alarm" "disk_free_storage_space_too_low" {
count = var.create_low_disk_space_alarm ? 1 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-lowFreeStorageSpace"
comparison_operator = "LessThanThreshold"
evaluation_periods = var.evaluation_period
metric_name = "FreeStorageSpace"
namespace = "AWS/RDS"
period = var.statistic_period
statistic = "Average"
threshold = var.disk_free_storage_space_too_low_threshold
alarm_description = "Average database free storage space is too low and may fill up soon."
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
dimensions = {
DBInstanceIdentifier = var.db_instance_id
}
tags = var.tags
}
resource "aws_cloudwatch_metric_alarm" "disk_burst_balance_too_low" {
count = var.create_low_disk_burst_alarm ? 1 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-lowEBSBurstBalance"
comparison_operator = "LessThanThreshold"
evaluation_periods = var.evaluation_period
metric_name = "BurstBalance"
namespace = "AWS/RDS"
period = var.statistic_period
statistic = "Average"
threshold = var.disk_burst_balance_too_low_threshold
alarm_description = "Average database storage burst balance is too low, a negative performance impact is imminent."
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
dimensions = {
DBInstanceIdentifier = var.db_instance_id
}
tags = var.tags
}
// Memory Utilization
resource "aws_cloudwatch_metric_alarm" "memory_freeable_too_low" {
count = var.create_low_memory_alarm ? 1 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-lowFreeableMemory"
comparison_operator = "LessThanThreshold"
evaluation_periods = var.evaluation_period
metric_name = "FreeableMemory"
namespace = "AWS/RDS"
period = var.statistic_period
statistic = "Average"
threshold = var.memory_freeable_too_low_threshold
alarm_description = "Average database freeable memory is too low, performance may be negatively impacted."
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
dimensions = {
DBInstanceIdentifier = var.db_instance_id
}
tags = var.tags
}
resource "aws_cloudwatch_metric_alarm" "memory_swap_usage_too_high" {
count = var.create_swap_alarm ? 1 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-highSwapUsage"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.evaluation_period
metric_name = "SwapUsage"
namespace = "AWS/RDS"
period = var.statistic_period
statistic = "Average"
threshold = var.memory_swap_usage_too_high_threshold
alarm_description = "Average database swap usage is too high, performance may be negatively impacted."
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
dimensions = {
DBInstanceIdentifier = var.db_instance_id
}
tags = var.tags
}
// Connection Count
resource "aws_cloudwatch_metric_alarm" "connection_count_anomalous" {
count = var.create_anomaly_alarm ? 1 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-anomalousConnectionCount"
comparison_operator = "GreaterThanUpperThreshold"
evaluation_periods = var.evaluation_period
threshold_metric_id = "e1"
alarm_description = "Anomalous database connection count detected. Something unusual is happening."
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
metric_query {
id = "e1"
expression = "ANOMALY_DETECTION_BAND(m1, ${var.anomaly_band_width})"
label = "DatabaseConnections (Expected)"
return_data = "true"
}
metric_query {
id = "m1"
return_data = "true"
metric {
metric_name = "DatabaseConnections"
namespace = "AWS/RDS"
period = var.anomaly_period
stat = "Average"
unit = "Count"
dimensions = {
DBInstanceIdentifier = var.db_instance_id
}
}
}
tags = var.tags
}
// [postgres, aurora-postgres] Early Warning System for Transaction ID Wraparound
// more info - https://aws.amazon.com/blogs/database/implement-an-early-warning-system-for-transaction-id-wraparound-in-amazon-rds-for-postgresql/
resource "aws_cloudwatch_metric_alarm" "maximum_used_transaction_ids_too_high" {
count = contains(["aurora-postgresql", "postgres"], var.engine) ? 1 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-maximumUsedTransactionIDs"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.evaluation_period
metric_name = "MaximumUsedTransactionIDs"
namespace = "AWS/RDS"
period = var.statistic_period
statistic = "Average"
threshold = var.maximum_used_transaction_ids_too_high_threshold
alarm_description = "Nearing a possible critical transaction ID wraparound."
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
}
# SOC2 requirements
resource "aws_cloudwatch_metric_alarm" "read_iops_too_high" {
count = var.create_read_iops_alarm ? 1 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-read-iops-too-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.evaluation_period
metric_name = "ReadIOPS"
namespace = "AWS/RDS"
period = var.statistic_period
statistic = "Average"
threshold = var.read_iops_too_high_threshold
alarm_description = "Average Read IO over last ${(var.evaluation_period * var.statistic_period / 60)} minutes too high, performance may suffer"
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
dimensions = {
DBInstanceIdentifier = var.db_instance_id
}
}
resource "aws_cloudwatch_metric_alarm" "write_iops_too_high" {
count = var.create_write_iops_alarm ? 1 : 0
alarm_name = "${var.prefix}rds-${var.db_instance_id}-write-iops-too-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.evaluation_period
metric_name = "WriteIOPS"
namespace = "AWS/RDS"
period = var.statistic_period
statistic = "Average"
threshold = var.write_iops_too_high_threshold
alarm_description = "Average Write IO over last ${(var.evaluation_period * var.statistic_period / 60)} minutes too high, performance may suffer"
alarm_actions = var.actions_alarm
ok_actions = var.actions_ok
dimensions = {
DBInstanceIdentifier = var.db_instance_id
}
}